Last active
February 28, 2019 05:10
-
-
Save r17x/cc48929a16865849a3ac20aaa124ef08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @description | |
| * this script for replace environment variable from public directory | |
| * of Create-React-App. | |
| * | |
| * Problem | |
| * create-react-app can't work with ENV_VAR for all javascript files | |
| * in public directory | |
| * on running build script. | |
| * | |
| * Solve | |
| * this little script for change all env var in the public directory | |
| * @author ri7nz <[email protected]> | |
| * | |
| */ | |
| const dotEnv = require('dotenv') | |
| const replacer = require('replace-in-files') | |
| const { NODE_ENV: ENV } = process.env | |
| if (ENV) { | |
| const parseEnv = dotEnv.config('.env' + ENV !== '' ? '.' + ENV : '').parsed | |
| Object.keys(parseEnv) | |
| .filter(env => env.startsWith('REACT_APP')) | |
| .forEach(env => { | |
| const replacerOptions = { | |
| // after build, all of javascript file on public directory | |
| // is copied in build directory | |
| files: 'build/*.js', | |
| from: new RegExp(env, 'g'), | |
| to: parseEnv[env], | |
| } | |
| const onReplace = e => { | |
| const { changedFiles } = e | |
| console.log('Modified Files:', changedFiles) | |
| console.log('From env name:' + env + ' With Value:' + parseEnv[env]) | |
| } | |
| replacer(replacerOptions).then(onReplace) | |
| }) | |
| } else { | |
| console.warn( | |
| 'Please use NODE_ENV=your_env_state\n' + | |
| '$ NODE_ENV=development node me.js', | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment