@@ -17,7 +17,7 @@ const PnpWebpackPlugin = require('pnp-webpack-plugin');
1717const 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');
@@ -39,6 +39,8 @@ const postcssNormalize = require('postcss-normalize');
3939
4040const appPackageJson = require ( paths . appPackageJson ) ;
4141
42+ const HardSourceWebpackPlugin = require ( 'hard-source-webpack-plugin' ) ;
43+ const LoadablePlugin = require ( '@loadable/webpack-plugin' ) ;
4244const sassFunctions = require ( 'bpk-mixins/sass-functions' ) ;
4345// const camelCase = require('lodash/camelCase');
4446const bpkReactScriptsConfig = appPackageJson [ 'backpack-react-scripts' ] || { } ;
@@ -54,6 +56,9 @@ const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
5456// Some apps do not need the benefits of saving a web request, so not inlining the chunk
5557// makes for a smoother build process.
5658// 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 = process . env . USE_HARD_SOURCE_WEBPACK_PLUGIN === 'true' ;
61+ const environmentHash = require ( './environmentHash' ) ;
5762
5863// const isExtendingEslintConfig = process.env.EXTEND_ESLINT === 'true';
5964
@@ -98,17 +103,17 @@ module.exports = function(webpackEnv) {
98103 preProcessorOptions = { }
99104 ) => {
100105 const loaders = [
101- isEnvDevelopment && require . resolve ( 'style-loader' ) ,
102- isEnvProduction && {
103- loader : MiniCssExtractPlugin . loader ,
104- // css is located in `static/css`, use '../../' to locate index.html folder
105- // in production `paths.publicUrlOrPath` can be a relative path
106- options : paths . publicUrlOrPath . startsWith ( '.' )
107- ? { publicPath : '../../' }
108- : { } ,
109- } ,
106+ // isEnvDevelopment && require.resolve('style-loader'),
107+ // isEnvProduction && {
108+ // loader: MiniCssExtractPlugin.loader,
109+ // // css is located in `static/css`, use '../../' to locate index.html folder
110+ // // in production `paths.publicUrlOrPath` can be a relative path
111+ // options: paths.publicUrlOrPath.startsWith('.')
112+ // ? { publicPath: '../../' }
113+ // : {},
114+ // },
110115 {
111- loader : require . resolve ( 'css-loader' ) ,
116+ loader : require . resolve ( 'css-loader/locals ' ) ,
112117 options : cssOptions ,
113118 } ,
114119 {
@@ -182,8 +187,8 @@ module.exports = function(webpackEnv) {
182187 // the line below with these two lines if you prefer the stock client:
183188 // require.resolve('webpack-dev-server/client') + '?/',
184189 // require.resolve('webpack/hot/dev-server'),
185- isEnvDevelopment &&
186- require . resolve ( 'react-dev-utils/webpackHotDevClient' ) ,
190+ // isEnvDevelopment &&
191+ // require.resolve('react-dev-utils/webpackHotDevClient'),
187192 // Finally, this is your app's code:
188193 // paths.appIndexJs,
189194 paths . appSsrJs ,
@@ -193,7 +198,7 @@ module.exports = function(webpackEnv) {
193198 ] . filter ( Boolean ) ,
194199 output : {
195200 // The build folder.
196- path : isEnvProduction ? paths . appBuild : undefined ,
201+ path : paths . appBuildSsr ,
197202 // Add /* filename */ comments to generated require()s in the output.
198203 pathinfo : isEnvDevelopment ,
199204 // There will be one main bundle, and one file per asynchronous chunk.
@@ -203,12 +208,12 @@ module.exports = function(webpackEnv) {
203208 // : isEnvDevelopment && 'static/js/bundle.js',
204209 // TODO: remove this when upgrading to webpack 5
205210 futureEmitAssets : true ,
206- filename : 'ssr.js' ,
211+ filename : 'ssr.[hash:8]. js' ,
207212 libraryTarget : 'commonjs2' ,
208213 // There are also additional JS chunk files if you use code splitting.
209214 chunkFilename : isEnvProduction
210215 ? 'static/js/[name].[contenthash:8].chunk.js'
211- : isEnvDevelopment && 'static/js/[name].chunk.js' ,
216+ : isEnvDevelopment && 'static/js/[name].[chunkhash:8]. chunk.js' ,
212217 // webpack uses `publicPath` to determine where the app is being served from.
213218 // It requires a trailing slash, or the file assets will get an incorrect path.
214219 // We inferred the "public path" (such as / or /my-project) from homepage.
@@ -351,6 +356,7 @@ module.exports = function(webpackEnv) {
351356 ] ,
352357 } ,
353358 module : {
359+ noParse : / i c o n v - l o a d e r \. j s $ / , // https://github.com/webpack/webpack/issues/3078#issuecomment-400697407
354360 strictExportPresence : true ,
355361 rules : [
356362 // Disable require.ensure as it's not a standard language feature.
@@ -446,6 +452,7 @@ module.exports = function(webpackEnv) {
446452 ) ,
447453 // @remove -on-eject-end
448454 plugins : [
455+ require . resolve ( '@loadable/babel-plugin' ) ,
449456 [
450457 require . resolve ( 'babel-plugin-named-asset-import' ) ,
451458 {
@@ -623,6 +630,20 @@ module.exports = function(webpackEnv) {
623630 ] ,
624631 } ,
625632 plugins : [
633+ useHardSourceWebpackPlugin &&
634+ new HardSourceWebpackPlugin ( { environmentHash } ) ,
635+ useHardSourceWebpackPlugin &&
636+ new HardSourceWebpackPlugin . ExcludeModulePlugin ( [
637+ {
638+ // HardSource works with mini-css-extract-plugin but due to how
639+ // mini-css emits assets, assets are not emitted on repeated builds with
640+ // mini-css and hard-source together. Ignoring the mini-css loader
641+ // modules, but not the other css loader modules, excludes the modules
642+ // that mini-css needs rebuilt to output assets every time.
643+ test : / m i n i - c s s - e x t r a c t - p l u g i n [ \\ / ] d i s t [ \\ / ] l o a d e r / ,
644+ } ,
645+ ] ) ,
646+ new LoadablePlugin ( ) ,
626647 // Generates an `index.html` file with the <script> injected.
627648 // new HtmlWebpackPlugin(
628649 // Object.assign(
@@ -689,9 +710,12 @@ module.exports = function(webpackEnv) {
689710 // It is absolutely essential that NODE_ENV is set to production
690711 // during a production build.
691712 // Otherwise React will be compiled in the very slow development mode.
692- new webpack . DefinePlugin ( env . stringified ) ,
713+ new webpack . DefinePlugin ( {
714+ ...env . stringified ,
715+ 'typeof window' : '"undefined"' ,
716+ } ) ,
693717 // This is necessary to emit hot updates (currently CSS only):
694- isEnvDevelopment && new webpack . HotModuleReplacementPlugin ( ) ,
718+ // isEnvDevelopment && new webpack.HotModuleReplacementPlugin(),
695719 // Watcher doesn't work well if you mistype casing in a path so we use
696720 // a plugin that prints an error when you attempt to do this.
697721 // See https://github.com/facebook/create-react-app/issues/240
@@ -702,14 +726,14 @@ module.exports = function(webpackEnv) {
702726 // See https://github.com/facebook/create-react-app/issues/186
703727 isEnvDevelopment &&
704728 new WatchMissingNodeModulesPlugin ( paths . appNodeModules ) ,
705- isEnvProduction &&
706- new MiniCssExtractPlugin ( {
707- // Options similar to the same options in webpackOptions.output
708- // both options are optional
709- // filename: 'static/css/[name].[contenthash:8].css',
710- filename : 'ssr.css' ,
711- // chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
712- } ) ,
729+ // isEnvProduction &&
730+ // new MiniCssExtractPlugin({
731+ // // Options similar to the same options in webpackOptions.output
732+ // // both options are optional
733+ // // filename: 'static/css/[name].[contenthash:8].css',
734+ // filename: 'ssr.css',
735+ // // chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
736+ // }),
713737 // Generate an asset manifest file with the following content:
714738 // - "files" key: Mapping of all asset filenames to their corresponding
715739 // output file so that tools can pick it up without having to parse
0 commit comments