Skip to content

Commit ad4c053

Browse files
committed
Hard source environmentHash allowing for standalone BRS install optimisation, definePlugin typeof window
1 parent ed4062e commit ad4c053

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
const reactScriptsRoot = path.resolve(__dirname, '..');
7+
const haveIsolatedDependencies =
8+
fs.existsSync(path.join(reactScriptsRoot, 'package-lock.json')) ||
9+
fs.existsSync(path.join(reactScriptsRoot, 'yarn.lock'));
10+
11+
module.exports = {
12+
root: haveIsolatedDependencies ? reactScriptsRoot : process.cwd(),
13+
directories: [],
14+
files: ['package-lock.json', 'yarn.lock'],
15+
};

packages/react-scripts/config/webpack.config.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const shouldInlineRuntimeChunk = process.env.INLINE_RUNTIME_CHUNK !== 'false';
6060
// We might not want to use the hard source plugin on environments that won't persist the cache for later
6161
const useHardSourceWebpackPlugin =
6262
process.env.USE_HARD_SOURCE_WEBPACK_PLUGIN === 'true';
63+
const environmentHash = require('./environmentHash');
6364

6465
// Check if TypeScript is setup
6566
const useTypeScript = fs.existsSync(paths.appTsConfig);
@@ -610,7 +611,8 @@ module.exports = function(webpackEnv) {
610611
],
611612
},
612613
plugins: [
613-
useHardSourceWebpackPlugin && new HardSourceWebpackPlugin(),
614+
useHardSourceWebpackPlugin &&
615+
new HardSourceWebpackPlugin({ environmentHash }),
614616
useHardSourceWebpackPlugin &&
615617
new HardSourceWebpackPlugin.ExcludeModulePlugin([
616618
{
@@ -689,7 +691,10 @@ module.exports = function(webpackEnv) {
689691
// It is absolutely essential that NODE_ENV is set to production
690692
// during a production build.
691693
// Otherwise React will be compiled in the very slow development mode.
692-
new webpack.DefinePlugin(env.stringified),
694+
new webpack.DefinePlugin({
695+
...env.stringified,
696+
'typeof window': '"object"',
697+
}),
693698
// This is necessary to emit hot updates (currently CSS only):
694699
isEnvDevelopment && new webpack.HotModuleReplacementPlugin(),
695700
// Watcher doesn't work well if you mistype casing in a path so we use

packages/react-scripts/config/webpack.config.ssr.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const PnpWebpackPlugin = require('pnp-webpack-plugin');
1717
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
1818
// const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
1919
// const TerserPlugin = require('terser-webpack-plugin');
20-
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
20+
// const MiniCssExtractPlugin = require('mini-css-extract-plugin');
2121
// const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
2222
// const safePostCssParser = require('postcss-safe-parser');
2323
// const ManifestPlugin = require('webpack-manifest-plugin');
@@ -59,6 +59,7 @@ const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
5959
// We might not want to use the hard source plugin on environments that won't persist the cache for later
6060
const useHardSourceWebpackPlugin =
6161
process.env.USE_HARD_SOURCE_WEBPACK_PLUGIN === 'true';
62+
const environmentHash = require('./environmentHash');
6263

6364
// Check if TypeScript is setup
6465
const useTypeScript = fs.existsSync(paths.appTsConfig);
@@ -87,7 +88,7 @@ module.exports = function(webpackEnv) {
8788
: isEnvDevelopment && '/';
8889
// Some apps do not use client-side routing with pushState.
8990
// For these, "homepage" can be set to "." to enable relative asset paths.
90-
const shouldUseRelativeAssetPaths = publicPath === './';
91+
// const shouldUseRelativeAssetPaths = publicPath === './';
9192

9293
// `publicUrl` is just like `publicPath`, but we will provide it to our app
9394
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
@@ -616,7 +617,8 @@ module.exports = function(webpackEnv) {
616617
},
617618
plugins: [
618619
new webpack.IgnorePlugin(/\/iconv-loader$/), // https://github.com/webpack/webpack/issues/3078#issuecomment-400697407
619-
useHardSourceWebpackPlugin && new HardSourceWebpackPlugin(),
620+
useHardSourceWebpackPlugin &&
621+
new HardSourceWebpackPlugin({ environmentHash }),
620622
useHardSourceWebpackPlugin &&
621623
new HardSourceWebpackPlugin.ExcludeModulePlugin([
622624
{
@@ -675,7 +677,10 @@ module.exports = function(webpackEnv) {
675677
// It is absolutely essential that NODE_ENV is set to production
676678
// during a production build.
677679
// Otherwise React will be compiled in the very slow development mode.
678-
new webpack.DefinePlugin(env.stringified),
680+
new webpack.DefinePlugin({
681+
...env.stringified,
682+
'typeof window': '"undefined"',
683+
}),
679684
// This is necessary to emit hot updates (currently CSS only):
680685
// isEnvDevelopment && new webpack.HotModuleReplacementPlugin(),
681686
// Watcher doesn't work well if you mistype casing in a path so we use

0 commit comments

Comments
 (0)