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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/build.js +48 -59
  2. package/dist/cli.js +6 -15
  3. package/dist/client/client.js +34 -47
  4. package/dist/config/_sampleFeature.js +6 -17
  5. package/dist/config/assetRules.js +44 -55
  6. package/dist/config/bundleAnalyzerPlugin.js +12 -23
  7. package/dist/config/compressPlugin.js +68 -76
  8. package/dist/config/config.js +177 -185
  9. package/dist/config/copyPlugin.js +29 -40
  10. package/dist/config/cssRules.js +93 -83
  11. package/dist/config/definePlugin.js +11 -19
  12. package/dist/config/detectDeadCodePlugin.js +16 -21
  13. package/dist/config/fastRefreshPlugin.js +11 -22
  14. package/dist/config/forkTSCheckerPlugin.js +11 -22
  15. package/dist/config/harmonyLinkingErrorPlugin.js +3 -14
  16. package/dist/config/ignorePlugin.js +10 -21
  17. package/dist/config/javaScriptRules.js +151 -162
  18. package/dist/config/manifestPlugin.js +10 -18
  19. package/dist/config/miniCSSExtractPlugin.js +12 -23
  20. package/dist/config/nodePolyfill.js +14 -21
  21. package/dist/config/nodePrefixPlugin.js +8 -19
  22. package/dist/config/progressPlugin.js +7 -18
  23. package/dist/config/purgecssWebpackPlugin.js +15 -26
  24. package/dist/config/speedMeasureWebpackPlugin.js +12 -23
  25. package/dist/config/svgRules.js +43 -47
  26. package/dist/dev.js +101 -112
  27. package/dist/loader/svgr.js +4 -13
  28. package/dist/loader/swc.js +9 -14
  29. package/dist/plugins/ESBuildCSSMinifyPlugin.js +23 -34
  30. package/dist/plugins/ParcelCSSMinifyPlugin.js +30 -32
  31. package/dist/plugins/RuntimePublicPathPlugin.js +1 -1
  32. package/dist/schema.js +18 -6
  33. package/dist/server/server.d.ts +1 -1
  34. package/dist/server/server.js +161 -170
  35. package/dist/server/ws.d.ts +1 -1
  36. package/dist/types.d.ts +2 -6
  37. package/package.json +9 -11
  38. package/dist/server/https.d.ts +0 -4
  39. package/dist/server/https.js +0 -74
@@ -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,189 +29,190 @@ 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
- extraBabelIncludes: opts.extraBabelIncludes || [],
58
- extraEsbuildLoaderHandler: opts.extraEsbuildLoaderHandler || [],
59
- browsers: (0, browsersList_1.getBrowsersList)({
60
- targets: userConfig.targets,
61
- }),
62
- useHash,
63
- staticPathPrefix: opts.staticPathPrefix !== undefined ? opts.staticPathPrefix : 'static/',
64
- };
65
- // mode
66
- config.mode(opts.env);
67
- config.stats('none');
68
- // entry
69
- Object.keys(opts.entry).forEach((key) => {
70
- const entry = config.entry(key);
71
- if (isDev && opts.hmr) {
72
- entry.add(require.resolve('../../client/client/client'));
73
- }
74
- entry.add(opts.entry[key]);
75
- });
76
- // devtool
77
- config.devtool(isDev
78
- ? userConfig.devtool === false
79
- ? false
80
- : userConfig.devtool || constants_1.DEFAULT_DEVTOOL
81
- : userConfig.devtool);
82
- // output
83
- const absOutputPath = (0, path_1.join)(opts.cwd, userConfig.outputPath || constants_1.DEFAULT_OUTPUT_PATH);
84
- const disableCompress = process.env.COMPRESS === 'none';
85
- config.output
86
- .path(absOutputPath)
87
- .filename(useHash ? `[name].[contenthash:8].js` : `[name].js`)
88
- .chunkFilename(useHash ? `[name].[contenthash:8].async.js` : `[name].js`)
89
- .publicPath(userConfig.publicPath || 'auto')
90
- .pathinfo(isDev || disableCompress);
91
- // resolve
92
- // prettier-ignore
93
- config.resolve
94
- .set('symlinks', true)
95
- .modules
96
- .add('node_modules')
97
- .end()
98
- .alias
99
- .merge(userConfig.alias || {})
100
- .end()
101
- .extensions
102
- .merge([
103
- '.wasm',
104
- '.mjs',
105
- '.js',
106
- '.jsx',
107
- '.ts',
108
- '.tsx',
109
- '.json'
110
- ])
111
- .end();
112
- // externals
113
- config.externals(userConfig.externals || []);
114
- // target
115
- config.target(['web', 'es5']);
116
- // experiments
117
- config.experiments({
118
- topLevelAwait: true,
119
- outputModule: !!userConfig.esm,
120
- });
121
- // node polyfill
122
- yield (0, nodePolyfill_1.addNodePolyfill)(applyOpts);
123
- // rules
124
- yield (0, javaScriptRules_1.addJavaScriptRules)(applyOpts);
125
- yield (0, cssRules_1.addCSSRules)(applyOpts);
126
- yield (0, assetRules_1.addAssetRules)(applyOpts);
127
- yield (0, svgRules_1.addSVGRules)(applyOpts);
128
- // plugins
129
- // mini-css-extract-plugin
130
- yield (0, miniCSSExtractPlugin_1.addMiniCSSExtractPlugin)(applyOpts);
131
- // ignoreMomentLocale
132
- yield (0, ignorePlugin_1.addIgnorePlugin)(applyOpts);
133
- // define
134
- yield (0, definePlugin_1.addDefinePlugin)(applyOpts);
135
- // fast refresh
136
- yield (0, fastRefreshPlugin_1.addFastRefreshPlugin)(applyOpts);
137
- // progress
138
- yield (0, progressPlugin_1.addProgressPlugin)(applyOpts);
139
- // detect-dead-code-plugin
140
- yield (0, detectDeadCodePlugin_1.addDetectDeadCodePlugin)(applyOpts);
141
- // fork-ts-checker
142
- yield (0, forkTSCheckerPlugin_1.addForkTSCheckerPlugin)(applyOpts);
143
- // copy
144
- yield (0, copyPlugin_1.addCopyPlugin)(applyOpts);
145
- // manifest
146
- yield (0, manifestPlugin_1.addManifestPlugin)(applyOpts);
147
- // 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);
148
61
  if (isDev && opts.hmr) {
149
- config.plugin('hmr').use(webpack_1.default.HotModuleReplacementPlugin);
150
- }
151
- // compress
152
- yield (0, compressPlugin_1.addCompressPlugin)(applyOpts);
153
- // purgecss
154
- // await applyPurgeCSSWebpackPlugin(applyOpts);
155
- // handle HarmonyLinkingError
156
- yield (0, harmonyLinkingErrorPlugin_1.addHarmonyLinkingErrorPlugin)(applyOpts);
157
- // remove node: prefix
158
- // disable for performance
159
- // await addNodePrefixPlugin(applyOpts);
160
- // runtimePublicPath
161
- if (userConfig.runtimePublicPath) {
162
- config.plugin('runtimePublicPath').use(RuntimePublicPathPlugin_1.RuntimePublicPathPlugin);
62
+ entry.add(require.resolve('../../client/client/client'));
163
63
  }
164
- // cache
165
- if (opts.cache) {
166
- config.cache({
167
- type: 'filesystem',
168
- version: require('../../package.json').version,
169
- buildDependencies: {
170
- config: opts.cache.buildDependencies || [],
171
- },
172
- cacheDirectory: opts.cache.cacheDirectory ||
173
- // 使用 rootDir 是在有 APP_ROOT 时,把 cache 目录放在根目录下
174
- (0, path_1.join)(opts.rootDir || 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
+ // ref: smallfish
170
+ if ( /*isTnpm*/require('@umijs/utils/package').__npminstall_done) {
171
+ config.snapshot({
172
+ immutablePaths: [
173
+ opts.cache.absNodeModulesPath ||
174
+ (0, path_1.join)(opts.rootDir || opts.cwd, 'node_modules'),
175
+ ],
175
176
  });
176
- // tnpm 安装依赖的情况 webpack 默认的 managedPaths 不生效
177
- // 使用 immutablePaths 避免 node_modules 的内容被写入缓存
178
- // tnpm 安装的依赖路径中同时包含包名和版本号,满足 immutablePaths 使用的条件
179
- // ref: smallfish
180
- if ( /*isTnpm*/require('@umijs/utils/package').__npminstall_done) {
181
- config.snapshot({
182
- immutablePaths: [
183
- opts.cache.absNodeModulesPath ||
184
- (0, path_1.join)(opts.rootDir || opts.cwd, 'node_modules'),
185
- ],
186
- });
187
- }
188
- config.infrastructureLogging(Object.assign({ level: 'error' }, (process.env.WEBPACK_FS_CACHE_DEBUG
177
+ }
178
+ config.infrastructureLogging({
179
+ level: 'error',
180
+ ...(process.env.WEBPACK_FS_CACHE_DEBUG
189
181
  ? {
190
182
  debug: /webpack\.cache/,
191
183
  }
192
- : {})));
193
- }
194
- // analyzer
195
- if (opts.analyze) {
196
- yield (0, bundleAnalyzerPlugin_1.addBundleAnalyzerPlugin)(applyOpts);
197
- }
198
- // chain webpack
199
- if (opts.chainWebpack) {
200
- yield opts.chainWebpack(config, {
201
- env: opts.env,
202
- webpack: webpack_1.default,
203
- });
204
- }
205
- if (userConfig.chainWebpack) {
206
- yield userConfig.chainWebpack(config, {
207
- env: opts.env,
208
- webpack: webpack_1.default,
209
- });
210
- }
211
- let webpackConfig = config.toConfig();
212
- // speed measure
213
- // TODO: mini-css-extract-plugin 报错
214
- webpackConfig = yield (0, speedMeasureWebpackPlugin_1.addSpeedMeasureWebpackPlugin)({
215
- webpackConfig,
184
+ : {}),
216
185
  });
217
- if (opts.modifyWebpackConfig) {
218
- webpackConfig = yield opts.modifyWebpackConfig(webpackConfig, {
219
- env: opts.env,
220
- webpack: webpack_1.default,
221
- });
222
- }
223
- return webpackConfig;
186
+ }
187
+ // analyzer
188
+ if (opts.analyze) {
189
+ await (0, bundleAnalyzerPlugin_1.addBundleAnalyzerPlugin)(applyOpts);
190
+ }
191
+ // chain webpack
192
+ if (opts.chainWebpack) {
193
+ await opts.chainWebpack(config, {
194
+ env: opts.env,
195
+ webpack: webpack_1.default,
196
+ });
197
+ }
198
+ if (userConfig.chainWebpack) {
199
+ await userConfig.chainWebpack(config, {
200
+ env: opts.env,
201
+ webpack: webpack_1.default,
202
+ });
203
+ }
204
+ let webpackConfig = config.toConfig();
205
+ // speed measure
206
+ // TODO: mini-css-extract-plugin 报错
207
+ webpackConfig = await (0, speedMeasureWebpackPlugin_1.addSpeedMeasureWebpackPlugin)({
208
+ webpackConfig,
224
209
  });
210
+ if (opts.modifyWebpackConfig) {
211
+ webpackConfig = await opts.modifyWebpackConfig(webpackConfig, {
212
+ env: opts.env,
213
+ webpack: webpack_1.default,
214
+ });
215
+ }
216
+ return webpackConfig;
225
217
  }
226
218
  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;
@@ -1,98 +1,108 @@
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.addCSSRules = void 0;
13
- function addCSSRules(opts) {
14
- return __awaiter(this, void 0, void 0, function* () {
15
- const { config, userConfig } = opts;
16
- const rulesConfig = [
17
- { name: 'css', test: /\.css(\?.*)?$/ },
18
- {
19
- name: 'less',
20
- test: /\.less(\?.*)?$/,
21
- loader: require.resolve('@umijs/bundler-webpack/compiled/less-loader'),
22
- loaderOptions: {
23
- implementation: require.resolve('@umijs/bundler-utils/compiled/less'),
24
- lessOptions: Object.assign({ modifyVars: userConfig.theme, javascriptEnabled: true }, userConfig.lessLoader),
4
+ async function addCSSRules(opts) {
5
+ const { config, userConfig } = opts;
6
+ const rulesConfig = [
7
+ { name: 'css', test: /\.css(\?.*)?$/ },
8
+ {
9
+ name: 'less',
10
+ test: /\.less(\?.*)?$/,
11
+ loader: require.resolve('@umijs/bundler-webpack/compiled/less-loader'),
12
+ loaderOptions: {
13
+ implementation: require.resolve('@umijs/bundler-utils/compiled/less'),
14
+ lessOptions: {
15
+ modifyVars: userConfig.theme,
16
+ javascriptEnabled: true,
17
+ ...userConfig.lessLoader,
25
18
  },
26
19
  },
20
+ },
21
+ {
22
+ name: 'sass',
23
+ test: /\.(sass|scss)(\?.*)?$/,
24
+ loader: require.resolve('@umijs/bundler-webpack/compiled/sass-loader'),
25
+ loaderOptions: userConfig.sassLoader || {},
26
+ },
27
+ ];
28
+ for (const { name, test, loader, loaderOptions } of rulesConfig) {
29
+ const rule = config.module.rule(name);
30
+ const nestRulesConfig = [
31
+ userConfig.autoCSSModules && {
32
+ rule: rule
33
+ .test(test)
34
+ .oneOf('css-modules')
35
+ .resourceQuery(/modules/),
36
+ isCSSModules: true,
37
+ },
27
38
  {
28
- name: 'sass',
29
- test: /\.(sass|scss)(\?.*)?$/,
30
- loader: require.resolve('@umijs/bundler-webpack/compiled/sass-loader'),
31
- loaderOptions: userConfig.sassLoader || {},
39
+ rule: rule.test(test).oneOf('css').sideEffects(true),
40
+ isCSSModules: false,
32
41
  },
33
- ];
34
- for (const { name, test, loader, loaderOptions } of rulesConfig) {
35
- const rule = config.module.rule(name);
36
- const nestRulesConfig = [
37
- userConfig.autoCSSModules && {
38
- rule: rule
39
- .test(test)
40
- .oneOf('css-modules')
41
- .resourceQuery(/modules/),
42
- isCSSModules: true,
43
- },
44
- {
45
- rule: rule.test(test).oneOf('css').sideEffects(true),
46
- isCSSModules: false,
47
- },
48
- ].filter(Boolean);
49
- // @ts-ignore
50
- for (const { rule, isCSSModules } of nestRulesConfig) {
51
- if (userConfig.styleLoader) {
52
- rule
53
- .use('style-loader')
54
- .loader(require.resolve('@umijs/bundler-webpack/compiled/style-loader'))
55
- .options(Object.assign({ base: 0, esModule: true }, userConfig.styleLoader));
56
- }
57
- else {
58
- rule
59
- .use('mini-css-extract-plugin')
60
- .loader(require.resolve('@umijs/bundler-webpack/compiled/mini-css-extract-plugin/loader'))
61
- .options({
62
- publicPath: './',
63
- emit: true,
64
- esModule: true,
65
- });
66
- }
42
+ ].filter(Boolean);
43
+ // @ts-ignore
44
+ for (const { rule, isCSSModules } of nestRulesConfig) {
45
+ if (userConfig.styleLoader) {
67
46
  rule
68
- .use('css-loader')
69
- .loader(require.resolve('css-loader'))
70
- .options(Object.assign(Object.assign({ importLoaders: 1, esModule: true, url: true, import: true }, (isCSSModules
71
- ? {
72
- modules: Object.assign({ localIdentName: '[local]___[hash:base64:5]' }, userConfig.cssLoaderModules),
73
- }
74
- : {})), userConfig.cssLoader));
47
+ .use('style-loader')
48
+ .loader(require.resolve('@umijs/bundler-webpack/compiled/style-loader'))
49
+ .options({ base: 0, esModule: true, ...userConfig.styleLoader });
50
+ }
51
+ else {
75
52
  rule
76
- .use('postcss-loader')
77
- .loader(require.resolve('@umijs/bundler-webpack/compiled/postcss-loader'))
53
+ .use('mini-css-extract-plugin')
54
+ .loader(require.resolve('@umijs/bundler-webpack/compiled/mini-css-extract-plugin/loader'))
78
55
  .options({
79
- postcssOptions: Object.assign({ ident: 'postcss', plugins: [
80
- require('@umijs/bundler-webpack/compiled/postcss-flexbugs-fixes'),
81
- require('postcss-preset-env')({
82
- browsers: opts.browsers,
83
- autoprefixer: Object.assign({ flexbox: 'no-2009' }, userConfig.autoprefixer),
84
- stage: 3,
85
- }),
86
- ].concat(userConfig.extraPostCSSPlugins || []) }, userConfig.postcssLoader),
56
+ publicPath: './',
57
+ emit: true,
58
+ esModule: true,
87
59
  });
88
- if (loader) {
89
- rule
90
- .use(loader)
91
- .loader(typeof loader === 'string' ? require.resolve(loader) : loader)
92
- .options(loaderOptions || {});
93
- }
60
+ }
61
+ rule
62
+ .use('css-loader')
63
+ .loader(require.resolve('css-loader'))
64
+ .options({
65
+ importLoaders: 1,
66
+ esModule: true,
67
+ url: true,
68
+ import: true,
69
+ ...(isCSSModules
70
+ ? {
71
+ modules: {
72
+ localIdentName: '[local]___[hash:base64:5]',
73
+ ...userConfig.cssLoaderModules,
74
+ },
75
+ }
76
+ : {}),
77
+ ...userConfig.cssLoader,
78
+ });
79
+ rule
80
+ .use('postcss-loader')
81
+ .loader(require.resolve('@umijs/bundler-webpack/compiled/postcss-loader'))
82
+ .options({
83
+ postcssOptions: {
84
+ ident: 'postcss',
85
+ plugins: [
86
+ require('@umijs/bundler-webpack/compiled/postcss-flexbugs-fixes'),
87
+ require('postcss-preset-env')({
88
+ browsers: opts.browsers,
89
+ autoprefixer: {
90
+ flexbox: 'no-2009',
91
+ ...userConfig.autoprefixer,
92
+ },
93
+ stage: 3,
94
+ }),
95
+ ].concat(userConfig.extraPostCSSPlugins || []),
96
+ ...userConfig.postcssLoader,
97
+ },
98
+ });
99
+ if (loader) {
100
+ rule
101
+ .use(loader)
102
+ .loader(typeof loader === 'string' ? require.resolve(loader) : loader)
103
+ .options(loaderOptions || {});
94
104
  }
95
105
  }
96
- });
106
+ }
97
107
  }
98
108
  exports.addCSSRules = addCSSRules;