mirror of
https://github.com/zhigang1992/create-react-app.git
synced 2026-04-30 10:22:33 +08:00
Make all react app vars accessible in index.html (#1440)
* Make all vars accessiable in index.html * Fix wrong env provieded to DefinePlugin * Separate results from getClientEnvironment * The `string` should be object instead of string * Fix accessing wrong field * Changed variables naming to `raw` and `stringified` * Remove trailing commas
This commit is contained in:
22
packages/react-scripts/config/env.js
vendored
22
packages/react-scripts/config/env.js
vendored
@@ -15,25 +15,33 @@
|
|||||||
var REACT_APP = /^REACT_APP_/i;
|
var REACT_APP = /^REACT_APP_/i;
|
||||||
|
|
||||||
function getClientEnvironment(publicUrl) {
|
function getClientEnvironment(publicUrl) {
|
||||||
var processEnv = Object
|
var raw = Object
|
||||||
.keys(process.env)
|
.keys(process.env)
|
||||||
.filter(key => REACT_APP.test(key))
|
.filter(key => REACT_APP.test(key))
|
||||||
.reduce((env, key) => {
|
.reduce((env, key) => {
|
||||||
env[key] = JSON.stringify(process.env[key]);
|
env[key] = process.env[key];
|
||||||
return env;
|
return env;
|
||||||
}, {
|
}, {
|
||||||
// Useful for determining whether we’re running in production mode.
|
// Useful for determining whether we’re running in production mode.
|
||||||
// Most importantly, it switches React into the correct mode.
|
// Most importantly, it switches React into the correct mode.
|
||||||
'NODE_ENV': JSON.stringify(
|
'NODE_ENV': process.env.NODE_ENV || 'development',
|
||||||
process.env.NODE_ENV || 'development'
|
|
||||||
),
|
|
||||||
// Useful for resolving the correct path to static assets in `public`.
|
// Useful for resolving the correct path to static assets in `public`.
|
||||||
// For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
|
// For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
|
||||||
// This should only be used as an escape hatch. Normally you would put
|
// This should only be used as an escape hatch. Normally you would put
|
||||||
// images into the `src` and `import` them in code to get their paths.
|
// images into the `src` and `import` them in code to get their paths.
|
||||||
'PUBLIC_URL': JSON.stringify(publicUrl)
|
'PUBLIC_URL': publicUrl
|
||||||
});
|
});
|
||||||
return {'process.env': processEnv};
|
// Stringify all values so we can feed into Webpack DefinePlugin
|
||||||
|
var stringified = {
|
||||||
|
'process.env': Object
|
||||||
|
.keys(raw)
|
||||||
|
.reduce((env, key) => {
|
||||||
|
env[key] = JSON.stringify(raw[key]);
|
||||||
|
return env;
|
||||||
|
}, {})
|
||||||
|
};
|
||||||
|
|
||||||
|
return { raw, stringified };
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = getClientEnvironment;
|
module.exports = getClientEnvironment;
|
||||||
|
|||||||
@@ -198,12 +198,11 @@ module.exports = {
|
|||||||
];
|
];
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
// Makes the public URL available as %PUBLIC_URL% in index.html, e.g.:
|
// Makes some environment variables available in index.html.
|
||||||
|
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
|
||||||
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
|
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
|
||||||
// In development, this will be an empty string.
|
// In development, this will be an empty string.
|
||||||
new InterpolateHtmlPlugin({
|
new InterpolateHtmlPlugin(env.raw),
|
||||||
PUBLIC_URL: publicUrl
|
|
||||||
}),
|
|
||||||
// Generates an `index.html` file with the <script> injected.
|
// Generates an `index.html` file with the <script> injected.
|
||||||
new HtmlWebpackPlugin({
|
new HtmlWebpackPlugin({
|
||||||
inject: true,
|
inject: true,
|
||||||
@@ -211,7 +210,7 @@ module.exports = {
|
|||||||
}),
|
}),
|
||||||
// Makes some environment variables available to the JS code, for example:
|
// Makes some environment variables available to the JS code, for example:
|
||||||
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
|
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
|
||||||
new webpack.DefinePlugin(env),
|
new webpack.DefinePlugin(env.stringified),
|
||||||
// This is necessary to emit hot updates (currently CSS only):
|
// This is necessary to emit hot updates (currently CSS only):
|
||||||
new webpack.HotModuleReplacementPlugin(),
|
new webpack.HotModuleReplacementPlugin(),
|
||||||
// Watcher doesn't work well if you mistype casing in a path so we use
|
// Watcher doesn't work well if you mistype casing in a path so we use
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ var env = getClientEnvironment(publicUrl);
|
|||||||
|
|
||||||
// Assert this just to be safe.
|
// Assert this just to be safe.
|
||||||
// Development builds of React are slow and not intended for production.
|
// Development builds of React are slow and not intended for production.
|
||||||
if (env['process.env'].NODE_ENV !== '"production"') {
|
if (env.stringified['process.env'].NODE_ENV !== '"production"') {
|
||||||
throw new Error('Production builds must have NODE_ENV=production.');
|
throw new Error('Production builds must have NODE_ENV=production.');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,13 +212,12 @@ module.exports = {
|
|||||||
];
|
];
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
// Makes the public URL available as %PUBLIC_URL% in index.html, e.g.:
|
// Makes some environment variables available in index.html.
|
||||||
|
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
|
||||||
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
|
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
|
||||||
// In production, it will be an empty string unless you specify "homepage"
|
// In production, it will be an empty string unless you specify "homepage"
|
||||||
// in `package.json`, in which case it will be the pathname of that URL.
|
// in `package.json`, in which case it will be the pathname of that URL.
|
||||||
new InterpolateHtmlPlugin({
|
new InterpolateHtmlPlugin(env.raw),
|
||||||
PUBLIC_URL: publicUrl
|
|
||||||
}),
|
|
||||||
// Generates an `index.html` file with the <script> injected.
|
// Generates an `index.html` file with the <script> injected.
|
||||||
new HtmlWebpackPlugin({
|
new HtmlWebpackPlugin({
|
||||||
inject: true,
|
inject: true,
|
||||||
@@ -240,7 +239,7 @@ module.exports = {
|
|||||||
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
|
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
|
||||||
// It is absolutely essential that NODE_ENV was set to production here.
|
// It is absolutely essential that NODE_ENV was set to production here.
|
||||||
// Otherwise React will be compiled in the very slow development mode.
|
// Otherwise React will be compiled in the very slow development mode.
|
||||||
new webpack.DefinePlugin(env),
|
new webpack.DefinePlugin(env.stringified),
|
||||||
// This helps ensure the builds are consistent if source hasn't changed:
|
// This helps ensure the builds are consistent if source hasn't changed:
|
||||||
new webpack.optimize.OccurrenceOrderPlugin(),
|
new webpack.optimize.OccurrenceOrderPlugin(),
|
||||||
// Try to dedupe duplicated modules, if any:
|
// Try to dedupe duplicated modules, if any:
|
||||||
|
|||||||
Reference in New Issue
Block a user