@@ -12,11 +12,11 @@ const CrittersPlugin = require('critters-webpack-plugin');
1212const renderHTMLPlugin = require ( './render-html-plugin' ) ;
1313const PushManifestPlugin = require ( './push-manifest' ) ;
1414const baseConfig = require ( './webpack-base-config' ) ;
15- //const BabelEsmPlugin = require('babel-esm-plugin');
1615const { InjectManifest } = require ( 'workbox-webpack-plugin' ) ;
1716const CompressionPlugin = require ( 'compression-webpack-plugin' ) ;
1817const RefreshPlugin = require ( '@prefresh/webpack' ) ;
1918const { normalizePath, warn } = require ( '../../util' ) ;
19+ const OptimizePlugin = require ( 'optimize-plugin' ) ;
2020
2121const cleanFilename = name =>
2222 name . replace (
@@ -50,10 +50,9 @@ async function clientConfig(env) {
5050 swInjectManifest . push (
5151 new InjectManifest ( {
5252 swSrc : swPath ,
53- swDest : 'sw-esm.js' ,
5453 include : [
5554 / 2 0 0 \. h t m l $ / ,
56- / \. e s m .j s $ / ,
55+ / (?< ! l e g a c y ) .j s $ / ,
5756 / \. c s s $ / ,
5857 / \. ( p n g | j p g | s v g | g i f | w e b p ) $ / ,
5958 ] ,
@@ -64,15 +63,20 @@ async function clientConfig(env) {
6463 ] ,
6564 } )
6665 ) ;
66+ } else {
67+ swInjectManifest . push (
68+ new InjectManifest ( {
69+ swSrc : swPath ,
70+ swDest : env . esm ? 'sw-legacy.js' : 'sw.js' ,
71+ include : [
72+ / 2 0 0 \. h t m l $ / ,
73+ / \. l e g a c y .j s $ / ,
74+ / \. c s s $ / ,
75+ / \. ( p n g | j p g | s v g | g i f | w e b p ) $ / ,
76+ ] ,
77+ } )
78+ ) ;
6779 }
68-
69- swInjectManifest . push (
70- new InjectManifest ( {
71- swSrc : swPath ,
72- include : [ / 2 0 0 \. h t m l $ / , / \. j s $ / , / \. c s s $ / , / \. ( p n g | j p g | s v g | g i f | w e b p ) $ / ] ,
73- exclude : [ / \. e s m \. j s $ / ] ,
74- } )
75- ) ;
7680 }
7781
7882 let copyPatterns = [
@@ -142,7 +146,12 @@ async function clientConfig(env) {
142146 } ) ,
143147 new PushManifestPlugin ( env ) ,
144148 ...( await renderHTMLPlugin ( env ) ) ,
145- //...getBabelEsmPlugin(env),
149+ env . esm &&
150+ new OptimizePlugin ( {
151+ modernize : false ,
152+ minify : isProd ,
153+ exclude : [ / ^ s w .* \. j s / ] ,
154+ } ) ,
146155 copyPatterns . length !== 0 &&
147156 new CopyWebpackPlugin ( {
148157 patterns : copyPatterns ,
@@ -152,43 +161,6 @@ async function clientConfig(env) {
152161 } ;
153162}
154163
155- //function getBabelEsmPlugin(config) {
156- // const esmPlugins = [];
157- // if (config.esm) {
158- // esmPlugins.push(
159- // new BabelEsmPlugin({
160- // filename: config.isProd
161- // ? '[name].[chunkhash:5].esm.js'
162- // : '[name].esm.js',
163- // chunkFilename: '[name].chunk.[chunkhash:5].esm.js',
164- // excludedPlugins: ['BabelEsmPlugin', 'InjectManifest'],
165- // beforeStartExecution: plugins => {
166- // plugins.forEach(plugin => {
167- // if (
168- // plugin.constructor.name === 'DefinePlugin' &&
169- // plugin.definitions
170- // ) {
171- // for (const definition in plugin.definitions) {
172- // if (definition === 'process.env.ES_BUILD') {
173- // plugin.definitions[definition] = true;
174- // }
175- // }
176- // } else if (
177- // plugin.constructor.name === 'DefinePlugin' &&
178- // !plugin.definitions
179- // ) {
180- // throw new Error(
181- // 'WebpackDefinePlugin found but not `process.env.ES_BUILD`.'
182- // );
183- // }
184- // });
185- // },
186- // })
187- // );
188- // }
189- // return esmPlugins;
190- //}
191-
192164/**
193165 * @returns {import('webpack').Configuration }
194166 */
@@ -210,14 +182,18 @@ function isProd(config) {
210182 'process.env.ESM' : config . esm ,
211183 'process.env.PRERENDER' : config . prerender ,
212184 } ) ,
213- new SizePlugin ( ) ,
185+ new SizePlugin ( {
186+ stripHash : name =>
187+ name . replace ( / \. [ a - z 0 - 9 ] { 5 } ( ( \. l e g a c y ) ? \. (?: j s | c s s ) $ ) / i, '.*****$1' ) ,
188+ } ) ,
214189 ] ,
215190 cache : true ,
216191
217192 optimization : {
218193 minimizer : [
219194 new TerserPlugin ( {
220195 parallel : true ,
196+ test : config . esm ? / s w .* .j s $ / : / \. m ? j s ( \? .* ) ? $ / i,
221197 terserOptions : {
222198 output : { comments : false } ,
223199 mangle : true ,
0 commit comments