Use custom build output folder when using create-react-app

后端 未结 13 896
被撕碎了的回忆
被撕碎了的回忆 2020-12-04 17:24

Facebook provides a create-react-app command to build react apps. When we run npm run build, we see output in /build folder.

相关标签:
13条回答
  • 2020-12-04 18:04

    I had the scenario like want to rename the folder and change the build output location, and used below code in the package.json with the latest version

    "build": "react-scripts build && mv build ../my_bundles"
    
    0 讨论(0)
  • 2020-12-04 18:04

    Quick compatibility build script (also works on Windows):

    "build": "react-scripts build && rm -rf docs && mv build docs"
    
    0 讨论(0)
  • 2020-12-04 18:04

    Open Command Prompt inside your Application's source. Run the Command

    npm run eject

    Open your scripts/build.js file and add this at the beginning of the file after 'use strict' line

    'use strict';
    ....
    process.env.PUBLIC_URL = './' 
    // Provide the current path
    .....
    

    Open your config/paths.js and modify the buildApp property in the exports object to your destination folder. (Here, I provide 'react-app-scss' as the destination folder)

    module.exports = {
    .....
    appBuild: resolveApp('build/react-app-scss'),
    .....
    }
    

    Run

    npm run build

    Note: Running Platform dependent scripts are not advisable

    0 讨论(0)
  • 2020-12-04 18:06

    Create-react-app Version 2+ answer

    For recent (> v2) versions of create-react-app (and possible older as well), add the following line to your package.json, then rebuild.

    "homepage": "./"
    

    You should now see the build/index.html will have relative links ./static/... instead of links to the server root: /static/....

    0 讨论(0)
  • For anyone still looking for an answer that works on both Linux and Windows:

    Add this to the scripts section in package.json

    "build": "react-scripts build && mv build ../docs || move build ../docs",
    

    with ../docs is the relative folder you want to move the build folder to

    0 讨论(0)
  • 2020-12-04 18:08

    webpack =>

    renamed as build to dist

    output: {
          filename: '[name].bundle.js',
          path: path.resolve(__dirname, 'dist'),
        },
    
    0 讨论(0)
提交回复
热议问题