Skip to content

Commit e977ce2

Browse files
committed
Changes process.env build flags to be inline with expected string type of typical process.env member. closes mui#395
1 parent 7c5338b commit e977ce2

File tree

10 files changed

+47
-45
lines changed

10 files changed

+47
-45
lines changed

client/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,11 @@ renderApp(DemoApp);
5454
require('./registerServiceWorker');
5555

5656
// The following is needed so that we can support hot reloading our application.
57-
if (process.env.BUILD_FLAG_IS_DEV && module.hot) {
57+
if (process.env.BUILD_FLAG_IS_DEV === 'true' && module.hot) {
5858
// Accept changes to this file for hot reloading.
5959
module.hot.accept('./index.js');
6060
// Any changes to our App will cause a hotload re-render.
61-
module.hot.accept(
62-
'../shared/components/DemoApp',
63-
() => {
64-
renderApp(require('../shared/components/DemoApp').default);
65-
},
66-
);
61+
module.hot.accept('../shared/components/DemoApp', () => {
62+
renderApp(require('../shared/components/DemoApp').default);
63+
});
6764
}

client/registerServiceWorker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111
import config from '../config';
1212

13-
if (!process.env.BUILD_FLAG_IS_DEV) {
13+
if (process.env.BUILD_FLAG_IS_DEV === 'false') {
1414
// We check the shared config, ensuring that the service worker has been
1515
// enabled.
1616
if (config('serviceWorker.enabled')) {
17+
// eslint-disable-next-line global-require
1718
const OfflinePluginRuntime = require('offline-plugin/runtime');
1819

1920
// Install the offline plugin, which instantiates our service worker and app

config/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,18 @@ function resolveConfigForBrowserOrServer() {
3434
// will be removed when "process.env.BUILD_FLAG_IS_NODE === true".
3535
// If no "BUILD_FLAG_IS_NODE" env var is undefined we can assume that we are running outside
3636
// of a webpack run, and will therefore return the config file.
37-
if (typeof process.env.BUILD_FLAG_IS_NODE === 'undefined' || process.env.BUILD_FLAG_IS_NODE) {
37+
if (
38+
typeof process.env.BUILD_FLAG_IS_NODE === 'undefined' ||
39+
process.env.BUILD_FLAG_IS_NODE === 'true'
40+
) {
3841
// i.e. running in our server/node process.
3942
configCache = require('./values').default;
4043
return configCache;
4144
}
4245

4346
// To get here we are likely running in the browser.
4447

45-
if (typeof window !== 'undefined'
46-
&& typeof window.__CLIENT_CONFIG__ === 'object') {
48+
if (typeof window !== 'undefined' && typeof window.__CLIENT_CONFIG__ === 'object') {
4749
configCache = window.__CLIENT_CONFIG__;
4850
} else {
4951
// To get here we must be running in the browser.
@@ -87,20 +89,22 @@ function resolveConfigForBrowserOrServer() {
8789
* could not be found at the given path.
8890
*/
8991
export default function configGet(path) {
90-
const parts = typeof path === 'string'
91-
? path.split('.')
92-
: path;
92+
const parts = typeof path === 'string' ? path.split('.') : path;
9393

9494
if (parts.length === 0) {
95-
throw new Error('You must provide the path to the configuration value you would like to consume.');
95+
throw new Error(
96+
'You must provide the path to the configuration value you would like to consume.',
97+
);
9698
}
9799
let result = resolveConfigForBrowserOrServer();
98100
for (let i = 0; i < parts.length; i += 1) {
99101
if (result === undefined) {
100102
const errorMessage = `Failed to resolve configuration value at "${parts.join('.')}".`;
101103
// This "if" block gets stripped away by webpack for production builds.
102-
if (process.env.BUILD_FLAG_IS_DEV && process.env.BUILD_FLAG_IS_CLIENT) {
103-
throw new Error(`${errorMessage} We have noticed that you are trying to access this configuration value from the client bundle (i.e. code that will be executed in a browser). For configuration values to be exposed to the client bundle you must ensure that the path is added to the client configuration filter in the project configuration values file.`);
104+
if (process.env.BUILD_FLAG_IS_DEV === 'true' && process.env.BUILD_FLAG_IS_CLIENT === 'true') {
105+
throw new Error(
106+
`${errorMessage} We have noticed that you are trying to access this configuration value from the client bundle (i.e. code that will be executed in a browser). For configuration values to be exposed to the client bundle you must ensure that the path is added to the client configuration filter in the project configuration values file.`,
107+
);
104108
}
105109
throw new Error(errorMessage);
106110
}

config/values.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ const values = {
8383
childSrc: [],
8484
connectSrc: [],
8585
defaultSrc: [],
86-
fontSrc: [
87-
'https://fonts.googleapis.com/css',
88-
'https://fonts.gstatic.com',
89-
],
86+
fontSrc: ['https://fonts.googleapis.com/css', 'https://fonts.gstatic.com'],
9087
imgSrc: [],
9188
mediaSrc: [],
9289
manifestSrc: [],
@@ -232,11 +229,7 @@ const values = {
232229
srcEntryFile: './server/index.js',
233230

234231
// Src paths.
235-
srcPaths: [
236-
'./server',
237-
'./shared',
238-
'./config',
239-
],
232+
srcPaths: ['./server', './shared', './config'],
240233

241234
// Where does the server bundle output live?
242235
outputPath: './build/server',
@@ -330,8 +323,10 @@ const values = {
330323

331324
// This protects us from accidentally including this configuration in our
332325
// client bundle. That would be a big NO NO to do. :)
333-
if (process.env.BUILD_FLAG_IS_CLIENT) {
334-
throw new Error("You shouldn't be importing the `<projectroot>/config/values.js` directly into code that will be included in your 'client' bundle as the configuration object will be sent to user's browsers. This could be a security risk! Instead, use the `config` helper function located at `<projectroot>/config/index.js`.");
326+
if (process.env.BUILD_FLAG_IS_CLIENT === 'true') {
327+
throw new Error(
328+
"You shouldn't be importing the `<projectroot>/config/values.js` directly into code that will be included in your 'client' bundle as the configuration object will be sent to user's browsers. This could be a security risk! Instead, use the `config` helper function located at `<projectroot>/config/index.js`.",
329+
);
335330
}
336331

337332
export default values;

internal/webpack/configFactory.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,14 @@ export default function webpackConfigFactory(buildOptions) {
227227
// the associated values below.
228228
//
229229
// For example you may have the following in your code:
230-
// if (process.env.BUILD_FLAG_IS_CLIENT === true) {
230+
// if (process.env.BUILD_FLAG_IS_CLIENT === 'true') {
231231
// console.log('Foo');
232232
// }
233233
//
234234
// If the BUILD_FLAG_IS_CLIENT was assigned a value of `false` the above
235235
// code would be converted to the following by the webpack bundling
236236
// process:
237-
// if (false === true) {
237+
// if ('false' === 'true') {
238238
// console.log('Foo');
239239
// }
240240
//
@@ -243,18 +243,22 @@ export default function webpackConfigFactory(buildOptions) {
243243
// final output. This is helpful for extreme cases where you want to
244244
// ensure that code is only included/executed on specific targets, or for
245245
// doing debugging.
246+
//
247+
// NOTE: We are stringifying the values to keep them in line with the
248+
// expected type of a typical process.env member (i.e. string).
249+
// @see https://github.com/ctrlplusb/react-universally/issues/395
246250
new webpack.EnvironmentPlugin({
247251
// It is really important to use NODE_ENV=production in order to use
248252
// optimised versions of some node_modules, such as React.
249253
NODE_ENV: isProd ? 'production' : 'development',
250254
// Is this the "client" bundle?
251-
BUILD_FLAG_IS_CLIENT: isClient,
255+
BUILD_FLAG_IS_CLIENT: JSON.stringify(isClient),
252256
// Is this the "server" bundle?
253-
BUILD_FLAG_IS_SERVER: isServer,
257+
BUILD_FLAG_IS_SERVER: JSON.stringify(isServer),
254258
// Is this a node bundle?
255-
BUILD_FLAG_IS_NODE: isNode,
259+
BUILD_FLAG_IS_NODE: JSON.stringify(isNode),
256260
// Is this a development build?
257-
BUILD_FLAG_IS_DEV: isDev,
261+
BUILD_FLAG_IS_DEV: JSON.stringify(isDev),
258262
}),
259263

260264
// Generates a JSON file containing a map of all the output files for

server/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ app.use(compression());
2727
// Register our service worker generated by our webpack config.
2828
// We do not want the service worker registered for development builds, and
2929
// additionally only want it registered if the config allows.
30-
if (!process.env.BUILD_FLAG_IS_DEV && config('serviceWorker.enabled')) {
30+
if (process.env.BUILD_FLAG_IS_DEV === 'false' && config('serviceWorker.enabled')) {
3131
app.get(`/${config('serviceWorker.fileName')}`, serviceWorker);
3232
app.get(
3333
`${config('bundles.client.webPath')}${config('serviceWorker.offlinePageFileName')}`,

server/middleware/reactApplication/ServerHTML.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ function ServerHTML(props) {
8383
// generate a vendor DLL in order to dramatically reduce our
8484
// compilation times. Therefore we need to inject the path to the
8585
// vendor dll bundle below.
86-
ifElse(process.env.BUILD_FLAG_IS_DEV && config('bundles.client.devVendorDLL.enabled'))(() =>
86+
ifElse(
87+
process.env.BUILD_FLAG_IS_DEV === 'true' && config('bundles.client.devVendorDLL.enabled'),
88+
)(() =>
8789
scriptTag(
8890
`${config('bundles.client.webPath')}${config('bundles.client.devVendorDLL.name')}.js?t=${Date.now()}`,
8991
)),

server/middleware/reactApplication/getClientBundleEntryAssets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default function getClientBundleEntryAssets() {
2626
// Return the assets json cache if it exists.
2727
// In development mode we always read the assets json file from disk to avoid
2828
// any cases where an older version gets cached.
29-
if (!process.env.BUILD_FLAG_IS_DEV && resultCache) {
29+
if (process.env.BUILD_FLAG_IS_DEV === 'false' && resultCache) {
3030
return resultCache;
3131
}
3232

server/middleware/reactApplication/index.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import React from 'react';
32
import Helmet from 'react-helmet';
43
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
@@ -25,7 +24,7 @@ export default function reactApplicationMiddleware(request, response) {
2524
// It's possible to disable SSR, which can be useful in development mode.
2625
// In this case traditional client side only rendering will occur.
2726
if (config('disableSSR')) {
28-
if (process.env.BUILD_FLAG_IS_DEV) {
27+
if (process.env.BUILD_FLAG_IS_DEV === 'true') {
2928
// eslint-disable-next-line no-console
3029
console.log('==> Handling react route without SSR');
3130
}
@@ -78,11 +77,11 @@ export default function reactApplicationMiddleware(request, response) {
7877
response
7978
.status(
8079
reactRouterContext.missed
81-
// If the renderResult contains a "missed" match then we set a 404 code.
82-
// Our App component will handle the rendering of an Error404 view.
83-
? 404
84-
// Otherwise everything is all good and we send a 200 OK status.
85-
: 200,
80+
? // If the renderResult contains a "missed" match then we set a 404 code.
81+
// Our App component will handle the rendering of an Error404 view.
82+
404
83+
: // Otherwise everything is all good and we send a 200 OK status.
84+
200,
8685
)
8786
.send(`<!DOCTYPE html>${html}`);
8887
});

server/middleware/security.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Object.keys(cspExtensions).forEach((key) => {
5555
}
5656
});
5757

58-
if (process.env.BUILD_FLAG_IS_DEV) {
58+
if (process.env.BUILD_FLAG_IS_DEV === 'true') {
5959
// When in development mode we need to add our secondary express server that
6060
// is used to host our client bundle to our csp config.
6161
Object.keys(cspConfig.directives).forEach((directive) => {

0 commit comments

Comments
 (0)