How do I change `src` folder to something else in create-react-app

后端 未结 6 1755
慢半拍i
慢半拍i 2020-12-08 20:05

I\'d like to know if it\'s possible using react-script to rename src to something else like app folder

相关标签:
6条回答
  • 2020-12-08 20:20

    I know this is an old question but I'm still gonna post my solution since it might help someone.

    I got it working by doing the following:

    1. Run npm run eject. This exposes some internal configuration stuff from create-react-app
    2. Open your package.json and edit the respective regexes under jest.collectCoverageFrom and jest.testMatch to match your test path
    3. Alter the paths for appSrc, appIndexJs and testsSetup in the config/paths.js file
    0 讨论(0)
  • 2020-12-08 20:22

    Perhaps a symbolic link might address your reasons for wanting to do this:

    ln -s ./src/ ./app

    The src folder will remain but you can work with it as if it was the app folder.

    If, like me you're using vscode you can also do:

    Cmd-shift-p search workspace settings, and add the following:

    {
      "files.exclude": {
          "src/": true
      }
    }
    

    You could do similarly with other editors

    0 讨论(0)
  • 2020-12-08 20:30

    Create file in root of your project, insert this code and run.

    const fs = require('fs');
    const path = './node_modules/react-scripts/config/paths.js';
    const folder = 'app';
    
    fs.readFile(path, 'utf8', (err, data) => {
     if (err) throw err;
     data = data.replace(/src/g, folder);
     fs.writeFile(path, data, 'utf8');
    });

    0 讨论(0)
  • 2020-12-08 20:31

    Not sure if this answers your question but I'll give it a shot. My directory structure looks like this:

    /root
    --/app
    ----/build
    ----/public
    ------index.html
    ----/src
    ------index.js
    app.js
    package.js
    

    My /root/package.json has this in it:

      "scripts": {
        "build": "cd app && npm run build",
        "start": "node app.js",
        "serve": "cd app && npm run start"
      },
      "dependencies": {
        "express": "^4.8.0",
        "react": "^16.2.0",
        "react-dom": "^16.2.0",
        "react-router": "^4.2.0",
        "react-router-dom": "^4.2.2",
        "react-scripts": "^1.0.17"
      },
    

    and my /root/app/package.json looks like this:

      "scripts": {
        "build": "react-scripts build",
        "start": "set PORT=3000 && react-scripts start"
      },
      "dependencies": {
        "react-scripts": "^1.0.17"
      }
    

    To run the development version of Reactjs, in the /root I can just npm run serve to serve up the dev version.

    I am using node and express, so to run the production version of Reactjs, in the /root I can just npm run build to create the /root/app/build directory. I have a router that looks like this:

    var options = {root : config.siteRoot + '/app/build'};
    
    mainRte.all('/', function(req, res) {
        console.log('In mainRte.all Root');
        res.sendFile('index.html', options);
    });
    

    so when I run /root/app.js in node and surf to "/" it opens up /root/app/public/index.html and then /root/app/index.js.

    Hopefully that helps.

    0 讨论(0)
  • 2020-12-08 20:33

    You can use react-app-rewired to override react paths configuration. In my case, I can change the paths in config-overrides.js file

    const path = require('path');
    
    module.exports = {
        paths: function (paths, env) {        
            paths.appIndexJs = path.resolve(__dirname, 'mysrc/client.js');
            paths.appSrc = path.resolve(__dirname, 'mysrc');
            return paths;
        },
    }
    
    0 讨论(0)
  • 2020-12-08 20:40

    T0astBread's answer is nearly perfect, but there's an additional reference to "src" that he missed inside modules.js.

    Specifically:

        return {
          src: paths.appSrc,
        };
    

    needs to be changed to

        return {
          newSrcName: paths.appSrc,
        };
    
    0 讨论(0)
提交回复
热议问题