Skip to content

Commit b16f145

Browse files
committed
refactor: Replacing babel-esm-plugin with optimize-plugin
1 parent bd54130 commit b16f145

File tree

4 files changed

+372
-67
lines changed

4 files changed

+372
-67
lines changed

packages/cli/lib/lib/webpack/webpack-client-config.js

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ const CrittersPlugin = require('critters-webpack-plugin');
1212
const renderHTMLPlugin = require('./render-html-plugin');
1313
const PushManifestPlugin = require('./push-manifest');
1414
const baseConfig = require('./webpack-base-config');
15-
//const BabelEsmPlugin = require('babel-esm-plugin');
1615
const { InjectManifest } = require('workbox-webpack-plugin');
1716
const CompressionPlugin = require('compression-webpack-plugin');
1817
const RefreshPlugin = require('@prefresh/webpack');
1918
const { normalizePath, warn } = require('../../util');
19+
const OptimizePlugin = require('optimize-plugin');
2020

2121
const 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
/200\.html$/,
56-
/\.esm.js$/,
55+
/(?<!legacy).js$/,
5756
/\.css$/,
5857
/\.(png|jpg|svg|gif|webp)$/,
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+
/200\.html$/,
73+
/\.legacy.js$/,
74+
/\.css$/,
75+
/\.(png|jpg|svg|gif|webp)$/,
76+
],
77+
})
78+
);
6779
}
68-
69-
swInjectManifest.push(
70-
new InjectManifest({
71-
swSrc: swPath,
72-
include: [/200\.html$/, /\.js$/, /\.css$/, /\.(png|jpg|svg|gif|webp)$/],
73-
exclude: [/\.esm\.js$/],
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: [/^sw.*\.js/],
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-z0-9]{5}((\.legacy)?\.(?:js|css)$)/i, '.*****$1'),
188+
}),
214189
],
215190
cache: true,
216191

217192
optimization: {
218193
minimizer: [
219194
new TerserPlugin({
220195
parallel: true,
196+
test: config.esm ? /sw.*.js$/ : /\.m?js(\?.*)?$/i,
221197
terserOptions: {
222198
output: { comments: false },
223199
mangle: true,

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"mini-css-extract-plugin": "^2.5.3",
113113
"minimatch": "^3.0.3",
114114
"native-url": "0.3.4",
115+
"optimize-plugin": "^1.1.0",
115116
"ora": "^5.4.0",
116117
"postcss": "^8.2.10",
117118
"postcss-load-config": "^3.0.1",

packages/cli/tests/images/build.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const common = {
2-
'polyfills.ddfab.js': 5656,
3-
'polyfills.ddfab.js.map': 18693,
2+
'polyfills.d63a3.js': 5210,
3+
'polyfills.d63a3.js.map': 18665,
44
};
55

66
exports.default = Object.assign({}, common, {
@@ -13,23 +13,23 @@ exports.default = Object.assign({}, common, {
1313
'assets/favicon.ico': 15086,
1414
'ssr-build/ssr-bundle.77c49.css': 1281,
1515
'ssr-build/ssr-bundle.77c49.css.map': 2070,
16-
'ssr-build/ssr-bundle.js': 11090,
16+
'ssr-build/ssr-bundle.js': 9769,
1717
'ssr-build/ssr-bundle.js.map': 30625,
18-
'bundle.32925.js': 21429,
19-
'bundle.32925.js.map': 111801,
18+
'bundle.20e9c.js': 21323,
19+
'bundle.20e9c.js.map': 114522,
2020
'bundle.9bde9.css': 945,
2121
'bundle.9bde9.css.map': 1758,
2222
'favicon.ico': 15086,
23-
'index.html': 2034,
23+
'index.html': 2154,
2424
'manifest.json': 455,
2525
'preact_prerender_data.json': 11,
2626
'push-manifest.json': 812,
27-
'route-home.chunk.72205.js': 327,
28-
'route-home.chunk.72205.js.map': 483,
27+
'route-home.chunk.4a013.js': 306,
28+
'route-home.chunk.4a013.js.map': 483,
2929
'route-home.chunk.f1c94.css': 112,
3030
'route-home.chunk.f1c94.css.map': 224,
31-
'route-profile.chunk.abefe.js': 3514,
32-
'route-profile.chunk.abefe.js.map': 15454,
31+
'route-profile.chunk.efe1e.js': 2437,
32+
'route-profile.chunk.efe1e.js.map': 15454,
3333
'route-profile.chunk.e0d39.css': 118,
3434
'route-profile.chunk.e0d39.css.map': 231,
3535
});

0 commit comments

Comments
 (0)