2

I have a web app created with create-react-app. Instead of having just one build folder generated via "yarn build" I need to have multiple build folders each using a different configuration file (for database connection etc).

How can I do that?

0

2 Answers 2

0

Yes, it is possible. Just define another build script.

Find script in package.json something like this:

"scripts": {
    "start": "node scripts/start.js",
    "build": "npm run git-info && node scripts/build.js",
    "test": "git-info && node scripts/test.js --env=jsdom",
    "git-info": "git log -1 --pretty=format:\"%h%x09%x09%ad%x09%s\" > src/static/gitInfo.txt"
},

You can define something like this:

"scripts": { 
    "build": "npm run git-info && node scripts/build.js && node scripts/build2.js && node scripts/build3.js", 
},

When you call yarn build , its called all commends in build section (npm run git-info && node scripts/build.js && node scripts/build2.js && node scripts/build3.js)

(yarn or npm...)

In your build script scripts/build.js, scripts/build1.js, ... you can define what you want (output folders, etc...)

Sign up to request clarification or add additional context in comments.

3 Comments

What is build.js? Can you give an example?
@ynotu. Build.js is a file generated by create-react-app. See: pastebin.com/2NkTv3sM
Do I need to eject for that? How do I create different cfg files and how do I integrate them in the build process?
0

Assuming you have one configuration file per environment, you could have a build script that takes the config as an argument or that reads from process.env and then you load the right .env file for each build.

"scripts": {
    "build": "dotenv-cli -e .env.production node build.js",
    "build:staging": "dotenv-cli -e .env.staging node build.js",
    "build:dev": "dotenv-cli -e .env.dev node build.js",
}

Here, build.js would be a custom JS file that you wrote and it would build your app.

Keep in mind that you'd need to output the built files in a way that they don't overwrite each other as you build for different environments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.