Skip to content

Commit f9169d8

Browse files
authored
feat!: use the latest versions of css preprocessor loaders by default (#6301)
1 parent 3f367b2 commit f9169d8

File tree

5 files changed

+72
-10
lines changed

5 files changed

+72
-10
lines changed

docs/migrations/migrate-from-v4.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ No longer supports generating project with `node-sass`. It has been [deprecated]
8080

8181
* `html-webpack-plugin` is upgraded from v3 to v5, and for webpack 4 users, v4 will be used. More details are available in the [release announcement of `html-webpack-plugin` v4](https://dev.to/jantimon/html-webpack-plugin-4-has-been-released-125d) and the [full changelog](https://github.com/jantimon/html-webpack-plugin/blob/master/CHANGELOG.md).
8282
* `sass-loader` v7 support is dropped. See the v8 breaking changes at its [changelog](https://github.com/webpack-contrib/sass-loader/blob/master/CHANGELOG.md#800-2019-08-29).
83-
* `postcss-loader` is upgraded from v3 to v4. Most notably, `PostCSS` options (`plugin` / `syntax` / `parser` / `stringifier`) are moved into the `postcssOptions` field. More details available at the [changelog](https://github.com/webpack-contrib/postcss-loader/blob/master/CHANGELOG.md#400-2020-09-07).
83+
* `postcss-loader` is upgraded from v3 to v5 (v4 for webpack 4 users). Most notably, `PostCSS` options (`plugin` / `syntax` / `parser` / `stringifier`) are moved into the `postcssOptions` field. More details available at the [changelog](https://github.com/webpack-contrib/postcss-loader/blob/master/CHANGELOG.md#400-2020-09-07).
8484
* `copy-webpack-plugin` is upgraded from v5 to v7 (v6 if you choose to stay at webpack 4). If you never customized its config through `config.plugin('copy')`, there should be no user-facing breaking changes. A full list of breaking changes is available at [`copy-webpack-plugin` v6.0.0 release](https://github.com/webpack-contrib/copy-webpack-plugin/releases/tag/v6.0.0) and [v7.0.0 release](https://github.com/webpack-contrib/copy-webpack-plugin/releases/tag/v7.0.0).
85-
* `file-loader` is upgraded from v4 to v6, and `url-loader` from v2 to v4. The `esModule` option is now turned on by default for non-Vue-2 projects. Full changelog available at [`file-loader` changelog](https://github.com/webpack-contrib/file-loader/blob/master/CHANGELOG.md) and [`url-loader` changelog](https://github.com/webpack-contrib/url-loader/blob/master/CHANGELOG.md)
85+
* `file-loader` is upgraded from v4 to v6, and `url-loader` from v2 to v4. The `esModule` option is now turned on by default for non-Vue-2 projects. Full changelog available at [`file-loader` changelog](https://github.com/webpack-contrib/file-loader/blob/master/CHANGELOG.md) and [`url-loader` changelog](https://github.com/webpack-contrib/url-loader/blob/master/CHANGELOG.md).
8686
* `terser-webpack-plugin` is upgraded from v2 to v5 (v4 if you choose to stay at webpack 4), using terser 5 and some there are some changes in the options format. See full details in its [changelog](https://github.com/webpack-contrib/terser-webpack-plugin/blob/master/CHANGELOG.md).
87+
* When creating new projects, the default `less-loader` is updated from [v5 to v8](https://github.com/webpack-contrib/less-loader/blob/master/CHANGELOG.md)(v7 for webpack 4 users); `less` from [v3 to v4](https://github.com/less/less.js/pull/3573); `sass-loader` from [v8 to v11](https://github.com/webpack-contrib/sass-loader/blob/master/CHANGELOG.md) (v10 for webpack 4 users); `stylus-loader` from [v3 to v5](https://github.com/webpack-contrib/stylus-loader/blob/master/CHANGELOG.md) (v4 for webpack 4 users).
8788

8889
### ESLint Plugin
8990

packages/@vue/cli-plugin-webpack-4/generator.js

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
const { semver } = require('@vue/cli-shared-utils')
2+
13
/** @type {import('@vue/cli').GeneratorPlugin} */
24
module.exports = (api) => {
35
api.extendPackage({
46
devDependencies: {
5-
'webpack': '^4.0.0'
7+
webpack: '^4.0.0'
68
},
79
// Force resolutions is more reliable than module-alias
810
// Yarn and PNPM 5.10+ support this feature
@@ -13,5 +15,36 @@ module.exports = (api) => {
1315
}
1416
})
1517

16-
// TODO: if uses sass, replace sass-loader@11 with sass-loader@10
18+
api.extendPackage(
19+
(pkg) => {
20+
const oldDevDeps = pkg.devDependencies
21+
const newDevDeps = {}
22+
const unsupportedRanges = {
23+
'less-loader': '>= 8.0.0',
24+
'sass-loader': '>= 11.0.0',
25+
'stylus-loader': '>= 5.0.0'
26+
}
27+
const maxSupportedRanges = {
28+
'less-loader': '^7.3.0',
29+
'sass-loader': '^10.1.1',
30+
'stylus-loader': '^4.3.3'
31+
}
32+
33+
for (const loader of ['less-loader', 'sass-loader', 'stylus-loader']) {
34+
if (
35+
oldDevDeps[loader] &&
36+
semver.intersects(oldDevDeps[loader], unsupportedRanges[loader])
37+
) {
38+
newDevDeps[loader] = maxSupportedRanges[loader]
39+
}
40+
}
41+
42+
const toMerge = { devDependencies: newDevDeps }
43+
44+
return toMerge
45+
},
46+
{
47+
warnIncompatibleVersions: false
48+
}
49+
)
1750
}

packages/@vue/cli-plugin-webpack-4/index.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const htmlWebpackPlugin4Path = path.dirname(require.resolve('html-webpack-plugin
1010
moduleAlias.addAlias('html-webpack-plugin', htmlWebpackPlugin4Path)
1111

1212
/** @type {import('@vue/cli-service').ServicePlugin} */
13-
module.exports = (api, options) => {
13+
module.exports = (api, rootOptions) => {
1414
api.chainWebpack(config => {
1515
// webpack-4 alias is set for the webpack-dev-server
1616
// should also set for the injected client hmr code so as to avoid mismatch
@@ -49,6 +49,31 @@ module.exports = (api, options) => {
4949
.use({ ...require('pnp-webpack-plugin').topLevelLoader })
5050
.end()
5151

52+
// Use postcss-loader v7
53+
const shadowMode = !!process.env.VUE_CLI_CSS_SHADOW_MODE
54+
const isProd = process.env.NODE_ENV === 'production'
55+
const { extract = isProd } = rootOptions.css || {}
56+
const shouldExtract = extract !== false && !shadowMode
57+
const needInlineMinification = isProd && !shouldExtract
58+
59+
const postcssLoaderPath = require.resolve('postcss-loader')
60+
61+
const langs = ['css', 'postcss', 'scss', 'sass', 'less', 'stylus']
62+
const matches = ['vue-modules', 'vue', 'normal-modules', 'normal']
63+
64+
langs.forEach(lang =>
65+
matches.forEach(match => {
66+
const rule = config.module
67+
.rule(lang)
68+
.oneOf(match)
69+
70+
if (needInlineMinification) {
71+
rule.use('cssnano').loader(postcssLoaderPath)
72+
}
73+
rule.use('postcss-loader').loader(postcssLoaderPath)
74+
})
75+
)
76+
5277
if (!process.env.VUE_CLI_BUILD_TARGET || process.env.VUE_CLI_BUILD_TARGET === 'app') {
5378
const isLegacyBundle = process.env.VUE_CLI_MODERN_MODE && !process.env.VUE_CLI_MODERN_BUILD
5479
const publicDir = api.resolve('public')
@@ -103,7 +128,7 @@ module.exports = (api, options) => {
103128
config.optimization.minimizer('terser').init(
104129
(Plugin, [terserPluginOptions]) =>
105130
new TerserPluginV4({
106-
sourceMap: options.productionSourceMap,
131+
sourceMap: rootOptions.productionSourceMap,
107132
cache: true,
108133
...terserPluginOptions
109134
})

packages/@vue/cli-plugin-webpack-4/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
"access": "public"
2424
},
2525
"dependencies": {
26+
"@vue/cli-shared-utils": "^5.0.0-alpha.4",
2627
"copy-webpack-plugin": "^6.4.1",
2728
"hash-sum": "^2.0.0",
2829
"html-webpack-plugin": "^4.5.1",
2930
"module-alias": "^2.2.2",
3031
"pnp-webpack-plugin": "^1.6.4",
32+
"postcss": "^8.2.6",
33+
"postcss-loader": "^4.2.0",
3134
"terser-webpack-plugin": "^4.2.3",
3235
"webpack": "^4.44.2"
3336
},

packages/@vue/cli-service/generator/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ module.exports = (api, options) => {
3939
const deps = {
4040
sass: {
4141
sass: '^1.32.7',
42-
'sass-loader': '^10.1.0'
42+
'sass-loader': '^11.0.1'
4343
},
4444
'dart-sass': {
4545
sass: '^1.32.7',
4646
'sass-loader': '^10.1.0'
4747
},
4848
less: {
49-
'less': '^3.0.4',
50-
'less-loader': '^5.0.0'
49+
'less': '^4.0.0',
50+
'less-loader': '^8.0.0'
5151
},
5252
stylus: {
5353
'stylus': '^0.54.8',
54-
'stylus-loader': '^4.3.1'
54+
'stylus-loader': '^5.0.0'
5555
}
5656
}
5757

0 commit comments

Comments
 (0)