@umijs/bundler-webpack 4.0.0-rc.14 → 4.0.0-rc.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/build.js +48 -59
  2. package/dist/cli.js +6 -15
  3. package/dist/client/client.js +34 -47
  4. package/dist/config/_sampleFeature.js +6 -17
  5. package/dist/config/assetRules.js +44 -55
  6. package/dist/config/bundleAnalyzerPlugin.js +12 -23
  7. package/dist/config/compressPlugin.js +68 -76
  8. package/dist/config/config.js +177 -185
  9. package/dist/config/copyPlugin.js +29 -40
  10. package/dist/config/cssRules.js +93 -83
  11. package/dist/config/definePlugin.js +11 -19
  12. package/dist/config/detectDeadCodePlugin.js +16 -21
  13. package/dist/config/fastRefreshPlugin.js +11 -22
  14. package/dist/config/forkTSCheckerPlugin.js +11 -22
  15. package/dist/config/harmonyLinkingErrorPlugin.js +3 -14
  16. package/dist/config/ignorePlugin.js +10 -21
  17. package/dist/config/javaScriptRules.js +151 -162
  18. package/dist/config/manifestPlugin.js +10 -18
  19. package/dist/config/miniCSSExtractPlugin.js +12 -23
  20. package/dist/config/nodePolyfill.js +14 -21
  21. package/dist/config/nodePrefixPlugin.js +8 -19
  22. package/dist/config/progressPlugin.js +7 -18
  23. package/dist/config/purgecssWebpackPlugin.js +15 -26
  24. package/dist/config/speedMeasureWebpackPlugin.js +12 -23
  25. package/dist/config/svgRules.js +43 -47
  26. package/dist/dev.js +101 -112
  27. package/dist/loader/svgr.js +4 -13
  28. package/dist/loader/swc.js +9 -14
  29. package/dist/plugins/ESBuildCSSMinifyPlugin.js +23 -34
  30. package/dist/plugins/ParcelCSSMinifyPlugin.js +30 -32
  31. package/dist/plugins/RuntimePublicPathPlugin.js +1 -1
  32. package/dist/schema.js +18 -6
  33. package/dist/server/server.d.ts +1 -1
  34. package/dist/server/server.js +161 -170
  35. package/dist/server/ws.d.ts +1 -1
  36. package/dist/types.d.ts +2 -6
  37. package/package.json +9 -11
  38. package/dist/server/https.d.ts +0 -4
  39. package/dist/server/https.js +0 -74
package/dist/build.js CHANGED
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -17,58 +8,56 @@ const utils_1 = require("@umijs/utils");
17
8
  const webpack_1 = __importDefault(require("../compiled/webpack"));
18
9
  const config_1 = require("./config/config");
19
10
  const types_1 = require("./types");
20
- function build(opts) {
21
- return __awaiter(this, void 0, void 0, function* () {
22
- const webpackConfig = yield (0, config_1.getConfig)({
23
- cwd: opts.cwd,
24
- rootDir: opts.rootDir,
25
- env: types_1.Env.production,
26
- entry: opts.entry,
27
- userConfig: opts.config,
28
- analyze: process.env.ANALYZE,
29
- babelPreset: opts.babelPreset,
30
- extraBabelPlugins: [
31
- ...(opts.beforeBabelPlugins || []),
32
- ...(opts.extraBabelPlugins || []),
33
- ],
34
- extraBabelPresets: [
35
- ...(opts.beforeBabelPresets || []),
36
- ...(opts.extraBabelPresets || []),
37
- ],
38
- extraBabelIncludes: opts.config.extraBabelIncludes,
39
- chainWebpack: opts.chainWebpack,
40
- modifyWebpackConfig: opts.modifyWebpackConfig,
41
- cache: opts.cache,
42
- });
43
- let isFirstCompile = true;
44
- return new Promise((resolve, reject) => {
45
- utils_1.rimraf.sync(webpackConfig.output.path);
46
- const compiler = (0, webpack_1.default)(webpackConfig);
47
- compiler.run((err, stats) => {
48
- var _a;
49
- (_a = opts.onBuildComplete) === null || _a === void 0 ? void 0 : _a.call(opts, {
50
- err,
51
- stats,
52
- isFirstCompile,
53
- time: stats ? stats.endTime - stats.startTime : null,
54
- });
55
- isFirstCompile = false;
56
- if (err || (stats === null || stats === void 0 ? void 0 : stats.hasErrors())) {
57
- if (err) {
58
- // console.error(err);
59
- reject(err);
60
- }
61
- if (stats) {
62
- const errorMsg = stats.toString('errors-only');
63
- // console.error(errorMsg);
64
- reject(new Error(errorMsg));
65
- }
11
+ async function build(opts) {
12
+ const webpackConfig = await (0, config_1.getConfig)({
13
+ cwd: opts.cwd,
14
+ rootDir: opts.rootDir,
15
+ env: types_1.Env.production,
16
+ entry: opts.entry,
17
+ userConfig: opts.config,
18
+ analyze: process.env.ANALYZE,
19
+ babelPreset: opts.babelPreset,
20
+ extraBabelPlugins: [
21
+ ...(opts.beforeBabelPlugins || []),
22
+ ...(opts.extraBabelPlugins || []),
23
+ ],
24
+ extraBabelPresets: [
25
+ ...(opts.beforeBabelPresets || []),
26
+ ...(opts.extraBabelPresets || []),
27
+ ],
28
+ extraBabelIncludes: opts.config.extraBabelIncludes,
29
+ chainWebpack: opts.chainWebpack,
30
+ modifyWebpackConfig: opts.modifyWebpackConfig,
31
+ cache: opts.cache,
32
+ });
33
+ let isFirstCompile = true;
34
+ return new Promise((resolve, reject) => {
35
+ utils_1.rimraf.sync(webpackConfig.output.path);
36
+ const compiler = (0, webpack_1.default)(webpackConfig);
37
+ compiler.run((err, stats) => {
38
+ var _a;
39
+ (_a = opts.onBuildComplete) === null || _a === void 0 ? void 0 : _a.call(opts, {
40
+ err,
41
+ stats,
42
+ isFirstCompile,
43
+ time: stats ? stats.endTime - stats.startTime : null,
44
+ });
45
+ isFirstCompile = false;
46
+ if (err || (stats === null || stats === void 0 ? void 0 : stats.hasErrors())) {
47
+ if (err) {
48
+ // console.error(err);
49
+ reject(err);
66
50
  }
67
- else {
68
- resolve(stats);
51
+ if (stats) {
52
+ const errorMsg = stats.toString('errors-only');
53
+ // console.error(errorMsg);
54
+ reject(new Error(errorMsg));
69
55
  }
70
- compiler.close(() => { });
71
- });
56
+ }
57
+ else {
58
+ resolve(stats);
59
+ }
60
+ compiler.close(() => { });
72
61
  });
73
62
  });
74
63
  }
package/dist/cli.js CHANGED
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -40,11 +31,11 @@ if ((0, fs_1.existsSync)(configFile)) {
40
31
  }
41
32
  Object.assign(config, args);
42
33
  if (command === 'build') {
43
- (() => __awaiter(void 0, void 0, void 0, function* () {
34
+ (async () => {
44
35
  process.env.NODE_ENV = 'production';
45
36
  (0, assert_1.default)(entry, `Build failed: entry not found.`);
46
37
  try {
47
- yield (0, build_1.build)({
38
+ await (0, build_1.build)({
48
39
  config,
49
40
  cwd,
50
41
  entry: {
@@ -55,14 +46,14 @@ if (command === 'build') {
55
46
  catch (e) {
56
47
  console.error(e);
57
48
  }
58
- }))();
49
+ })();
59
50
  }
60
51
  else if (command === 'dev') {
61
- (() => __awaiter(void 0, void 0, void 0, function* () {
52
+ (async () => {
62
53
  process.env.NODE_ENV = 'development';
63
54
  try {
64
55
  (0, assert_1.default)(entry, `Build failed: entry not found.`);
65
- yield (0, dev_1.dev)({
56
+ await (0, dev_1.dev)({
66
57
  config,
67
58
  cwd,
68
59
  port: process.env.PORT,
@@ -77,7 +68,7 @@ else if (command === 'dev') {
77
68
  catch (e) {
78
69
  console.error(e);
79
70
  }
80
- }))();
71
+ })();
81
72
  }
82
73
  else {
83
74
  error(`Unsupported command ${command}.`);
@@ -22,15 +22,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
35
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
27
  };
@@ -64,7 +55,7 @@ let hasCompileErrors = false;
64
55
  let hadRuntimeError = false;
65
56
  const pingUrl = getPingUrl();
66
57
  const socket = new WebSocket(getSocketUrl(), 'webpack-hmr');
67
- socket.addEventListener('message', ({ data }) => __awaiter(void 0, void 0, void 0, function* () {
58
+ socket.addEventListener('message', async ({ data }) => {
68
59
  data = JSON.parse(data);
69
60
  if (data.type === 'connected') {
70
61
  console.log(`[webpack] connected.`);
@@ -75,28 +66,26 @@ socket.addEventListener('message', ({ data }) => __awaiter(void 0, void 0, void
75
66
  else {
76
67
  handleMessage(data).catch(console.error);
77
68
  }
78
- }));
79
- function waitForSuccessfulPing(ms = 1000) {
80
- return __awaiter(this, void 0, void 0, function* () {
81
- // eslint-disable-next-line no-constant-condition
82
- while (true) {
83
- try {
84
- yield fetch(pingUrl);
85
- break;
86
- }
87
- catch (e) {
88
- yield new Promise((resolve) => setTimeout(resolve, ms));
89
- }
69
+ });
70
+ async function waitForSuccessfulPing(ms = 1000) {
71
+ // eslint-disable-next-line no-constant-condition
72
+ while (true) {
73
+ try {
74
+ await fetch(pingUrl);
75
+ break;
90
76
  }
91
- });
77
+ catch (e) {
78
+ await new Promise((resolve) => setTimeout(resolve, ms));
79
+ }
80
+ }
92
81
  }
93
- socket.addEventListener('close', () => __awaiter(void 0, void 0, void 0, function* () {
82
+ socket.addEventListener('close', async () => {
94
83
  if (pingTimer)
95
84
  clearInterval(pingTimer);
96
85
  console.info('[webpack] Dev server disconnected. Polling for restart...');
97
- yield waitForSuccessfulPing();
86
+ await waitForSuccessfulPing();
98
87
  location.reload();
99
- }));
88
+ });
100
89
  ErrorOverlay.startReportingRuntimeErrors({
101
90
  onError: function () {
102
91
  hadRuntimeError = true;
@@ -234,25 +223,23 @@ function tryApplyUpdates(onHotUpdateSuccess) {
234
223
  handleApplyUpdates(err, null);
235
224
  });
236
225
  }
237
- function handleMessage(payload) {
238
- return __awaiter(this, void 0, void 0, function* () {
239
- // console.log('[payload]', payload);
240
- switch (payload.type) {
241
- case constants_1.MESSAGE_TYPE.hash:
242
- handleAvailableHash(payload.data);
243
- break;
244
- case constants_1.MESSAGE_TYPE.stillOk:
245
- case constants_1.MESSAGE_TYPE.ok:
246
- handleSuccess();
247
- break;
248
- case constants_1.MESSAGE_TYPE.errors:
249
- handleErrors(payload.data);
250
- break;
251
- case constants_1.MESSAGE_TYPE.warnings:
252
- handleWarnings(payload.data);
253
- break;
254
- default:
255
- // Do nothing
256
- }
257
- });
226
+ async function handleMessage(payload) {
227
+ // console.log('[payload]', payload);
228
+ switch (payload.type) {
229
+ case constants_1.MESSAGE_TYPE.hash:
230
+ handleAvailableHash(payload.data);
231
+ break;
232
+ case constants_1.MESSAGE_TYPE.stillOk:
233
+ case constants_1.MESSAGE_TYPE.ok:
234
+ handleSuccess();
235
+ break;
236
+ case constants_1.MESSAGE_TYPE.errors:
237
+ handleErrors(payload.data);
238
+ break;
239
+ case constants_1.MESSAGE_TYPE.warnings:
240
+ handleWarnings(payload.data);
241
+ break;
242
+ default:
243
+ // Do nothing
244
+ }
258
245
  }
@@ -1,22 +1,11 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.addSampleFeature = void 0;
13
- function addSampleFeature(opts) {
14
- return __awaiter(this, void 0, void 0, function* () {
15
- const { config, userConfig, cwd, env } = opts;
16
- config;
17
- userConfig;
18
- cwd;
19
- env;
20
- });
4
+ async function addSampleFeature(opts) {
5
+ const { config, userConfig, cwd, env } = opts;
6
+ config;
7
+ userConfig;
8
+ cwd;
9
+ env;
21
10
  }
22
11
  exports.addSampleFeature = addSampleFeature;
@@ -1,61 +1,50 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.addAssetRules = void 0;
13
- function addAssetRules(opts) {
14
- return __awaiter(this, void 0, void 0, function* () {
15
- const { config, userConfig } = opts;
16
- const inlineLimit = parseInt(userConfig.inlineLimit || '10000', 10);
17
- const rule = config.module.rule('asset');
18
- rule
19
- .oneOf('avif')
20
- .test(/\.avif$/)
21
- .type('asset')
22
- .mimetype('image/avif')
23
- .parser({
24
- dataUrlCondition: {
25
- maxSize: inlineLimit,
26
- },
27
- })
28
- .generator({
29
- filename: `${opts.staticPathPrefix}[name].[hash:8].[ext]`,
30
- });
31
- rule
32
- .oneOf('image')
33
- .test(/\.(bmp|gif|jpg|jpeg|png)$/)
34
- .type('asset')
35
- .parser({
36
- dataUrlCondition: {
37
- maxSize: inlineLimit,
38
- },
39
- })
40
- .generator({
41
- filename: `${opts.staticPathPrefix}[name].[hash:8].[ext]`,
42
- });
43
- const fallback = rule
44
- .oneOf('fallback')
45
- .exclude.add(/^$/) /* handle data: resources */
46
- .add(/\.(js|mjs|jsx|ts|tsx)$/)
47
- .add(/\.(css|less|sass|scss|stylus)$/)
48
- .add(/\.html$/)
49
- .add(/\.json$/);
50
- if (userConfig.mdx) {
51
- fallback.add(/\.mdx?$/);
52
- }
53
- fallback
54
- .end()
55
- .type('asset/resource')
56
- .generator({
57
- filename: `${opts.staticPathPrefix}[name].[hash:8].[ext]`,
58
- });
4
+ async function addAssetRules(opts) {
5
+ const { config, userConfig } = opts;
6
+ const inlineLimit = parseInt(userConfig.inlineLimit || '10000', 10);
7
+ const rule = config.module.rule('asset');
8
+ rule
9
+ .oneOf('avif')
10
+ .test(/\.avif$/)
11
+ .type('asset')
12
+ .mimetype('image/avif')
13
+ .parser({
14
+ dataUrlCondition: {
15
+ maxSize: inlineLimit,
16
+ },
17
+ })
18
+ .generator({
19
+ filename: `${opts.staticPathPrefix}[name].[hash:8].[ext]`,
20
+ });
21
+ rule
22
+ .oneOf('image')
23
+ .test(/\.(bmp|gif|jpg|jpeg|png)$/)
24
+ .type('asset')
25
+ .parser({
26
+ dataUrlCondition: {
27
+ maxSize: inlineLimit,
28
+ },
29
+ })
30
+ .generator({
31
+ filename: `${opts.staticPathPrefix}[name].[hash:8].[ext]`,
32
+ });
33
+ const fallback = rule
34
+ .oneOf('fallback')
35
+ .exclude.add(/^$/) /* handle data: resources */
36
+ .add(/\.(js|mjs|jsx|ts|tsx)$/)
37
+ .add(/\.(css|less|sass|scss|stylus)$/)
38
+ .add(/\.html$/)
39
+ .add(/\.json$/);
40
+ if (userConfig.mdx) {
41
+ fallback.add(/\.mdx?$/);
42
+ }
43
+ fallback
44
+ .end()
45
+ .type('asset/resource')
46
+ .generator({
47
+ filename: `${opts.staticPathPrefix}[name].[hash:8].[ext]`,
59
48
  });
60
49
  }
61
50
  exports.addAssetRules = addAssetRules;
@@ -1,30 +1,19 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.addBundleAnalyzerPlugin = void 0;
13
4
  // @ts-ignore
14
5
  const webpack_bundle_analyzer_1 = require("@umijs/bundler-webpack/compiled/webpack-bundle-analyzer");
15
- function addBundleAnalyzerPlugin(opts) {
16
- return __awaiter(this, void 0, void 0, function* () {
17
- const { config } = opts;
18
- config.plugin('webpack-bundle-analyzer').use(webpack_bundle_analyzer_1.BundleAnalyzerPlugin, [
19
- // https://github.com/webpack-contrib/webpack-bundle-analyzer
20
- {
21
- analyzerMode: 'server',
22
- analyzerPort: process.env.ANALYZE_PORT || 8888,
23
- openAnalyzer: false,
24
- logLevel: 'info',
25
- defaultSizes: 'parsed',
26
- },
27
- ]);
28
- });
6
+ async function addBundleAnalyzerPlugin(opts) {
7
+ const { config } = opts;
8
+ config.plugin('webpack-bundle-analyzer').use(webpack_bundle_analyzer_1.BundleAnalyzerPlugin, [
9
+ // https://github.com/webpack-contrib/webpack-bundle-analyzer
10
+ {
11
+ analyzerMode: 'server',
12
+ analyzerPort: process.env.ANALYZE_PORT || 8888,
13
+ openAnalyzer: false,
14
+ logLevel: 'info',
15
+ defaultSizes: 'parsed',
16
+ },
17
+ ]);
29
18
  }
30
19
  exports.addBundleAnalyzerPlugin = addBundleAnalyzerPlugin;
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -20,72 +11,73 @@ const ESBuildCSSMinifyPlugin_1 = __importDefault(require("../plugins/ESBuildCSSM
20
11
  const ParcelCSSMinifyPlugin_1 = require("../plugins/ParcelCSSMinifyPlugin");
21
12
  const types_1 = require("../types");
22
13
  const getEsBuildTarget_1 = require("../utils/getEsBuildTarget");
23
- function addCompressPlugin(opts) {
24
- return __awaiter(this, void 0, void 0, function* () {
25
- const { config, userConfig, env } = opts;
26
- const jsMinifier = userConfig.jsMinifier || types_1.JSMinifier.esbuild;
27
- const cssMinifier = userConfig.cssMinifier || types_1.CSSMinifier.esbuild;
28
- if (env === types_1.Env.development ||
29
- process.env.COMPRESS === 'none' ||
30
- (jsMinifier === types_1.JSMinifier.none && cssMinifier === types_1.CSSMinifier.none)) {
31
- config.optimization.minimize(false);
32
- return;
33
- }
34
- config.optimization.minimize(true);
35
- let minify;
36
- let terserOptions;
37
- if (jsMinifier === types_1.JSMinifier.esbuild) {
38
- minify = terser_webpack_plugin_1.default.esbuildMinify;
39
- terserOptions = {
40
- target: (0, getEsBuildTarget_1.getEsBuildTarget)({
41
- targets: userConfig.targets || {},
42
- }),
43
- };
44
- }
45
- else if (jsMinifier === types_1.JSMinifier.terser) {
46
- minify = terser_webpack_plugin_1.default.terserMinify;
47
- }
48
- else if (jsMinifier === types_1.JSMinifier.swc) {
49
- minify = terser_webpack_plugin_1.default.swcMinify;
50
- }
51
- else if (jsMinifier === types_1.JSMinifier.uglifyJs) {
52
- minify = terser_webpack_plugin_1.default.uglifyJsMinify;
53
- }
54
- else if (jsMinifier !== types_1.JSMinifier.none) {
55
- throw new Error(`Unsupported jsMinifier ${userConfig.jsMinifier}.`);
56
- }
57
- terserOptions = Object.assign(Object.assign({}, terserOptions), userConfig.jsMinifierOptions);
58
- if (jsMinifier !== types_1.JSMinifier.none) {
59
- config.optimization.minimizer(`js-${jsMinifier}`).use(terser_webpack_plugin_1.default, [
60
- {
61
- minify,
62
- terserOptions,
63
- },
64
- ]);
65
- }
66
- if (cssMinifier === types_1.CSSMinifier.esbuild) {
67
- config.optimization
68
- .minimizer(`css-${cssMinifier}`)
69
- .use(ESBuildCSSMinifyPlugin_1.default, [userConfig.cssMinifierOptions]);
70
- }
71
- else if (cssMinifier === types_1.CSSMinifier.cssnano) {
72
- config.optimization
73
- .minimizer(`css-${cssMinifier}`)
74
- .use(css_minimizer_webpack_plugin_1.default, [
75
- {
76
- minimizerOptions: userConfig.cssMinifierOptions,
77
- parallel: true,
78
- },
79
- ]);
80
- }
81
- else if (cssMinifier === types_1.CSSMinifier.parcelCSS) {
82
- config.optimization
83
- .minimizer(`css-${cssMinifier}`)
84
- .use(ParcelCSSMinifyPlugin_1.ParcelCSSMinifyPlugin);
85
- }
86
- else if (cssMinifier !== types_1.CSSMinifier.none) {
87
- throw new Error(`Unsupported cssMinifier ${userConfig.cssMinifier}.`);
88
- }
89
- });
14
+ async function addCompressPlugin(opts) {
15
+ const { config, userConfig, env } = opts;
16
+ const jsMinifier = userConfig.jsMinifier || types_1.JSMinifier.esbuild;
17
+ const cssMinifier = userConfig.cssMinifier || types_1.CSSMinifier.esbuild;
18
+ if (env === types_1.Env.development ||
19
+ process.env.COMPRESS === 'none' ||
20
+ (jsMinifier === types_1.JSMinifier.none && cssMinifier === types_1.CSSMinifier.none)) {
21
+ config.optimization.minimize(false);
22
+ return;
23
+ }
24
+ config.optimization.minimize(true);
25
+ let minify;
26
+ let terserOptions;
27
+ if (jsMinifier === types_1.JSMinifier.esbuild) {
28
+ minify = terser_webpack_plugin_1.default.esbuildMinify;
29
+ terserOptions = {
30
+ target: (0, getEsBuildTarget_1.getEsBuildTarget)({
31
+ targets: userConfig.targets || {},
32
+ }),
33
+ };
34
+ }
35
+ else if (jsMinifier === types_1.JSMinifier.terser) {
36
+ minify = terser_webpack_plugin_1.default.terserMinify;
37
+ }
38
+ else if (jsMinifier === types_1.JSMinifier.swc) {
39
+ minify = terser_webpack_plugin_1.default.swcMinify;
40
+ }
41
+ else if (jsMinifier === types_1.JSMinifier.uglifyJs) {
42
+ minify = terser_webpack_plugin_1.default.uglifyJsMinify;
43
+ }
44
+ else if (jsMinifier !== types_1.JSMinifier.none) {
45
+ throw new Error(`Unsupported jsMinifier ${userConfig.jsMinifier}.`);
46
+ }
47
+ terserOptions = {
48
+ ...terserOptions,
49
+ ...userConfig.jsMinifierOptions,
50
+ };
51
+ if (jsMinifier !== types_1.JSMinifier.none) {
52
+ config.optimization.minimizer(`js-${jsMinifier}`).use(terser_webpack_plugin_1.default, [
53
+ {
54
+ minify,
55
+ terserOptions,
56
+ },
57
+ ]);
58
+ }
59
+ if (cssMinifier === types_1.CSSMinifier.esbuild) {
60
+ config.optimization
61
+ .minimizer(`css-${cssMinifier}`)
62
+ .use(ESBuildCSSMinifyPlugin_1.default, [userConfig.cssMinifierOptions]);
63
+ }
64
+ else if (cssMinifier === types_1.CSSMinifier.cssnano) {
65
+ config.optimization
66
+ .minimizer(`css-${cssMinifier}`)
67
+ .use(css_minimizer_webpack_plugin_1.default, [
68
+ {
69
+ minimizerOptions: userConfig.cssMinifierOptions,
70
+ parallel: true,
71
+ },
72
+ ]);
73
+ }
74
+ else if (cssMinifier === types_1.CSSMinifier.parcelCSS) {
75
+ config.optimization
76
+ .minimizer(`css-${cssMinifier}`)
77
+ .use(ParcelCSSMinifyPlugin_1.ParcelCSSMinifyPlugin);
78
+ }
79
+ else if (cssMinifier !== types_1.CSSMinifier.none) {
80
+ throw new Error(`Unsupported cssMinifier ${userConfig.cssMinifier}.`);
81
+ }
90
82
  }
91
83
  exports.addCompressPlugin = addCompressPlugin;