@umijs/bundler-webpack 4.0.0-beta.9 → 4.0.0-canary.20220317.2

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 (161) hide show
  1. package/compiled/autoprefixer/browserslist/index.d.ts +13 -7
  2. package/compiled/autoprefixer/index.js +3 -3
  3. package/compiled/autoprefixer/postcss/lib/at-rule.d.ts +1 -1
  4. package/compiled/autoprefixer/postcss/lib/comment.d.ts +1 -1
  5. package/compiled/autoprefixer/postcss/lib/container.d.ts +2 -2
  6. package/compiled/autoprefixer/postcss/lib/css-syntax-error.d.ts +55 -8
  7. package/compiled/autoprefixer/postcss/lib/declaration.d.ts +2 -2
  8. package/compiled/autoprefixer/postcss/lib/input.d.ts +26 -6
  9. package/compiled/autoprefixer/postcss/lib/lazy-result.d.ts +11 -10
  10. package/compiled/autoprefixer/postcss/lib/no-work-result.d.ts +37 -0
  11. package/compiled/autoprefixer/postcss/lib/node.d.ts +38 -4
  12. package/compiled/autoprefixer/postcss/lib/postcss.d.ts +31 -30
  13. package/compiled/autoprefixer/postcss/lib/processor.d.ts +5 -4
  14. package/compiled/autoprefixer/postcss/lib/result.d.ts +3 -2
  15. package/compiled/autoprefixer/postcss/lib/rule.d.ts +1 -1
  16. package/compiled/autoprefixer/postcss/lib/warning.d.ts +37 -3
  17. package/compiled/autoprefixer/source-map-js/source-map.d.ts +99 -82
  18. package/compiled/babel-loader/index.js +2 -2
  19. package/compiled/copy-webpack-plugin/576.index.js +1171 -0
  20. package/compiled/copy-webpack-plugin/index.js +16 -10
  21. package/compiled/copy-webpack-plugin/package.json +1 -1
  22. package/compiled/css-minimizer-webpack-plugin/index.js +8 -2
  23. package/compiled/css-minimizer-webpack-plugin/minify.js +25 -11
  24. package/compiled/css-minimizer-webpack-plugin/package.json +1 -1
  25. package/compiled/css-minimizer-webpack-plugin/utils.js +225 -28
  26. package/compiled/cssnano/index.js +12 -11
  27. package/compiled/cssnano/package.json +1 -1
  28. package/compiled/express.d.ts +2 -0
  29. package/compiled/fork-ts-checker-webpack-plugin/LICENSE +21 -0
  30. package/compiled/fork-ts-checker-webpack-plugin/fsevents.node +0 -0
  31. package/compiled/fork-ts-checker-webpack-plugin/index.js +48 -0
  32. package/compiled/fork-ts-checker-webpack-plugin/package.json +1 -0
  33. package/compiled/http-proxy-middleware/dist/types.d.ts +5 -5
  34. package/compiled/http-proxy-middleware/http-proxy/index.d.ts +1 -1
  35. package/compiled/http-proxy-middleware/index.js +10 -10
  36. package/compiled/mini-css-extract-plugin/hmr/hotModuleReplacement.js +60 -8
  37. package/compiled/mini-css-extract-plugin/hmr/normalize-url.js +13 -1
  38. package/compiled/mini-css-extract-plugin/index.js +7970 -7445
  39. package/compiled/mini-css-extract-plugin/loader.js +179 -54
  40. package/compiled/mini-css-extract-plugin/package.json +1 -1
  41. package/compiled/mini-css-extract-plugin/utils.js +85 -24
  42. package/compiled/postcss-loader/index.js +1 -1
  43. package/compiled/react-refresh/index.js +3 -3
  44. package/compiled/sass-loader/index.js +1 -1
  45. package/compiled/schema-utils/ajv/dist/ajv.d.ts +16 -0
  46. package/compiled/schema-utils/ajv/dist/compile/codegen/code.d.ts +39 -0
  47. package/compiled/schema-utils/ajv/dist/compile/codegen/index.d.ts +79 -0
  48. package/compiled/schema-utils/ajv/dist/compile/codegen/scope.d.ts +79 -0
  49. package/compiled/schema-utils/ajv/dist/compile/errors.d.ts +13 -0
  50. package/compiled/schema-utils/ajv/dist/compile/index.d.ts +80 -0
  51. package/compiled/schema-utils/ajv/dist/compile/ref_error.d.ts +5 -0
  52. package/compiled/schema-utils/ajv/dist/compile/resolve.d.ts +12 -0
  53. package/compiled/schema-utils/ajv/dist/compile/rules.d.ts +28 -0
  54. package/compiled/schema-utils/ajv/dist/compile/util.d.ts +40 -0
  55. package/compiled/schema-utils/ajv/dist/compile/validate/index.d.ts +42 -0
  56. package/compiled/schema-utils/ajv/dist/compile/validate/subschema.d.ts +47 -0
  57. package/compiled/schema-utils/ajv/dist/core.d.ts +171 -0
  58. package/compiled/schema-utils/ajv/dist/runtime/validation_error.d.ts +7 -0
  59. package/compiled/schema-utils/ajv/dist/types/index.d.ts +177 -0
  60. package/compiled/schema-utils/ajv/dist/types/json-schema.d.ts +124 -0
  61. package/compiled/schema-utils/ajv/dist/types/jtd-schema.d.ts +168 -0
  62. package/compiled/schema-utils/declarations/validate.d.ts +4 -4
  63. package/compiled/schema-utils/index.js +8 -2
  64. package/compiled/schema-utils/uri-js/dist/es5/uri.all.d.ts +59 -0
  65. package/compiled/style-loader/index.js +1 -1
  66. package/compiled/terser/index.js +1 -1
  67. package/compiled/terser-webpack-plugin/index.js +393 -517
  68. package/compiled/terser-webpack-plugin/jest-worker/build/types.d.ts +1 -0
  69. package/compiled/terser-webpack-plugin/minify.js +10 -3
  70. package/compiled/terser-webpack-plugin/package.json +1 -1
  71. package/compiled/terser-webpack-plugin/types/index.d.ts +183 -149
  72. package/compiled/terser-webpack-plugin/types/minify.d.ts +17 -0
  73. package/compiled/terser-webpack-plugin/types/utils.d.ts +10 -15
  74. package/compiled/terser-webpack-plugin/utils.js +118 -25
  75. package/compiled/webpack/index.js +11665 -5565
  76. package/compiled/webpack-dev-middleware/index.js +11 -5
  77. package/compiled/webpack-dev-middleware/package.json +1 -1
  78. package/compiled/webpack-manifest-plugin/LICENSE +21 -0
  79. package/compiled/webpack-manifest-plugin/index.js +1 -0
  80. package/compiled/webpack-manifest-plugin/package.json +1 -0
  81. package/compiled/webpack-sources/index.js +1 -1
  82. package/compiled/ws/index.d.ts +1 -2
  83. package/compiled/ws/index.js +1 -1
  84. package/dist/build.d.ts +5 -3
  85. package/dist/build.js +2 -1
  86. package/dist/cli.js +4 -7
  87. package/dist/client/client.js +5 -1
  88. package/dist/config/assetRules.js +6 -14
  89. package/dist/config/compressPlugin.js +6 -0
  90. package/dist/config/config.d.ts +7 -2
  91. package/dist/config/config.js +49 -7
  92. package/dist/config/cssRules.js +2 -2
  93. package/dist/config/detectDeadCode.d.ts +12 -0
  94. package/dist/config/detectDeadCode.js +120 -0
  95. package/dist/config/detectDeadCodePlugin.d.ts +9 -0
  96. package/dist/config/detectDeadCodePlugin.js +75 -0
  97. package/dist/config/forkTSCheckerPlugin.d.ts +11 -0
  98. package/dist/config/forkTSCheckerPlugin.js +34 -0
  99. package/dist/config/harmonyLinkingErrorPlugin.d.ts +6 -0
  100. package/dist/config/harmonyLinkingErrorPlugin.js +42 -0
  101. package/dist/config/javaScriptRules.d.ts +1 -0
  102. package/dist/config/javaScriptRules.js +33 -25
  103. package/dist/config/manifestPlugin.d.ts +11 -0
  104. package/dist/config/manifestPlugin.js +25 -0
  105. package/dist/config/miniCSSExtractPlugin.d.ts +1 -0
  106. package/dist/config/miniCSSExtractPlugin.js +2 -3
  107. package/dist/config/svgRules.d.ts +1 -0
  108. package/dist/config/svgRules.js +17 -3
  109. package/dist/constants.d.ts +3 -0
  110. package/dist/constants.js +4 -1
  111. package/dist/dev.d.ts +4 -2
  112. package/dist/dev.js +31 -5
  113. package/dist/index.d.ts +4 -1
  114. package/dist/index.js +5 -1
  115. package/dist/loader/svgr.d.ts +4 -0
  116. package/dist/loader/svgr.js +67 -0
  117. package/dist/loader/swc.d.ts +4 -0
  118. package/dist/loader/swc.js +79 -0
  119. package/dist/plugins/ESBuildCSSMinifyPlugin.js +1 -3
  120. package/dist/plugins/ParcelCSSMinifyPlugin.d.ts +10 -0
  121. package/dist/plugins/ParcelCSSMinifyPlugin.js +75 -0
  122. package/dist/plugins/ProgressPlugin.js +2 -2
  123. package/dist/plugins/RuntimePublicPathPlugin.d.ts +4 -0
  124. package/dist/plugins/RuntimePublicPathPlugin.js +20 -0
  125. package/dist/requireHook.js +1 -1
  126. package/dist/schema.js +17 -9
  127. package/dist/server/server.js +25 -3
  128. package/dist/swcPlugins/autoCSSModules.d.ts +7 -2
  129. package/dist/swcPlugins/autoCSSModules.js +13 -16
  130. package/dist/swcPlugins/lockCoreJS.d.ts +6 -0
  131. package/dist/swcPlugins/lockCoreJS.js +23 -0
  132. package/dist/types.d.ts +16 -0
  133. package/dist/types.js +1 -0
  134. package/dist/utils/depMatch.js +1 -1
  135. package/package.json +48 -47
  136. package/compiled/@svgr/webpack/LICENSE +0 -7
  137. package/compiled/@svgr/webpack/index.js +0 -266
  138. package/compiled/@svgr/webpack/package.json +0 -1
  139. package/compiled/css-loader/LICENSE +0 -20
  140. package/compiled/css-loader/api.js +0 -102
  141. package/compiled/css-loader/getUrl.js +0 -29
  142. package/compiled/css-loader/index.js +0 -2
  143. package/compiled/css-loader/noSourceMaps.js +0 -5
  144. package/compiled/css-loader/package.json +0 -1
  145. package/compiled/css-loader/sourceMaps.js +0 -22
  146. package/compiled/express/LICENSE +0 -24
  147. package/compiled/express/body-parser/index.d.ts +0 -104
  148. package/compiled/express/connect/index.d.ts +0 -93
  149. package/compiled/express/express-serve-static-core/index.d.ts +0 -1252
  150. package/compiled/express/index.d.ts +0 -133
  151. package/compiled/express/index.js +0 -338
  152. package/compiled/express/mime/index.d.ts +0 -35
  153. package/compiled/express/package.json +0 -1
  154. package/compiled/express/qs/index.d.ts +0 -62
  155. package/compiled/express/range-parser/index.d.ts +0 -35
  156. package/compiled/express/serve-static/index.d.ts +0 -108
  157. package/compiled/less/index.js +0 -31
  158. package/compiled/less/package.json +0 -1
  159. package/compiled/swc-loader/LICENSE +0 -25
  160. package/compiled/swc-loader/index.js +0 -1
  161. package/compiled/swc-loader/package.json +0 -1
@@ -17,6 +17,7 @@ exports.addCompressPlugin = void 0;
17
17
  const css_minimizer_webpack_plugin_1 = __importDefault(require("@umijs/bundler-webpack/compiled/css-minimizer-webpack-plugin"));
18
18
  const terser_webpack_plugin_1 = __importDefault(require("../../compiled/terser-webpack-plugin"));
19
19
  const ESBuildCSSMinifyPlugin_1 = __importDefault(require("../plugins/ESBuildCSSMinifyPlugin"));
20
+ const ParcelCSSMinifyPlugin_1 = require("../plugins/ParcelCSSMinifyPlugin");
20
21
  const types_1 = require("../types");
21
22
  function addCompressPlugin(opts) {
22
23
  return __awaiter(this, void 0, void 0, function* () {
@@ -69,6 +70,11 @@ function addCompressPlugin(opts) {
69
70
  },
70
71
  ]);
71
72
  }
73
+ else if (cssMinifier === types_1.CSSMinifier.parcelCSS) {
74
+ config.optimization
75
+ .minimizer(`css-${cssMinifier}`)
76
+ .use(ParcelCSSMinifyPlugin_1.ParcelCSSMinifyPlugin);
77
+ }
72
78
  else if (cssMinifier !== types_1.CSSMinifier.none) {
73
79
  throw new Error(`Unsupported cssMinifier ${userConfig.cssMinifier}.`);
74
80
  }
@@ -1,11 +1,12 @@
1
1
  import { Configuration } from '../../compiled/webpack';
2
2
  import { Env, IConfig } from '../types';
3
- interface IOpts {
3
+ export interface IOpts {
4
4
  cwd: string;
5
5
  env: Env;
6
6
  entry: Record<string, string>;
7
7
  extraBabelPresets?: any[];
8
8
  extraBabelPlugins?: any[];
9
+ extraEsbuildLoaderHandler?: any[];
9
10
  babelPreset?: any;
10
11
  chainWebpack?: Function;
11
12
  modifyWebpackConfig?: Function;
@@ -15,6 +16,10 @@ interface IOpts {
15
16
  userConfig: IConfig;
16
17
  analyze?: any;
17
18
  name?: string;
19
+ cache?: {
20
+ absNodeModulesPath?: string;
21
+ buildDependencies?: string[];
22
+ cacheDirectory?: string;
23
+ };
18
24
  }
19
25
  export declare function getConfig(opts: IOpts): Promise<Configuration>;
20
- export {};
@@ -17,6 +17,7 @@ const path_1 = require("path");
17
17
  const webpack_1 = __importDefault(require("../../compiled/webpack"));
18
18
  const webpack_5_chain_1 = __importDefault(require("../../compiled/webpack-5-chain"));
19
19
  const constants_1 = require("../constants");
20
+ const RuntimePublicPathPlugin_1 = require("../plugins/RuntimePublicPathPlugin");
20
21
  const types_1 = require("../types");
21
22
  const browsersList_1 = require("../utils/browsersList");
22
23
  const assetRules_1 = require("./assetRules");
@@ -25,9 +26,13 @@ const compressPlugin_1 = require("./compressPlugin");
25
26
  const copyPlugin_1 = require("./copyPlugin");
26
27
  const cssRules_1 = require("./cssRules");
27
28
  const definePlugin_1 = require("./definePlugin");
29
+ const detectDeadCodePlugin_1 = require("./detectDeadCodePlugin");
28
30
  const fastRefreshPlugin_1 = require("./fastRefreshPlugin");
31
+ const forkTSCheckerPlugin_1 = require("./forkTSCheckerPlugin");
32
+ const harmonyLinkingErrorPlugin_1 = require("./harmonyLinkingErrorPlugin");
29
33
  const ignorePlugin_1 = require("./ignorePlugin");
30
34
  const javaScriptRules_1 = require("./javaScriptRules");
35
+ const manifestPlugin_1 = require("./manifestPlugin");
31
36
  const miniCSSExtractPlugin_1 = require("./miniCSSExtractPlugin");
32
37
  const nodePolyfill_1 = require("./nodePolyfill");
33
38
  const progressPlugin_1 = require("./progressPlugin");
@@ -38,9 +43,8 @@ function getConfig(opts) {
38
43
  const { userConfig } = opts;
39
44
  const isDev = opts.env === types_1.Env.development;
40
45
  const config = new webpack_5_chain_1.default();
41
- userConfig.targets = userConfig.targets || {
42
- chrome: 80,
43
- };
46
+ userConfig.targets || (userConfig.targets = constants_1.DEFAULT_BROWSER_TARGETS);
47
+ const useHash = !!(opts.hash || (userConfig.hash && !isDev));
44
48
  const applyOpts = {
45
49
  name: opts.name,
46
50
  config,
@@ -50,9 +54,11 @@ function getConfig(opts) {
50
54
  babelPreset: opts.babelPreset,
51
55
  extraBabelPlugins: opts.extraBabelPlugins || [],
52
56
  extraBabelPresets: opts.extraBabelPresets || [],
57
+ extraEsbuildLoaderHandler: opts.extraEsbuildLoaderHandler || [],
53
58
  browsers: (0, browsersList_1.getBrowsersList)({
54
59
  targets: userConfig.targets,
55
60
  }),
61
+ useHash,
56
62
  staticPathPrefix: opts.staticPathPrefix !== undefined ? opts.staticPathPrefix : 'static/',
57
63
  };
58
64
  // mode
@@ -61,7 +67,6 @@ function getConfig(opts) {
61
67
  // entry
62
68
  Object.keys(opts.entry).forEach((key) => {
63
69
  const entry = config.entry(key);
64
- // TODO: runtimePublicPath
65
70
  if (isDev && opts.hmr) {
66
71
  entry.add(require.resolve('../../client/client/client'));
67
72
  }
@@ -75,7 +80,6 @@ function getConfig(opts) {
75
80
  : userConfig.devtool);
76
81
  // output
77
82
  const absOutputPath = (0, path_1.join)(opts.cwd, userConfig.outputPath || constants_1.DEFAULT_OUTPUT_PATH);
78
- const useHash = opts.hash || (userConfig.hash && !isDev);
79
83
  const disableCompress = process.env.COMPRESS === 'none';
80
84
  config.output
81
85
  .path(absOutputPath)
@@ -131,10 +135,14 @@ function getConfig(opts) {
131
135
  yield (0, fastRefreshPlugin_1.addFastRefreshPlugin)(applyOpts);
132
136
  // progress
133
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);
134
142
  // copy
135
143
  yield (0, copyPlugin_1.addCopyPlugin)(applyOpts);
136
- // TODO: friendly-error
137
- // TODO: manifest
144
+ // manifest
145
+ yield (0, manifestPlugin_1.addManifestPlugin)(applyOpts);
138
146
  // hmr
139
147
  if (isDev && opts.hmr) {
140
148
  config.plugin('hmr').use(webpack_1.default.HotModuleReplacementPlugin);
@@ -143,6 +151,40 @@ function getConfig(opts) {
143
151
  yield (0, compressPlugin_1.addCompressPlugin)(applyOpts);
144
152
  // purgecss
145
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);
159
+ }
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'),
170
+ });
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
183
+ ? {
184
+ debug: /webpack\.cache/,
185
+ }
186
+ : {})));
187
+ }
146
188
  // analyzer
147
189
  if (opts.analyze) {
148
190
  yield (0, bundleAnalyzerPlugin_1.addBundleAnalyzerPlugin)(applyOpts);
@@ -20,7 +20,7 @@ function addCSSRules(opts) {
20
20
  test: /\.less(\?.*)?$/,
21
21
  loader: require.resolve('@umijs/bundler-webpack/compiled/less-loader'),
22
22
  loaderOptions: {
23
- implementation: require.resolve('@umijs/bundler-webpack/compiled/less'),
23
+ implementation: require.resolve('@umijs/bundler-utils/compiled/less'),
24
24
  lessOptions: Object.assign({ modifyVars: userConfig.theme, javascriptEnabled: true }, userConfig.lessLoader),
25
25
  },
26
26
  },
@@ -56,7 +56,7 @@ function addCSSRules(opts) {
56
56
  }
57
57
  else {
58
58
  rule
59
- .use('mini-css-extract-plugin-loader')
59
+ .use('mini-css-extract-plugin')
60
60
  .loader(require.resolve('@umijs/bundler-webpack/compiled/mini-css-extract-plugin/loader'))
61
61
  .options({
62
62
  publicPath: './',
@@ -0,0 +1,12 @@
1
+ import { Compilation } from '@umijs/bundler-webpack/compiled/webpack';
2
+ import { DeadCodeParams } from '../types';
3
+ export interface Options extends DeadCodeParams {
4
+ patterns: string[];
5
+ exclude: string[];
6
+ failOnHint: boolean;
7
+ detectUnusedFiles: boolean;
8
+ detectUnusedExport: boolean;
9
+ }
10
+ export declare const disabledFolders: string[];
11
+ declare const detectDeadCode: (compilation: Compilation, options: Options) => void;
12
+ export default detectDeadCode;
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.disabledFolders = void 0;
7
+ const webpack_1 = require("@umijs/bundler-webpack/compiled/webpack");
8
+ const utils_1 = require("@umijs/utils");
9
+ const path_1 = __importDefault(require("path"));
10
+ exports.disabledFolders = [
11
+ 'node_modules',
12
+ '.umi',
13
+ '.umi-production',
14
+ 'dist',
15
+ ];
16
+ const detectDeadCode = (compilation, options) => {
17
+ const assets = getWebpackAssets(compilation);
18
+ const compiledFilesDictionary = convertFilesToDict(assets);
19
+ const includedFiles = getPattern(options)
20
+ .map((pattern) => utils_1.glob.sync(pattern))
21
+ .flat();
22
+ const unusedFiles = options.detectUnusedFiles
23
+ ? includedFiles.filter((file) => !compiledFilesDictionary[file])
24
+ : [];
25
+ const unusedExportMap = options.detectUnusedExport
26
+ ? getUnusedExportMap(convertFilesToDict(includedFiles), compilation)
27
+ : {};
28
+ logUnusedFiles(unusedFiles);
29
+ logUnusedExportMap(unusedExportMap);
30
+ const hasUnusedThings = unusedFiles.length || Object.keys(unusedExportMap).length;
31
+ if (hasUnusedThings && options.failOnHint) {
32
+ process.exit(2);
33
+ }
34
+ };
35
+ const getPattern = (options) => {
36
+ return options.patterns
37
+ .map((pattern) => path_1.default.resolve(options.context || '', pattern))
38
+ .concat(options.exclude.map((pattern) => path_1.default.resolve(options.context || '', `!${pattern}`)))
39
+ .map(convertToUnixPath);
40
+ };
41
+ const getUnusedExportMap = (includedFileMap, compilation) => {
42
+ const unusedExportMap = {};
43
+ compilation.chunks.forEach((chunk) => {
44
+ compilation.chunkGraph.getChunkModules(chunk).forEach((module) => {
45
+ outputUnusedExportMap(compilation, chunk, module, includedFileMap, unusedExportMap);
46
+ });
47
+ });
48
+ return unusedExportMap;
49
+ };
50
+ const outputUnusedExportMap = (compilation, chunk, module, includedFileMap, unusedExportMap) => {
51
+ if (!(module instanceof webpack_1.NormalModule) || !module.resource) {
52
+ return;
53
+ }
54
+ const path = convertToUnixPath(module.resource);
55
+ if (!/^((?!(node_modules)).)*$/.test(path))
56
+ return;
57
+ const providedExports = compilation.chunkGraph.moduleGraph.getProvidedExports(module);
58
+ const usedExports = compilation.chunkGraph.moduleGraph.getUsedExports(module, chunk.runtime);
59
+ if (usedExports !== true &&
60
+ providedExports !== true &&
61
+ includedFileMap[path]) {
62
+ if (usedExports === false) {
63
+ if (providedExports === null || providedExports === void 0 ? void 0 : providedExports.length) {
64
+ unusedExportMap[path] = providedExports;
65
+ }
66
+ }
67
+ else if (providedExports instanceof Array) {
68
+ const unusedExports = providedExports.filter((item) => usedExports && !usedExports.has(item));
69
+ if (unusedExports.length) {
70
+ unusedExportMap[path] = unusedExports;
71
+ }
72
+ }
73
+ }
74
+ };
75
+ const logUnusedExportMap = (unusedExportMap) => {
76
+ if (!Object.keys(unusedExportMap).length) {
77
+ return;
78
+ }
79
+ let numberOfUnusedExport = 0;
80
+ let logStr = '';
81
+ Object.keys(unusedExportMap).forEach((filePath, fileIndex) => {
82
+ const unusedExports = unusedExportMap[filePath];
83
+ logStr += [
84
+ `\n${fileIndex + 1}. `,
85
+ utils_1.chalk.yellow(`${filePath}\n`),
86
+ ' >>> ',
87
+ utils_1.chalk.yellow(`${unusedExports.join(', ')}`),
88
+ ].join('');
89
+ numberOfUnusedExport += unusedExports.length;
90
+ });
91
+ console.log(utils_1.chalk.yellow.bold('\nWarning:'), utils_1.chalk.yellow(`There are ${numberOfUnusedExport} unused exports in ${Object.keys(unusedExportMap).length} files:`), logStr, utils_1.chalk.red.bold('\nPlease be careful if you want to remove them (¬º-°)¬.\n'));
92
+ };
93
+ const getWebpackAssets = (compilation) => {
94
+ const outputPath = compilation.getPath(compilation.compiler.outputPath);
95
+ const assets = [
96
+ ...Array.from(compilation.fileDependencies),
97
+ ...compilation
98
+ .getAssets()
99
+ .map((asset) => path_1.default.join(outputPath, asset.name)),
100
+ ];
101
+ return assets;
102
+ };
103
+ const convertFilesToDict = (assets) => {
104
+ return assets
105
+ .filter((file) => Boolean(file) &&
106
+ exports.disabledFolders.every((disabledPath) => !file.includes(disabledPath)))
107
+ .reduce((fileDictionary, file) => {
108
+ const unixFile = convertToUnixPath(file);
109
+ fileDictionary[unixFile] = true;
110
+ return fileDictionary;
111
+ }, {});
112
+ };
113
+ const logUnusedFiles = (unusedFiles) => {
114
+ if (!(unusedFiles === null || unusedFiles === void 0 ? void 0 : unusedFiles.length)) {
115
+ return;
116
+ }
117
+ console.log(utils_1.chalk.yellow.bold('\nWarning:'), utils_1.chalk.yellow(`There are ${unusedFiles.length} unused files:`), ...unusedFiles.map((file, index) => `\n${index + 1}. ${utils_1.chalk.yellow(file)}`), utils_1.chalk.red.bold('\nPlease be careful if you want to remove them (¬º-°)¬.\n'));
118
+ };
119
+ const convertToUnixPath = (path) => path.replace(/\\+/g, '/');
120
+ exports.default = detectDeadCode;
@@ -0,0 +1,9 @@
1
+ import Config from '@umijs/bundler-webpack/compiled/webpack-5-chain';
2
+ import { Env, IConfig } from '../types';
3
+ interface IOpts {
4
+ userConfig: IConfig;
5
+ config: Config;
6
+ env: Env;
7
+ }
8
+ export declare function addDetectDeadCodePlugin(opts: IOpts): Promise<void>;
9
+ export {};
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
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
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.addDetectDeadCodePlugin = void 0;
36
+ const types_1 = require("../types");
37
+ const detectDeadCode_1 = __importStar(require("./detectDeadCode"));
38
+ const defaultOptions = {
39
+ patterns: [`!(${detectDeadCode_1.disabledFolders.join('|')})/**/*.*`],
40
+ exclude: [],
41
+ failOnHint: false,
42
+ detectUnusedFiles: true,
43
+ detectUnusedExport: true,
44
+ };
45
+ class DetectDeadCodePlugin {
46
+ constructor(options) {
47
+ this.options = defaultOptions;
48
+ this.handleAfterEmit = (compilation, callback) => {
49
+ (0, detectDeadCode_1.default)(compilation, this.options);
50
+ callback();
51
+ };
52
+ if (!options) {
53
+ return;
54
+ }
55
+ this.options = Object.assign(Object.assign({}, this.options), options);
56
+ }
57
+ apply(compiler) {
58
+ if (!this.options.context) {
59
+ this.options = Object.assign(Object.assign({}, this.options), { context: compiler.context });
60
+ }
61
+ compiler.hooks.afterEmit.tapAsync('DetectDeadCodePlugin', this.handleAfterEmit);
62
+ }
63
+ }
64
+ function addDetectDeadCodePlugin(opts) {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ const { config, userConfig } = opts;
67
+ const isDev = opts.env === types_1.Env.development;
68
+ if (userConfig.deadCode && !isDev) {
69
+ config
70
+ .plugin('detect-dead-code-plugin')
71
+ .use(DetectDeadCodePlugin, [userConfig.deadCode]);
72
+ }
73
+ });
74
+ }
75
+ exports.addDetectDeadCodePlugin = addDetectDeadCodePlugin;
@@ -0,0 +1,11 @@
1
+ import Config from '@umijs/bundler-webpack/compiled/webpack-5-chain';
2
+ import { Env, IConfig } from '../types';
3
+ interface IOpts {
4
+ name?: string;
5
+ config: Config;
6
+ userConfig: IConfig;
7
+ cwd: string;
8
+ env: Env;
9
+ }
10
+ export declare function addForkTSCheckerPlugin(opts: IOpts): Promise<void>;
11
+ export {};
@@ -0,0 +1,34 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.addForkTSCheckerPlugin = void 0;
16
+ // @ts-ignore
17
+ const fork_ts_checker_webpack_plugin_1 = __importDefault(require("@umijs/bundler-webpack/compiled/fork-ts-checker-webpack-plugin"));
18
+ function addForkTSCheckerPlugin(opts) {
19
+ var _a;
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ const { config, userConfig } = opts;
22
+ if (userConfig.forkTSChecker) {
23
+ // use user's typescript
24
+ if ((_a = userConfig.forkTSChecker.typescript) === null || _a === void 0 ? void 0 : _a.enable) {
25
+ userConfig.forkTSChecker.typescript.typescriptPath =
26
+ require.resolve('typescript');
27
+ }
28
+ config
29
+ .plugin('fork-ts-checker-plugin')
30
+ .use(fork_ts_checker_webpack_plugin_1.default, [userConfig.forkTSChecker]);
31
+ }
32
+ });
33
+ }
34
+ exports.addForkTSCheckerPlugin = addForkTSCheckerPlugin;
@@ -0,0 +1,6 @@
1
+ import Config from '@umijs/bundler-webpack/compiled/webpack-5-chain';
2
+ interface IOpts {
3
+ config: Config;
4
+ }
5
+ export declare function addHarmonyLinkingErrorPlugin(opts: IOpts): Promise<void>;
6
+ export {};
@@ -0,0 +1,42 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.addHarmonyLinkingErrorPlugin = void 0;
13
+ // ref: https://github.com/webpack/webpack/blob/ccecc17c01af96edddb931a76e7a3b21ef2969d8/lib/dependencies/HarmonyImportDependency.js#L164
14
+ const LINKING_ERROR_TAG = 'was not found in';
15
+ // build 时会出现 css modules 的引用警告,但这应该是需要忽略的
16
+ const CSS_NO_EXPORTS = /\.(css|sass|scss|styl|less)' \(module has no exports\)/;
17
+ class HarmonyLinkingErrorPlugin {
18
+ apply(compiler) {
19
+ compiler.hooks.afterCompile.tap('HarmonyLinkingErrorPlugin', (compilation) => {
20
+ if (!compilation.warnings.length) {
21
+ return;
22
+ }
23
+ const harmonyLinkingErrors = compilation.warnings.filter((w) => {
24
+ return (w.name === 'ModuleDependencyWarning' &&
25
+ !w.module.resource.includes('node_modules') &&
26
+ w.message.includes(LINKING_ERROR_TAG) &&
27
+ !CSS_NO_EXPORTS.test(w.message));
28
+ });
29
+ if (!harmonyLinkingErrors.length) {
30
+ return;
31
+ }
32
+ compilation.errors.push(...harmonyLinkingErrors);
33
+ });
34
+ }
35
+ }
36
+ function addHarmonyLinkingErrorPlugin(opts) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ const { config } = opts;
39
+ config.plugin('harmony-linking-error-plugin').use(HarmonyLinkingErrorPlugin);
40
+ });
41
+ }
42
+ exports.addHarmonyLinkingErrorPlugin = addHarmonyLinkingErrorPlugin;
@@ -7,6 +7,7 @@ interface IOpts {
7
7
  env: Env;
8
8
  extraBabelPlugins: any[];
9
9
  extraBabelPresets: any[];
10
+ extraEsbuildLoaderHandler: any[];
10
11
  babelPreset: any;
11
12
  name?: string;
12
13
  }
@@ -8,19 +8,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
11
  Object.defineProperty(exports, "__esModule", { value: true });
15
12
  exports.addJavaScriptRules = void 0;
13
+ const mfsu_1 = require("@umijs/mfsu");
16
14
  const utils_1 = require("@umijs/utils");
15
+ const webpack_1 = require("../../compiled/webpack");
17
16
  const constants_1 = require("../constants");
18
- const autoCSSModules_1 = __importDefault(require("../swcPlugins/autoCSSModules"));
19
17
  const types_1 = require("../types");
20
18
  const depMatch_1 = require("../utils/depMatch");
21
19
  function addJavaScriptRules(opts) {
20
+ var _a, _b;
22
21
  return __awaiter(this, void 0, void 0, function* () {
23
- const { config, userConfig, cwd, env, name } = opts;
22
+ const { config, userConfig, cwd, name } = opts;
24
23
  const isDev = opts.env === types_1.Env.development;
25
24
  const useFastRefresh = isDev && userConfig.fastRefresh !== false && name !== constants_1.MFSU_NAME;
26
25
  const depPkgs = Object.assign({}, (0, depMatch_1.es5ImcompatibleVersionsToPkg)());
@@ -54,6 +53,9 @@ function addJavaScriptRules(opts) {
54
53
  })
55
54
  .end(),
56
55
  ];
56
+ if (userConfig.mdx) {
57
+ srcRules.push(config.module.rule('markdown').test(/\.mdx?$/));
58
+ }
57
59
  const depRules = [
58
60
  config.module
59
61
  .rule('dep')
@@ -112,35 +114,41 @@ function addJavaScriptRules(opts) {
112
114
  });
113
115
  }
114
116
  else if (srcTranspiler === types_1.Transpiler.swc) {
115
- // TODO: support javascript
117
+ const AutoCSSModule = require('../swcPlugins/autoCSSModules').default;
116
118
  rule
117
119
  .use('swc-loader')
118
- .loader(require.resolve('../../compiled/swc-loader'))
120
+ .loader(require.resolve('../loader/swc'))
119
121
  .options({
120
- jsc: {
121
- parser: {
122
- syntax: 'typescript',
123
- dynamicImport: true,
124
- tsx: true,
125
- },
126
- transform: {
127
- react: {
128
- runtime: 'automatic',
129
- pragma: 'React.createElement',
130
- pragmaFrag: 'React.Fragment',
131
- throwIfNamespace: true,
132
- development: env === types_1.Env.development,
133
- useBuiltins: true,
134
- },
135
- },
136
- },
137
- plugin: (m) => new autoCSSModules_1.default().visitProgram(m),
122
+ plugin: (m) => new AutoCSSModule().visitProgram(m),
138
123
  });
139
124
  }
125
+ else if (srcTranspiler === types_1.Transpiler.esbuild) {
126
+ rule
127
+ .use('esbuild-loader')
128
+ .loader(mfsu_1.esbuildLoader)
129
+ .options({
130
+ target: isDev ? 'esnext' : 'es2015',
131
+ handler: [mfsu_1.autoCssModulesHandler, ...opts.extraEsbuildLoaderHandler],
132
+ });
133
+ // esbuild loader can not auto import `React`
134
+ config.plugin('react-provide-plugin').use(webpack_1.ProvidePlugin, [
135
+ {
136
+ React: 'react',
137
+ },
138
+ ]);
139
+ }
140
140
  else {
141
141
  throw new Error(`Unsupported srcTranspiler ${srcTranspiler}.`);
142
142
  }
143
143
  });
144
+ if (userConfig.mdx) {
145
+ config.module
146
+ .rule('mdx')
147
+ .test(/\.mdx?$/)
148
+ .use('mdx-loader')
149
+ .loader((_a = userConfig.mdx) === null || _a === void 0 ? void 0 : _a.loader)
150
+ .options((_b = userConfig.mdx) === null || _b === void 0 ? void 0 : _b.loaderOptions);
151
+ }
144
152
  const depTranspiler = userConfig.depTranspiler || types_1.Transpiler.none;
145
153
  depRules.forEach((_rule) => {
146
154
  if (depTranspiler === types_1.Transpiler.none) {
@@ -0,0 +1,11 @@
1
+ import Config from '@umijs/bundler-webpack/compiled/webpack-5-chain';
2
+ import { Env, IConfig } from '../types';
3
+ interface IOpts {
4
+ name?: string;
5
+ config: Config;
6
+ userConfig: IConfig;
7
+ cwd: string;
8
+ env: Env;
9
+ }
10
+ export declare function addManifestPlugin(opts: IOpts): Promise<void>;
11
+ export {};
@@ -0,0 +1,25 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.addManifestPlugin = void 0;
13
+ // @ts-ignore
14
+ const webpack_manifest_plugin_1 = require("@umijs/bundler-webpack/compiled/webpack-manifest-plugin");
15
+ function addManifestPlugin(opts) {
16
+ return __awaiter(this, void 0, void 0, function* () {
17
+ const { config, userConfig } = opts;
18
+ if (userConfig.manifest) {
19
+ config.plugin('manifest-plugin').use(webpack_manifest_plugin_1.WebpackManifestPlugin, [
20
+ Object.assign({ fileName: 'asset-manifest.json' }, userConfig.manifest),
21
+ ]);
22
+ }
23
+ });
24
+ }
25
+ exports.addManifestPlugin = addManifestPlugin;
@@ -5,6 +5,7 @@ interface IOpts {
5
5
  userConfig: IConfig;
6
6
  cwd: string;
7
7
  env: Env;
8
+ useHash: boolean;
8
9
  }
9
10
  export declare function addMiniCSSExtractPlugin(opts: IOpts): Promise<void>;
10
11
  export {};