@umijs/bundler-webpack 4.0.0-beta.9 → 4.0.0-rc.3

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 (135) hide show
  1. package/compiled/autoprefixer/browserslist/index.d.ts +11 -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 +1 -1
  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 +36 -2
  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/express/index.js +57 -74
  28. package/compiled/fork-ts-checker-webpack-plugin/LICENSE +21 -0
  29. package/compiled/fork-ts-checker-webpack-plugin/fsevents.node +0 -0
  30. package/compiled/fork-ts-checker-webpack-plugin/index.js +48 -0
  31. package/compiled/fork-ts-checker-webpack-plugin/package.json +1 -0
  32. package/compiled/http-proxy-middleware/dist/types.d.ts +5 -5
  33. package/compiled/http-proxy-middleware/http-proxy/index.d.ts +1 -1
  34. package/compiled/http-proxy-middleware/index.js +10 -10
  35. package/compiled/mini-css-extract-plugin/hmr/hotModuleReplacement.js +60 -8
  36. package/compiled/mini-css-extract-plugin/hmr/normalize-url.js +13 -1
  37. package/compiled/mini-css-extract-plugin/index.js +8103 -7617
  38. package/compiled/mini-css-extract-plugin/loader.js +177 -54
  39. package/compiled/mini-css-extract-plugin/package.json +1 -1
  40. package/compiled/mini-css-extract-plugin/utils.js +83 -24
  41. package/compiled/postcss-loader/index.js +1 -1
  42. package/compiled/react-refresh/index.js +3 -3
  43. package/compiled/sass-loader/index.js +1 -1
  44. package/compiled/schema-utils/ajv/dist/ajv.d.ts +16 -0
  45. package/compiled/schema-utils/ajv/dist/compile/codegen/code.d.ts +39 -0
  46. package/compiled/schema-utils/ajv/dist/compile/codegen/index.d.ts +79 -0
  47. package/compiled/schema-utils/ajv/dist/compile/codegen/scope.d.ts +79 -0
  48. package/compiled/schema-utils/ajv/dist/compile/errors.d.ts +13 -0
  49. package/compiled/schema-utils/ajv/dist/compile/index.d.ts +80 -0
  50. package/compiled/schema-utils/ajv/dist/compile/ref_error.d.ts +5 -0
  51. package/compiled/schema-utils/ajv/dist/compile/resolve.d.ts +12 -0
  52. package/compiled/schema-utils/ajv/dist/compile/rules.d.ts +28 -0
  53. package/compiled/schema-utils/ajv/dist/compile/util.d.ts +40 -0
  54. package/compiled/schema-utils/ajv/dist/compile/validate/index.d.ts +42 -0
  55. package/compiled/schema-utils/ajv/dist/compile/validate/subschema.d.ts +47 -0
  56. package/compiled/schema-utils/ajv/dist/core.d.ts +171 -0
  57. package/compiled/schema-utils/ajv/dist/runtime/validation_error.d.ts +7 -0
  58. package/compiled/schema-utils/ajv/dist/types/index.d.ts +177 -0
  59. package/compiled/schema-utils/ajv/dist/types/json-schema.d.ts +124 -0
  60. package/compiled/schema-utils/ajv/dist/types/jtd-schema.d.ts +168 -0
  61. package/compiled/schema-utils/declarations/validate.d.ts +4 -4
  62. package/compiled/schema-utils/index.js +8 -2
  63. package/compiled/schema-utils/uri-js/dist/es5/uri.all.d.ts +59 -0
  64. package/compiled/style-loader/index.js +1 -1
  65. package/compiled/terser/index.js +1 -1
  66. package/compiled/terser-webpack-plugin/index.js +393 -517
  67. package/compiled/terser-webpack-plugin/jest-worker/build/types.d.ts +1 -0
  68. package/compiled/terser-webpack-plugin/minify.js +10 -3
  69. package/compiled/terser-webpack-plugin/package.json +1 -1
  70. package/compiled/terser-webpack-plugin/types/index.d.ts +183 -149
  71. package/compiled/terser-webpack-plugin/types/minify.d.ts +17 -0
  72. package/compiled/terser-webpack-plugin/types/utils.d.ts +10 -15
  73. package/compiled/terser-webpack-plugin/utils.js +118 -25
  74. package/compiled/webpack/index.js +10464 -5286
  75. package/compiled/webpack-dev-middleware/index.js +11 -5
  76. package/compiled/webpack-dev-middleware/package.json +1 -1
  77. package/compiled/webpack-manifest-plugin/LICENSE +21 -0
  78. package/compiled/webpack-manifest-plugin/index.js +1 -0
  79. package/compiled/webpack-manifest-plugin/package.json +1 -0
  80. package/compiled/webpack-sources/index.js +1 -1
  81. package/compiled/ws/index.d.ts +1 -2
  82. package/compiled/ws/index.js +1 -1
  83. package/dist/build.d.ts +5 -3
  84. package/dist/build.js +2 -1
  85. package/dist/cli.js +4 -7
  86. package/dist/config/assetRules.js +6 -14
  87. package/dist/config/compressPlugin.js +6 -0
  88. package/dist/config/config.d.ts +7 -2
  89. package/dist/config/config.js +49 -7
  90. package/dist/config/cssRules.js +1 -1
  91. package/dist/config/detectDeadCode.d.ts +12 -0
  92. package/dist/config/detectDeadCode.js +120 -0
  93. package/dist/config/detectDeadCodePlugin.d.ts +9 -0
  94. package/dist/config/detectDeadCodePlugin.js +71 -0
  95. package/dist/config/forkTSCheckerPlugin.d.ts +11 -0
  96. package/dist/config/forkTSCheckerPlugin.js +34 -0
  97. package/dist/config/harmonyLinkingErrorPlugin.d.ts +6 -0
  98. package/dist/config/harmonyLinkingErrorPlugin.js +42 -0
  99. package/dist/config/javaScriptRules.d.ts +1 -0
  100. package/dist/config/javaScriptRules.js +31 -20
  101. package/dist/config/manifestPlugin.d.ts +11 -0
  102. package/dist/config/manifestPlugin.js +25 -0
  103. package/dist/config/miniCSSExtractPlugin.d.ts +1 -0
  104. package/dist/config/miniCSSExtractPlugin.js +2 -3
  105. package/dist/config/svgRules.d.ts +1 -0
  106. package/dist/config/svgRules.js +17 -3
  107. package/dist/constants.d.ts +3 -0
  108. package/dist/constants.js +4 -1
  109. package/dist/dev.d.ts +4 -2
  110. package/dist/dev.js +31 -5
  111. package/dist/loader/svgr.d.ts +4 -0
  112. package/dist/loader/svgr.js +67 -0
  113. package/dist/loader/swc.d.ts +4 -0
  114. package/dist/loader/swc.js +79 -0
  115. package/dist/plugins/ParcelCSSMinifyPlugin.d.ts +10 -0
  116. package/dist/plugins/ParcelCSSMinifyPlugin.js +74 -0
  117. package/dist/plugins/RuntimePublicPathPlugin.d.ts +4 -0
  118. package/dist/plugins/RuntimePublicPathPlugin.js +20 -0
  119. package/dist/requireHook.js +1 -1
  120. package/dist/schema.js +14 -6
  121. package/dist/server/server.js +20 -1
  122. package/dist/swcPlugins/autoCSSModules.d.ts +7 -2
  123. package/dist/swcPlugins/autoCSSModules.js +13 -16
  124. package/dist/swcPlugins/lockCoreJS.d.ts +6 -0
  125. package/dist/swcPlugins/lockCoreJS.js +23 -0
  126. package/dist/types.d.ts +16 -0
  127. package/dist/types.js +1 -0
  128. package/dist/utils/depMatch.js +1 -1
  129. package/package.json +45 -39
  130. package/compiled/@svgr/webpack/LICENSE +0 -7
  131. package/compiled/@svgr/webpack/index.js +0 -266
  132. package/compiled/@svgr/webpack/package.json +0 -1
  133. package/compiled/swc-loader/LICENSE +0 -25
  134. package/compiled/swc-loader/index.js +0 -1
  135. package/compiled/swc-loader/package.json +0 -1
@@ -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);
@@ -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:\n`), ...unusedFiles.map((file, index) => `${index + 1}. ${utils_1.chalk.yellow(file)}\n`), utils_1.chalk.red.bold('Please 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,71 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.addDetectDeadCodePlugin = void 0;
32
+ const types_1 = require("../types");
33
+ const detectDeadCode_1 = __importStar(require("./detectDeadCode"));
34
+ const defaultOptions = {
35
+ patterns: [`!(${detectDeadCode_1.disabledFolders.join('|')})/**/*.*`],
36
+ exclude: [],
37
+ failOnHint: false,
38
+ detectUnusedFiles: true,
39
+ detectUnusedExport: true,
40
+ };
41
+ class DetectDeadCodePlugin {
42
+ constructor(options) {
43
+ this.options = defaultOptions;
44
+ this.handleAfterEmit = (compilation, callback) => {
45
+ (0, detectDeadCode_1.default)(compilation, this.options);
46
+ callback();
47
+ };
48
+ if (!options) {
49
+ return;
50
+ }
51
+ this.options = Object.assign(Object.assign({}, this.options), options);
52
+ }
53
+ apply(compiler) {
54
+ if (!this.options.context) {
55
+ this.options = Object.assign(Object.assign({}, this.options), { context: compiler.context });
56
+ }
57
+ compiler.hooks.afterEmit.tapAsync('DetectDeadCodePlugin', this.handleAfterEmit);
58
+ }
59
+ }
60
+ function addDetectDeadCodePlugin(opts) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ const { config, userConfig } = opts;
63
+ const isDev = opts.env === types_1.Env.development;
64
+ if (userConfig.deadCode && !isDev) {
65
+ config
66
+ .plugin('detect-dead-code-plugin')
67
+ .use(DetectDeadCodePlugin, [userConfig.deadCode]);
68
+ }
69
+ });
70
+ }
71
+ 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
  }
@@ -13,14 +13,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.addJavaScriptRules = void 0;
16
+ const mfsu_1 = require("@umijs/mfsu");
16
17
  const utils_1 = require("@umijs/utils");
18
+ const webpack_1 = require("../../compiled/webpack");
17
19
  const constants_1 = require("../constants");
18
20
  const autoCSSModules_1 = __importDefault(require("../swcPlugins/autoCSSModules"));
19
21
  const types_1 = require("../types");
20
22
  const depMatch_1 = require("../utils/depMatch");
21
23
  function addJavaScriptRules(opts) {
24
+ var _a, _b;
22
25
  return __awaiter(this, void 0, void 0, function* () {
23
- const { config, userConfig, cwd, env, name } = opts;
26
+ const { config, userConfig, cwd, name } = opts;
24
27
  const isDev = opts.env === types_1.Env.development;
25
28
  const useFastRefresh = isDev && userConfig.fastRefresh !== false && name !== constants_1.MFSU_NAME;
26
29
  const depPkgs = Object.assign({}, (0, depMatch_1.es5ImcompatibleVersionsToPkg)());
@@ -54,6 +57,9 @@ function addJavaScriptRules(opts) {
54
57
  })
55
58
  .end(),
56
59
  ];
60
+ if (userConfig.mdx) {
61
+ srcRules.push(config.module.rule('markdown').test(/\.mdx?$/));
62
+ }
57
63
  const depRules = [
58
64
  config.module
59
65
  .rule('dep')
@@ -112,35 +118,40 @@ function addJavaScriptRules(opts) {
112
118
  });
113
119
  }
114
120
  else if (srcTranspiler === types_1.Transpiler.swc) {
115
- // TODO: support javascript
116
121
  rule
117
122
  .use('swc-loader')
118
- .loader(require.resolve('../../compiled/swc-loader'))
123
+ .loader(require.resolve('../loader/swc'))
119
124
  .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
125
  plugin: (m) => new autoCSSModules_1.default().visitProgram(m),
138
126
  });
139
127
  }
128
+ else if (srcTranspiler === types_1.Transpiler.esbuild) {
129
+ rule
130
+ .use('esbuild-loader')
131
+ .loader(mfsu_1.esbuildLoader)
132
+ .options({
133
+ target: isDev ? 'esnext' : 'es2015',
134
+ handler: [mfsu_1.autoCssModulesHandler, ...opts.extraEsbuildLoaderHandler],
135
+ });
136
+ // esbuild loader can not auto import `React`
137
+ config.plugin('react-provide-plugin').use(webpack_1.ProvidePlugin, [
138
+ {
139
+ React: 'react',
140
+ },
141
+ ]);
142
+ }
140
143
  else {
141
144
  throw new Error(`Unsupported srcTranspiler ${srcTranspiler}.`);
142
145
  }
143
146
  });
147
+ if (userConfig.mdx) {
148
+ config.module
149
+ .rule('mdx')
150
+ .test(/\.mdx?$/)
151
+ .use('mdx-loader')
152
+ .loader((_a = userConfig.mdx) === null || _a === void 0 ? void 0 : _a.loader)
153
+ .options((_b = userConfig.mdx) === null || _b === void 0 ? void 0 : _b.loaderOptions);
154
+ }
144
155
  const depTranspiler = userConfig.depTranspiler || types_1.Transpiler.none;
145
156
  depRules.forEach((_rule) => {
146
157
  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 {};
@@ -15,11 +15,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.addMiniCSSExtractPlugin = void 0;
16
16
  // @ts-ignore
17
17
  const mini_css_extract_plugin_1 = __importDefault(require("@umijs/bundler-webpack/compiled/mini-css-extract-plugin"));
18
- const types_1 = require("../types");
19
18
  function addMiniCSSExtractPlugin(opts) {
20
19
  return __awaiter(this, void 0, void 0, function* () {
21
- const { config, userConfig, env } = opts;
22
- const hash = env !== types_1.Env.development && userConfig.hash ? '.[contenthash:8]' : '';
20
+ const { config, userConfig, useHash } = opts;
21
+ const hash = useHash ? '.[contenthash:8]' : '';
23
22
  if (!userConfig.styleLoader) {
24
23
  config.plugin('mini-css-extract-plugin').use(mini_css_extract_plugin_1.default, [
25
24
  {
@@ -6,6 +6,7 @@ interface IOpts {
6
6
  cwd: string;
7
7
  env: Env;
8
8
  browsers: any;
9
+ staticPathPrefix: string;
9
10
  }
10
11
  export declare function addSVGRules(opts: IOpts): Promise<void>;
11
12
  export {};