Comments (8)
Here is what we tried to rewire process.env
const findWebpackPlugin = (plugins, pluginName) =>
plugins.find(plugin => plugin.constructor.name === pluginName);
const overrideProcessEnv = value => config => {
const plugin = findWebpackPlugin(config.plugins, 'DefinePlugin');
const processEnv = plugin.definitions['process.env'] || {};
plugin.definitions['process.env'] = {
...processEnv,
...value,
};
return config;
};
module.exports = override(
overrideProcessEnv({
VERSION: JSON.stringify(require('./package.json').version),
})
);
from customize-cra.
Just wondering, does the variables set into the .env
get consumed by webpack as well? If so maybe we can leverage dotenv to inject the variables in a clean way
from customize-cra.
dotenv in CRA requires environment variables starting with REACT_APP_
except NODE_ENV
. That is why we want directly access DefinePlugin
.
from customize-cra.
@aaronchenwei
I would like to be able to use DefinePlugin to define DEV and PROD variables. Any idea how I should try to achieve that ? I tried your solution but it does not work.
from customize-cra.
Do you mean ?
proccess.env.DEV = {...}
process.env.PROD = {...}
My previous code snippet is just to append variables under process.env
from customize-cra.
Here is package.json/scritps config:
"scripts": {
"dev": "react-app-rewired start --open",
"build:test": "react-app-rewired build --test",
"build:pre": "react-app-rewired build --pre",
"build:prod": "react-app-rewired build --prod",
"eject": "react-scripts eject"
}
const path = require('path')
const path = require('path')
const Dotenv = require('dotenv-webpack')
const version = JSON.stringify(require('../package.json').version)
const resolvePath = dir => path.join(__dirname, '..', dir)
const mode = process.argv[2].split('--')[1]
const isDev = process.env.NODE_ENV === 'development'
const envPath = isDev ? resolvePath('.env.development') : resolvePath(`.env.${mode}`)
const envVars = {}
const definitions = new Dotenv({
path: envPath
}).definitions
Object.keys(definitions).forEach(key => {
const [, api] = key.split('process.env.')
envVars[api] = definitions[key]
})
function rewireDefinePlugin (config) {
config.plugins.some(p => {
if (
p.definitions &&
p.definitions['process.env']
) {
p.definitions['process.env'] = { ...envVars, version}
return true
}
})
return config
}
module.exports = rewireDefinePlugin
from customize-cra.
Closing as the solution from @aaronchenwei is verified as working with [email protected]
+. If there are further problems, please open a new issue.
from customize-cra.
https://github.com/foxundermoon/cra-define-override#readme
npm i -D cra-define-override
// config-overrides.js
const { overridePassedProcessEnv } = require("cra-define-override");
module.exports = override(
overridePassedProcessEnv(["BASE_URL", "OTHER_ENV_NAME"])
);
then
BASE_URL=https://fox.mn npm run build
from customize-cra.
Related Issues (20)
- __webpack_require__(...) is not a function
- CRA v4 switch from GenerateSW to InjectManifest not supported - workaround
- Is this project unmaintained? anybody here? HOT 1
- Example of "addWebpackExternals" has a error HOT 1
- 啥时候更新webpack5
- how can I prevent build/service-worker.js from being generated. I don't want any caching. please help, urgent
- add graphql loader
- create-react-app 强制更新到了 5.0.0 ,启用的是 webpack5,在配置 less 的时候出现错误 postcss Loader has been initialized using an options object that does not match the API schema,影响项目开展,望修复 HOT 12
- Incompatible with Create-React-App 5.0: Invalid Options for PostCSS Loader HOT 8
- PostCSS plugin autoprefixer requires PostCSS 8.
- options has an unknown property 'plugins' HOT 1
- useEslintRc ERROR!! HOT 1
- addWebpackExternals: react is not defined HOT 2
- Using customizers for jest configuration HOT 3
- addPostcssPlugins doesn't work when react-scripts's version higher than or equal to 5.0.0 HOT 5
- setWebpackPublicPath auto option HOT 1
- Workbox strategies uncaught no-response
- TypeError: addWebpackPlugin is not a function
- react项目:在configoveride.js中addPostcssPlugins添加postcss-pxtorem无效 HOT 7
- getBabelLoader().options is undefined when you fix babel-loader
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from customize-cra.