@umijs/bundler-webpack 4.0.0-rc.2 → 4.0.0-rc.22

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 (113) hide show
  1. package/client/client/client.js +65 -37
  2. package/client/constants.js +9 -0
  3. package/compiled/autoprefixer/browserslist/index.d.ts +2 -0
  4. package/compiled/autoprefixer/index.js +3 -3
  5. package/compiled/autoprefixer/postcss/lib/declaration.d.ts +1 -1
  6. package/compiled/autoprefixer/postcss/lib/node.d.ts +2 -2
  7. package/compiled/babel-loader/index.js +2 -2
  8. package/compiled/copy-webpack-plugin/{576.index.js → 939.index.js} +11 -11
  9. package/compiled/copy-webpack-plugin/index.js +12 -12
  10. package/compiled/css-minimizer-webpack-plugin/index.js +7 -7
  11. package/compiled/cssnano/index.js +6 -6
  12. package/compiled/cssnano/package.json +1 -1
  13. package/compiled/express.d.ts +2 -0
  14. package/compiled/fork-ts-checker-webpack-plugin/index.js +7 -13
  15. package/compiled/http-proxy-middleware/index.js +10 -10
  16. package/compiled/mini-css-extract-plugin/index.js +440 -401
  17. package/compiled/mini-css-extract-plugin/loader.js +3 -1
  18. package/compiled/mini-css-extract-plugin/utils.js +2 -0
  19. package/compiled/{tapable → react-refresh}/LICENSE +6 -6
  20. package/compiled/react-refresh/index.js +9 -7
  21. package/compiled/react-refresh/package.json +1 -0
  22. package/compiled/terser/index.js +1 -1
  23. package/compiled/webpack/BasicEffectRulePlugin.js +1 -0
  24. package/compiled/webpack/BasicMatcherRulePlugin.js +1 -0
  25. package/compiled/webpack/HotModuleReplacement.runtime.js +29 -14
  26. package/compiled/webpack/JavascriptHotModuleReplacement.runtime.js +4 -3
  27. package/compiled/webpack/ObjectMatcherRulePlugin.js +1 -0
  28. package/compiled/webpack/RuleSetCompiler.js +1 -0
  29. package/compiled/webpack/UseEffectRulePlugin.js +1 -0
  30. package/compiled/webpack/deepImports.json +6 -1
  31. package/compiled/webpack/index.js +5155 -3422
  32. package/compiled/webpack/types.d.ts +606 -171
  33. package/compiled/webpack-dev-middleware/index.js +8 -7
  34. package/compiled/webpack-manifest-plugin/index.js +1 -1
  35. package/dist/build.d.ts +1 -0
  36. package/dist/build.js +48 -57
  37. package/dist/cli.js +6 -15
  38. package/dist/client/client.js +57 -51
  39. package/dist/config/_sampleFeature.js +6 -17
  40. package/dist/config/assetRules.js +44 -55
  41. package/dist/config/bundleAnalyzerPlugin.js +12 -23
  42. package/dist/config/compressPlugin.js +89 -64
  43. package/dist/config/config.d.ts +2 -0
  44. package/dist/config/config.js +178 -179
  45. package/dist/config/copyPlugin.js +29 -40
  46. package/dist/config/cssRules.js +93 -83
  47. package/dist/config/definePlugin.js +11 -19
  48. package/dist/config/detectDeadCode.js +1 -1
  49. package/dist/config/detectDeadCodePlugin.js +21 -22
  50. package/dist/config/fastRefreshPlugin.js +11 -22
  51. package/dist/config/forkTSCheckerPlugin.js +11 -22
  52. package/dist/config/harmonyLinkingErrorPlugin.js +3 -14
  53. package/dist/config/ignorePlugin.js +10 -21
  54. package/dist/config/javaScriptRules.d.ts +1 -0
  55. package/dist/config/javaScriptRules.js +152 -138
  56. package/dist/config/manifestPlugin.js +10 -18
  57. package/dist/config/miniCSSExtractPlugin.js +12 -23
  58. package/dist/config/nodePolyfill.js +14 -20
  59. package/dist/config/nodePrefixPlugin.d.ts +11 -0
  60. package/dist/config/nodePrefixPlugin.js +14 -0
  61. package/dist/config/progressPlugin.js +7 -18
  62. package/dist/config/purgecssWebpackPlugin.js +15 -26
  63. package/dist/config/speedMeasureWebpackPlugin.js +12 -23
  64. package/dist/config/svgRules.js +44 -47
  65. package/dist/constants.d.ts +1 -0
  66. package/dist/constants.js +7 -1
  67. package/dist/dev.d.ts +4 -0
  68. package/dist/dev.js +115 -94
  69. package/dist/index.d.ts +4 -1
  70. package/dist/index.js +5 -1
  71. package/dist/loader/svgr.js +4 -13
  72. package/dist/loader/swc.js +9 -14
  73. package/dist/plugins/ProgressPlugin.js +2 -2
  74. package/dist/plugins/RuntimePublicPathPlugin.js +4 -1
  75. package/dist/schema.js +29 -13
  76. package/dist/server/server.d.ts +3 -1
  77. package/dist/server/server.js +166 -148
  78. package/dist/server/ws.d.ts +7 -2
  79. package/dist/swcPlugins/autoCSSModules.js +3 -1
  80. package/dist/swcPlugins/changeImportFromString.d.ts +2 -0
  81. package/dist/swcPlugins/changeImportFromString.js +10 -0
  82. package/dist/swcPlugins/lockCoreJS.d.ts +1 -1
  83. package/dist/swcPlugins/lockCoreJS.js +3 -2
  84. package/dist/types.d.ts +4 -0
  85. package/dist/types.js +1 -0
  86. package/dist/utils/getEsBuildTarget.d.ts +5 -0
  87. package/dist/utils/getEsBuildTarget.js +12 -0
  88. package/package.json +33 -35
  89. package/compiled/css-loader/LICENSE +0 -20
  90. package/compiled/css-loader/api.js +0 -102
  91. package/compiled/css-loader/getUrl.js +0 -29
  92. package/compiled/css-loader/index.js +0 -2
  93. package/compiled/css-loader/noSourceMaps.js +0 -5
  94. package/compiled/css-loader/package.json +0 -1
  95. package/compiled/css-loader/sourceMaps.js +0 -22
  96. package/compiled/express/LICENSE +0 -24
  97. package/compiled/express/body-parser/index.d.ts +0 -104
  98. package/compiled/express/connect/index.d.ts +0 -93
  99. package/compiled/express/express-serve-static-core/index.d.ts +0 -1252
  100. package/compiled/express/index.d.ts +0 -133
  101. package/compiled/express/index.js +0 -321
  102. package/compiled/express/mime/index.d.ts +0 -35
  103. package/compiled/express/package.json +0 -1
  104. package/compiled/express/qs/index.d.ts +0 -62
  105. package/compiled/express/range-parser/index.d.ts +0 -35
  106. package/compiled/express/serve-static/index.d.ts +0 -108
  107. package/compiled/less/index.js +0 -31
  108. package/compiled/less/package.json +0 -1
  109. package/compiled/tapable/index.js +0 -1
  110. package/compiled/tapable/package.json +0 -1
  111. package/compiled/tapable/tapable.d.ts +0 -116
  112. package/dist/plugins/ESBuildCSSMinifyPlugin.d.ts +0 -11
  113. package/dist/plugins/ESBuildCSSMinifyPlugin.js +0 -65
@@ -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
  };
@@ -16,62 +7,96 @@ exports.addCompressPlugin = void 0;
16
7
  // @ts-ignore
17
8
  const css_minimizer_webpack_plugin_1 = __importDefault(require("@umijs/bundler-webpack/compiled/css-minimizer-webpack-plugin"));
18
9
  const terser_webpack_plugin_1 = __importDefault(require("../../compiled/terser-webpack-plugin"));
19
- const ESBuildCSSMinifyPlugin_1 = __importDefault(require("../plugins/ESBuildCSSMinifyPlugin"));
20
10
  const types_1 = require("../types");
21
- function addCompressPlugin(opts) {
22
- return __awaiter(this, void 0, void 0, function* () {
23
- const { config, userConfig, env } = opts;
24
- const jsMinifier = userConfig.jsMinifier || types_1.JSMinifier.esbuild;
25
- const cssMinifier = userConfig.cssMinifier || types_1.CSSMinifier.esbuild;
26
- if (env === types_1.Env.development ||
27
- process.env.COMPRESS === 'none' ||
28
- (jsMinifier === types_1.JSMinifier.none && cssMinifier === types_1.CSSMinifier.none)) {
29
- config.optimization.minimize(false);
30
- return;
31
- }
32
- config.optimization.minimize(true);
33
- let minify;
34
- if (jsMinifier === types_1.JSMinifier.esbuild) {
35
- minify = terser_webpack_plugin_1.default.esbuildMinify;
36
- }
37
- else if (jsMinifier === types_1.JSMinifier.terser) {
38
- minify = terser_webpack_plugin_1.default.terserMinify;
39
- }
40
- else if (jsMinifier === types_1.JSMinifier.swc) {
41
- minify = terser_webpack_plugin_1.default.swcMinify;
42
- }
43
- else if (jsMinifier === types_1.JSMinifier.uglifyJs) {
44
- minify = terser_webpack_plugin_1.default.uglifyJsMinify;
45
- }
46
- else if (jsMinifier !== types_1.JSMinifier.none) {
47
- throw new Error(`Unsupported jsMinifier ${userConfig.jsMinifier}.`);
48
- }
49
- if (jsMinifier !== types_1.JSMinifier.none) {
50
- config.optimization.minimizer(`js-${jsMinifier}`).use(terser_webpack_plugin_1.default, [
51
- {
52
- minify,
53
- terserOptions: userConfig.jsMinifierOptions,
54
- },
55
- ]);
56
- }
57
- if (cssMinifier === types_1.CSSMinifier.esbuild) {
58
- config.optimization
59
- .minimizer(`css-${cssMinifier}`)
60
- .use(ESBuildCSSMinifyPlugin_1.default, [userConfig.cssMinifierOptions]);
61
- }
62
- else if (cssMinifier === types_1.CSSMinifier.cssnano) {
63
- config.optimization
64
- .minimizer(`css-${cssMinifier}`)
65
- .use(css_minimizer_webpack_plugin_1.default, [
66
- {
67
- minimizerOptions: userConfig.cssMinifierOptions,
68
- parallel: true,
69
- },
70
- ]);
71
- }
72
- else if (cssMinifier !== types_1.CSSMinifier.none) {
73
- throw new Error(`Unsupported cssMinifier ${userConfig.cssMinifier}.`);
74
- }
11
+ const getEsBuildTarget_1 = require("../utils/getEsBuildTarget");
12
+ async function addCompressPlugin(opts) {
13
+ const { config, userConfig, env } = opts;
14
+ const jsMinifier = userConfig.jsMinifier || types_1.JSMinifier.esbuild;
15
+ const cssMinifier = userConfig.cssMinifier || types_1.CSSMinifier.esbuild;
16
+ if (env === types_1.Env.development ||
17
+ process.env.COMPRESS === 'none' ||
18
+ (jsMinifier === types_1.JSMinifier.none && cssMinifier === types_1.CSSMinifier.none)) {
19
+ config.optimization.minimize(false);
20
+ return;
21
+ }
22
+ config.optimization.minimize(true);
23
+ // esbuild transform only allow `string[]` as target
24
+ const esbuildTarget = (0, getEsBuildTarget_1.getEsBuildTarget)({
25
+ targets: userConfig.targets || {},
75
26
  });
27
+ let minify;
28
+ let terserOptions;
29
+ if (jsMinifier === types_1.JSMinifier.esbuild) {
30
+ minify = terser_webpack_plugin_1.default.esbuildMinify;
31
+ terserOptions = {
32
+ target: esbuildTarget,
33
+ // remove all comments
34
+ legalComments: 'none',
35
+ };
36
+ }
37
+ else if (jsMinifier === types_1.JSMinifier.terser) {
38
+ minify = terser_webpack_plugin_1.default.terserMinify;
39
+ terserOptions = {
40
+ format: {
41
+ comments: false,
42
+ },
43
+ };
44
+ }
45
+ else if (jsMinifier === types_1.JSMinifier.swc) {
46
+ minify = terser_webpack_plugin_1.default.swcMinify;
47
+ }
48
+ else if (jsMinifier === types_1.JSMinifier.uglifyJs) {
49
+ minify = terser_webpack_plugin_1.default.uglifyJsMinify;
50
+ terserOptions = {
51
+ output: {
52
+ comments: false,
53
+ },
54
+ };
55
+ }
56
+ else if (jsMinifier !== types_1.JSMinifier.none) {
57
+ throw new Error(`Unsupported jsMinifier ${userConfig.jsMinifier}.`);
58
+ }
59
+ terserOptions = {
60
+ ...terserOptions,
61
+ ...userConfig.jsMinifierOptions,
62
+ };
63
+ if (jsMinifier !== types_1.JSMinifier.none) {
64
+ config.optimization.minimizer(`js-${jsMinifier}`).use(terser_webpack_plugin_1.default, [
65
+ {
66
+ extractComments: false,
67
+ minify,
68
+ terserOptions,
69
+ },
70
+ ]);
71
+ }
72
+ let cssMinify;
73
+ let minimizerOptions;
74
+ if (cssMinifier === types_1.CSSMinifier.esbuild) {
75
+ cssMinify = css_minimizer_webpack_plugin_1.default.esbuildMinify;
76
+ minimizerOptions = {
77
+ target: esbuildTarget,
78
+ };
79
+ }
80
+ else if (cssMinifier === types_1.CSSMinifier.cssnano) {
81
+ cssMinify = css_minimizer_webpack_plugin_1.default.cssnanoMinify;
82
+ }
83
+ else if (cssMinifier === types_1.CSSMinifier.parcelCSS) {
84
+ cssMinify = css_minimizer_webpack_plugin_1.default.parcelCssMinify;
85
+ }
86
+ else if (cssMinifier !== types_1.CSSMinifier.none) {
87
+ throw new Error(`Unsupported cssMinifier ${userConfig.cssMinifier}.`);
88
+ }
89
+ minimizerOptions = {
90
+ ...minimizerOptions,
91
+ ...userConfig.cssMinifierOptions,
92
+ };
93
+ config.optimization
94
+ .minimizer(`css-${cssMinifier}`)
95
+ .use(css_minimizer_webpack_plugin_1.default, [
96
+ {
97
+ minify: cssMinify,
98
+ minimizerOptions,
99
+ },
100
+ ]);
76
101
  }
77
102
  exports.addCompressPlugin = addCompressPlugin;
@@ -2,10 +2,12 @@ import { Configuration } from '../../compiled/webpack';
2
2
  import { Env, IConfig } from '../types';
3
3
  export interface IOpts {
4
4
  cwd: string;
5
+ rootDir?: string;
5
6
  env: Env;
6
7
  entry: Record<string, string>;
7
8
  extraBabelPresets?: any[];
8
9
  extraBabelPlugins?: any[];
10
+ extraBabelIncludes?: string[];
9
11
  extraEsbuildLoaderHandler?: any[];
10
12
  babelPreset?: any;
11
13
  chainWebpack?: Function;
@@ -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
  };
@@ -38,183 +29,191 @@ const nodePolyfill_1 = require("./nodePolyfill");
38
29
  const progressPlugin_1 = require("./progressPlugin");
39
30
  const speedMeasureWebpackPlugin_1 = require("./speedMeasureWebpackPlugin");
40
31
  const svgRules_1 = require("./svgRules");
41
- function getConfig(opts) {
42
- return __awaiter(this, void 0, void 0, function* () {
43
- const { userConfig } = opts;
44
- const isDev = opts.env === types_1.Env.development;
45
- const config = new webpack_5_chain_1.default();
46
- userConfig.targets || (userConfig.targets = constants_1.DEFAULT_BROWSER_TARGETS);
47
- const useHash = !!(opts.hash || (userConfig.hash && !isDev));
48
- const applyOpts = {
49
- name: opts.name,
50
- config,
51
- userConfig,
52
- cwd: opts.cwd,
53
- env: opts.env,
54
- babelPreset: opts.babelPreset,
55
- extraBabelPlugins: opts.extraBabelPlugins || [],
56
- extraBabelPresets: opts.extraBabelPresets || [],
57
- extraEsbuildLoaderHandler: opts.extraEsbuildLoaderHandler || [],
58
- browsers: (0, browsersList_1.getBrowsersList)({
59
- targets: userConfig.targets,
60
- }),
61
- useHash,
62
- staticPathPrefix: opts.staticPathPrefix !== undefined ? opts.staticPathPrefix : 'static/',
63
- };
64
- // mode
65
- config.mode(opts.env);
66
- config.stats('none');
67
- // entry
68
- Object.keys(opts.entry).forEach((key) => {
69
- const entry = config.entry(key);
70
- if (isDev && opts.hmr) {
71
- entry.add(require.resolve('../../client/client/client'));
72
- }
73
- entry.add(opts.entry[key]);
74
- });
75
- // devtool
76
- config.devtool(isDev
77
- ? userConfig.devtool === false
78
- ? false
79
- : userConfig.devtool || constants_1.DEFAULT_DEVTOOL
80
- : userConfig.devtool);
81
- // output
82
- const absOutputPath = (0, path_1.join)(opts.cwd, userConfig.outputPath || constants_1.DEFAULT_OUTPUT_PATH);
83
- const disableCompress = process.env.COMPRESS === 'none';
84
- config.output
85
- .path(absOutputPath)
86
- .filename(useHash ? `[name].[contenthash:8].js` : `[name].js`)
87
- .chunkFilename(useHash ? `[name].[contenthash:8].async.js` : `[name].js`)
88
- .publicPath(userConfig.publicPath || 'auto')
89
- .pathinfo(isDev || disableCompress);
90
- // resolve
91
- // prettier-ignore
92
- config.resolve
93
- .set('symlinks', true)
94
- .modules
95
- .add('node_modules')
96
- .end()
97
- .alias
98
- .merge(userConfig.alias || {})
99
- .end()
100
- .extensions
101
- .merge([
102
- '.wasm',
103
- '.mjs',
104
- '.js',
105
- '.jsx',
106
- '.ts',
107
- '.tsx',
108
- '.json'
109
- ])
110
- .end();
111
- // externals
112
- config.externals(userConfig.externals || []);
113
- // target
114
- config.target(['web', 'es5']);
115
- // experiments
116
- config.experiments({
117
- topLevelAwait: true,
118
- outputModule: !!userConfig.esm,
119
- });
120
- // node polyfill
121
- yield (0, nodePolyfill_1.addNodePolyfill)(applyOpts);
122
- // rules
123
- yield (0, javaScriptRules_1.addJavaScriptRules)(applyOpts);
124
- yield (0, cssRules_1.addCSSRules)(applyOpts);
125
- yield (0, assetRules_1.addAssetRules)(applyOpts);
126
- yield (0, svgRules_1.addSVGRules)(applyOpts);
127
- // plugins
128
- // mini-css-extract-plugin
129
- yield (0, miniCSSExtractPlugin_1.addMiniCSSExtractPlugin)(applyOpts);
130
- // ignoreMomentLocale
131
- yield (0, ignorePlugin_1.addIgnorePlugin)(applyOpts);
132
- // define
133
- yield (0, definePlugin_1.addDefinePlugin)(applyOpts);
134
- // fast refresh
135
- yield (0, fastRefreshPlugin_1.addFastRefreshPlugin)(applyOpts);
136
- // progress
137
- yield (0, progressPlugin_1.addProgressPlugin)(applyOpts);
138
- // detect-dead-code-plugin
139
- yield (0, detectDeadCodePlugin_1.addDetectDeadCodePlugin)(applyOpts);
140
- // fork-ts-checker
141
- yield (0, forkTSCheckerPlugin_1.addForkTSCheckerPlugin)(applyOpts);
142
- // copy
143
- yield (0, copyPlugin_1.addCopyPlugin)(applyOpts);
144
- // manifest
145
- yield (0, manifestPlugin_1.addManifestPlugin)(applyOpts);
146
- // hmr
32
+ async function getConfig(opts) {
33
+ const { userConfig } = opts;
34
+ const isDev = opts.env === types_1.Env.development;
35
+ const config = new webpack_5_chain_1.default();
36
+ userConfig.targets || (userConfig.targets = constants_1.DEFAULT_BROWSER_TARGETS);
37
+ const useHash = !!(opts.hash || (userConfig.hash && !isDev));
38
+ const applyOpts = {
39
+ name: opts.name,
40
+ config,
41
+ userConfig,
42
+ cwd: opts.cwd,
43
+ env: opts.env,
44
+ babelPreset: opts.babelPreset,
45
+ extraBabelPlugins: opts.extraBabelPlugins || [],
46
+ extraBabelPresets: opts.extraBabelPresets || [],
47
+ extraBabelIncludes: opts.extraBabelIncludes || [],
48
+ extraEsbuildLoaderHandler: opts.extraEsbuildLoaderHandler || [],
49
+ browsers: (0, browsersList_1.getBrowsersList)({
50
+ targets: userConfig.targets,
51
+ }),
52
+ useHash,
53
+ staticPathPrefix: opts.staticPathPrefix !== undefined ? opts.staticPathPrefix : 'static/',
54
+ };
55
+ // mode
56
+ config.mode(opts.env);
57
+ config.stats('none');
58
+ // entry
59
+ Object.keys(opts.entry).forEach((key) => {
60
+ const entry = config.entry(key);
147
61
  if (isDev && opts.hmr) {
148
- config.plugin('hmr').use(webpack_1.default.HotModuleReplacementPlugin);
149
- }
150
- // compress
151
- yield (0, compressPlugin_1.addCompressPlugin)(applyOpts);
152
- // purgecss
153
- // await applyPurgeCSSWebpackPlugin(applyOpts);
154
- // handle HarmonyLinkingError
155
- yield (0, harmonyLinkingErrorPlugin_1.addHarmonyLinkingErrorPlugin)(applyOpts);
156
- // runtimePublicPath
157
- if (userConfig.runtimePublicPath) {
158
- config.plugin('runtimePublicPath').use(RuntimePublicPathPlugin_1.RuntimePublicPathPlugin);
62
+ entry.add(require.resolve('../../client/client/client'));
159
63
  }
160
- // cache
161
- if (opts.cache) {
162
- config.cache({
163
- type: 'filesystem',
164
- version: require('../../package.json').version,
165
- buildDependencies: {
166
- config: opts.cache.buildDependencies || [],
167
- },
168
- cacheDirectory: opts.cache.cacheDirectory ||
169
- (0, path_1.join)(opts.cwd, 'node_modules', '.cache', 'bundler-webpack'),
64
+ entry.add(opts.entry[key]);
65
+ });
66
+ // devtool
67
+ config.devtool(isDev
68
+ ? userConfig.devtool === false
69
+ ? false
70
+ : userConfig.devtool || constants_1.DEFAULT_DEVTOOL
71
+ : userConfig.devtool);
72
+ // output
73
+ const absOutputPath = (0, path_1.join)(opts.cwd, userConfig.outputPath || constants_1.DEFAULT_OUTPUT_PATH);
74
+ const disableCompress = process.env.COMPRESS === 'none';
75
+ config.output
76
+ .path(absOutputPath)
77
+ .filename(useHash ? `[name].[contenthash:8].js` : `[name].js`)
78
+ .chunkFilename(useHash ? `[name].[contenthash:8].async.js` : `[name].js`)
79
+ .publicPath(userConfig.publicPath || 'auto')
80
+ .pathinfo(isDev || disableCompress);
81
+ // resolve
82
+ // prettier-ignore
83
+ config.resolve
84
+ .set('symlinks', true)
85
+ .modules
86
+ .add('node_modules')
87
+ .end()
88
+ .alias
89
+ .merge(userConfig.alias || {})
90
+ .end()
91
+ .extensions
92
+ .merge([
93
+ '.wasm',
94
+ '.mjs',
95
+ '.js',
96
+ '.jsx',
97
+ '.ts',
98
+ '.tsx',
99
+ '.json'
100
+ ])
101
+ .end();
102
+ // externals
103
+ config.externals(userConfig.externals || []);
104
+ // target
105
+ config.target(['web', 'es5']);
106
+ // experiments
107
+ config.experiments({
108
+ topLevelAwait: true,
109
+ outputModule: !!userConfig.esm,
110
+ });
111
+ // node polyfill
112
+ await (0, nodePolyfill_1.addNodePolyfill)(applyOpts);
113
+ // rules
114
+ await (0, javaScriptRules_1.addJavaScriptRules)(applyOpts);
115
+ await (0, cssRules_1.addCSSRules)(applyOpts);
116
+ await (0, assetRules_1.addAssetRules)(applyOpts);
117
+ await (0, svgRules_1.addSVGRules)(applyOpts);
118
+ // plugins
119
+ // mini-css-extract-plugin
120
+ await (0, miniCSSExtractPlugin_1.addMiniCSSExtractPlugin)(applyOpts);
121
+ // ignoreMomentLocale
122
+ await (0, ignorePlugin_1.addIgnorePlugin)(applyOpts);
123
+ // define
124
+ await (0, definePlugin_1.addDefinePlugin)(applyOpts);
125
+ // fast refresh
126
+ await (0, fastRefreshPlugin_1.addFastRefreshPlugin)(applyOpts);
127
+ // progress
128
+ await (0, progressPlugin_1.addProgressPlugin)(applyOpts);
129
+ // detect-dead-code-plugin
130
+ await (0, detectDeadCodePlugin_1.addDetectDeadCodePlugin)(applyOpts);
131
+ // fork-ts-checker
132
+ await (0, forkTSCheckerPlugin_1.addForkTSCheckerPlugin)(applyOpts);
133
+ // copy
134
+ await (0, copyPlugin_1.addCopyPlugin)(applyOpts);
135
+ // manifest
136
+ await (0, manifestPlugin_1.addManifestPlugin)(applyOpts);
137
+ // hmr
138
+ if (isDev && opts.hmr) {
139
+ config.plugin('hmr').use(webpack_1.default.HotModuleReplacementPlugin);
140
+ }
141
+ // compress
142
+ await (0, compressPlugin_1.addCompressPlugin)(applyOpts);
143
+ // purgecss
144
+ // await applyPurgeCSSWebpackPlugin(applyOpts);
145
+ // handle HarmonyLinkingError
146
+ await (0, harmonyLinkingErrorPlugin_1.addHarmonyLinkingErrorPlugin)(applyOpts);
147
+ // remove node: prefix
148
+ // disable for performance
149
+ // await addNodePrefixPlugin(applyOpts);
150
+ // runtimePublicPath
151
+ if (userConfig.runtimePublicPath) {
152
+ config.plugin('runtimePublicPath').use(RuntimePublicPathPlugin_1.RuntimePublicPathPlugin);
153
+ }
154
+ // cache
155
+ if (opts.cache) {
156
+ config.cache({
157
+ type: 'filesystem',
158
+ version: require('../../package.json').version,
159
+ buildDependencies: {
160
+ config: opts.cache.buildDependencies || [],
161
+ },
162
+ cacheDirectory: opts.cache.cacheDirectory ||
163
+ // 使用 rootDir 是在有 APP_ROOT 时,把 cache 目录放在根目录下
164
+ (0, path_1.join)(opts.rootDir || opts.cwd, 'node_modules', '.cache', 'bundler-webpack'),
165
+ });
166
+ // tnpm 安装依赖的情况 webpack 默认的 managedPaths 不生效
167
+ // 使用 immutablePaths 避免 node_modules 的内容被写入缓存
168
+ // tnpm 安装的依赖路径中同时包含包名和版本号,满足 immutablePaths 使用的条件
169
+ // 同时配置 managedPaths 将 tnpm 的软连接结构标记为可信,避免执行快照序列化时 OOM
170
+ // ref: smallfish
171
+ if ( /*isTnpm*/require('@umijs/utils/package').__npminstall_done) {
172
+ const nodeModulesPath = opts.cache.absNodeModulesPath ||
173
+ (0, path_1.join)(opts.rootDir || opts.cwd, 'node_modules');
174
+ config.snapshot({
175
+ immutablePaths: [nodeModulesPath],
176
+ managedPaths: [nodeModulesPath],
170
177
  });
171
- // tnpm 安装依赖的情况 webpack 默认的 managedPaths 不生效
172
- // 使用 immutablePaths 避免 node_modules 的内容被写入缓存
173
- // tnpm 安装的依赖路径中同时包含包名和版本号,满足 immutablePaths 使用的条件
174
- // ref: smallfish
175
- if ( /*isTnpm*/require('@umijs/utils/package').__npminstall_done) {
176
- config.snapshot({
177
- immutablePaths: [
178
- opts.cache.absNodeModulesPath || (0, path_1.join)(opts.cwd, 'node_modules'),
179
- ],
180
- });
181
- }
182
- config.infrastructureLogging(Object.assign({ level: 'error' }, (process.env.WEBPACK_FS_CACHE_DEBUG
178
+ }
179
+ config.infrastructureLogging({
180
+ level: 'error',
181
+ ...(process.env.WEBPACK_FS_CACHE_DEBUG
183
182
  ? {
184
183
  debug: /webpack\.cache/,
185
184
  }
186
- : {})));
187
- }
188
- // analyzer
189
- if (opts.analyze) {
190
- yield (0, bundleAnalyzerPlugin_1.addBundleAnalyzerPlugin)(applyOpts);
191
- }
192
- // chain webpack
193
- if (opts.chainWebpack) {
194
- yield opts.chainWebpack(config, {
195
- env: opts.env,
196
- webpack: webpack_1.default,
197
- });
198
- }
199
- if (userConfig.chainWebpack) {
200
- yield userConfig.chainWebpack(config, {
201
- env: opts.env,
202
- webpack: webpack_1.default,
203
- });
204
- }
205
- let webpackConfig = config.toConfig();
206
- // speed measure
207
- // TODO: mini-css-extract-plugin 报错
208
- webpackConfig = yield (0, speedMeasureWebpackPlugin_1.addSpeedMeasureWebpackPlugin)({
209
- webpackConfig,
185
+ : {}),
210
186
  });
211
- if (opts.modifyWebpackConfig) {
212
- webpackConfig = yield opts.modifyWebpackConfig(webpackConfig, {
213
- env: opts.env,
214
- webpack: webpack_1.default,
215
- });
216
- }
217
- return webpackConfig;
187
+ }
188
+ // analyzer
189
+ if (opts.analyze) {
190
+ await (0, bundleAnalyzerPlugin_1.addBundleAnalyzerPlugin)(applyOpts);
191
+ }
192
+ // chain webpack
193
+ if (opts.chainWebpack) {
194
+ await opts.chainWebpack(config, {
195
+ env: opts.env,
196
+ webpack: webpack_1.default,
197
+ });
198
+ }
199
+ if (userConfig.chainWebpack) {
200
+ await userConfig.chainWebpack(config, {
201
+ env: opts.env,
202
+ webpack: webpack_1.default,
203
+ });
204
+ }
205
+ let webpackConfig = config.toConfig();
206
+ // speed measure
207
+ // TODO: mini-css-extract-plugin 报错
208
+ webpackConfig = await (0, speedMeasureWebpackPlugin_1.addSpeedMeasureWebpackPlugin)({
209
+ webpackConfig,
218
210
  });
211
+ if (opts.modifyWebpackConfig) {
212
+ webpackConfig = await opts.modifyWebpackConfig(webpackConfig, {
213
+ env: opts.env,
214
+ webpack: webpack_1.default,
215
+ });
216
+ }
217
+ return webpackConfig;
219
218
  }
220
219
  exports.getConfig = getConfig;
@@ -1,49 +1,38 @@
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.addCopyPlugin = void 0;
13
4
  const fs_1 = require("fs");
14
5
  const path_1 = require("path");
15
- function addCopyPlugin(opts) {
16
- return __awaiter(this, void 0, void 0, function* () {
17
- const { config, userConfig, cwd } = opts;
18
- const copyPatterns = [
19
- (0, fs_1.existsSync)((0, path_1.join)(cwd, 'public')) && {
20
- from: (0, path_1.join)(cwd, 'public'),
21
- },
22
- ...(userConfig.copy
23
- ? userConfig.copy.map((item) => {
24
- if (typeof item === 'string') {
25
- return {
26
- from: (0, path_1.join)(cwd, item),
27
- to: item,
28
- };
29
- }
6
+ async function addCopyPlugin(opts) {
7
+ const { config, userConfig, cwd } = opts;
8
+ const copyPatterns = [
9
+ (0, fs_1.existsSync)((0, path_1.join)(cwd, 'public')) && {
10
+ from: (0, path_1.join)(cwd, 'public'),
11
+ },
12
+ ...(userConfig.copy
13
+ ? userConfig.copy.map((item) => {
14
+ if (typeof item === 'string') {
30
15
  return {
31
- // 相对于 process.cwd,所以这里需要使用绝对路径
32
- from: (0, path_1.join)(cwd, item.from),
33
- to: item.to,
16
+ from: (0, path_1.join)(cwd, item),
17
+ to: item,
34
18
  };
35
- })
36
- : []),
37
- ].filter(Boolean);
38
- if (copyPatterns.length) {
39
- config
40
- .plugin('copy')
41
- .use(require('@umijs/bundler-webpack/compiled/copy-webpack-plugin'), [
42
- {
43
- patterns: copyPatterns,
44
- },
45
- ]);
46
- }
47
- });
19
+ }
20
+ return {
21
+ // 相对于 process.cwd,所以这里需要使用绝对路径
22
+ from: (0, path_1.join)(cwd, item.from),
23
+ to: item.to,
24
+ };
25
+ })
26
+ : []),
27
+ ].filter(Boolean);
28
+ if (copyPatterns.length) {
29
+ config
30
+ .plugin('copy')
31
+ .use(require('@umijs/bundler-webpack/compiled/copy-webpack-plugin'), [
32
+ {
33
+ patterns: copyPatterns,
34
+ },
35
+ ]);
36
+ }
48
37
  }
49
38
  exports.addCopyPlugin = addCopyPlugin;