Skip to content

Commit ed4062e

Browse files
committed
Make hard source plugin switchable
1 parent 722bc62 commit ed4062e

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
5757
// Some apps do not need the benefits of saving a web request, so not inlining the chunk
5858
// makes for a smoother build process.
5959
const shouldInlineRuntimeChunk = process.env.INLINE_RUNTIME_CHUNK !== 'false';
60+
// We might not want to use the hard source plugin on environments that won't persist the cache for later
61+
const useHardSourceWebpackPlugin =
62+
process.env.USE_HARD_SOURCE_WEBPACK_PLUGIN === 'true';
6063

6164
// Check if TypeScript is setup
6265
const useTypeScript = fs.existsSync(paths.appTsConfig);
@@ -283,11 +286,13 @@ module.exports = function(webpackEnv) {
283286
? {
284287
chunks: 'all',
285288
name: false,
286-
cacheGroups: bpkReactScriptsConfig.vendorsChunkRegex ? {
287-
vendors: {
288-
test: new RegExp(bpkReactScriptsConfig.vendorsChunkRegex),
289-
},
290-
} : {},
289+
cacheGroups: bpkReactScriptsConfig.vendorsChunkRegex
290+
? {
291+
vendors: {
292+
test: new RegExp(bpkReactScriptsConfig.vendorsChunkRegex),
293+
},
294+
}
295+
: {},
291296
}
292297
: {},
293298
// Keep the runtime chunk seperated to enable long term caching
@@ -605,17 +610,18 @@ module.exports = function(webpackEnv) {
605610
],
606611
},
607612
plugins: [
608-
new HardSourceWebpackPlugin(),
609-
new HardSourceWebpackPlugin.ExcludeModulePlugin([
610-
{
613+
useHardSourceWebpackPlugin && new HardSourceWebpackPlugin(),
614+
useHardSourceWebpackPlugin &&
615+
new HardSourceWebpackPlugin.ExcludeModulePlugin([
616+
{
611617
// HardSource works with mini-css-extract-plugin but due to how
612618
// mini-css emits assets, assets are not emitted on repeated builds with
613619
// mini-css and hard-source together. Ignoring the mini-css loader
614620
// modules, but not the other css loader modules, excludes the modules
615621
// that mini-css needs rebuilt to output assets every time.
616622
test: /mini-css-extract-plugin[\\/]dist[\\/]loader/,
617-
},
618-
]),
623+
},
624+
]),
619625
new LoadablePlugin(),
620626
// Generates an `index.html` file with the <script> injected.
621627
new HtmlWebpackPlugin(

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
5656
// Some apps do not need the benefits of saving a web request, so not inlining the chunk
5757
// makes for a smoother build process.
5858
// const shouldInlineRuntimeChunk = process.env.INLINE_RUNTIME_CHUNK !== 'false';
59+
// We might not want to use the hard source plugin on environments that won't persist the cache for later
60+
const useHardSourceWebpackPlugin =
61+
process.env.USE_HARD_SOURCE_WEBPACK_PLUGIN === 'true';
5962

6063
// Check if TypeScript is setup
6164
const useTypeScript = fs.existsSync(paths.appTsConfig);
@@ -613,17 +616,18 @@ module.exports = function(webpackEnv) {
613616
},
614617
plugins: [
615618
new webpack.IgnorePlugin(/\/iconv-loader$/), // https://github.com/webpack/webpack/issues/3078#issuecomment-400697407
616-
new HardSourceWebpackPlugin(),
617-
new HardSourceWebpackPlugin.ExcludeModulePlugin([
618-
{
619+
useHardSourceWebpackPlugin && new HardSourceWebpackPlugin(),
620+
useHardSourceWebpackPlugin &&
621+
new HardSourceWebpackPlugin.ExcludeModulePlugin([
622+
{
619623
// HardSource works with mini-css-extract-plugin but due to how
620624
// mini-css emits assets, assets are not emitted on repeated builds with
621625
// mini-css and hard-source together. Ignoring the mini-css loader
622626
// modules, but not the other css loader modules, excludes the modules
623627
// that mini-css needs rebuilt to output assets every time.
624628
test: /mini-css-extract-plugin[\\/]dist[\\/]loader/,
625-
},
626-
]),
629+
},
630+
]),
627631
new LoadablePlugin(),
628632
// Generates an `index.html` file with the <script> injected.
629633
// new HtmlWebpackPlugin(

0 commit comments

Comments
 (0)