Skip to content

Commit 9ebc2ab

Browse files
authored
feat: enable postcss+autoprefixer by default internally, reducing boilerplate (#4798)
This also fixes the issue with Yarn PnP that requires `autoprefixer` to be explicitly listed in the user's project dependency.
1 parent e1d156f commit 9ebc2ab

File tree

8 files changed

+39
-51
lines changed

8 files changed

+39
-51
lines changed

.postcssrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/@vue/cli-service/__tests__/css.spec.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ const findOptions = (config, lang, _loader, index) => {
5757
}
5858

5959
test('default loaders', () => {
60-
const config = genConfig({ postcss: {}})
60+
const config = genConfig()
6161

6262
LANGS.forEach(lang => {
6363
const loader = lang === 'css' ? [] : LOADERS[lang]
6464
expect(findLoaders(config, lang)).toEqual(['vue-style', 'css', 'postcss'].concat(loader))
65-
expect(findOptions(config, lang, 'postcss').plugins).toBeFalsy()
65+
expect(findOptions(config, lang, 'postcss').plugins).toEqual([require('autoprefixer')])
6666
// assert css-loader options
6767
expect(findOptions(config, lang, 'css')).toEqual({
6868
sourceMap: false,
@@ -79,11 +79,25 @@ test('default loaders', () => {
7979
})
8080

8181
test('production defaults', () => {
82-
const config = genConfig({ postcss: {}}, 'production')
82+
const config = genConfig({}, 'production')
8383
LANGS.forEach(lang => {
8484
const loader = lang === 'css' ? [] : LOADERS[lang]
8585
expect(findLoaders(config, lang)).toEqual([extractLoaderPath, 'css', 'postcss'].concat(loader))
86+
expect(findOptions(config, lang, 'postcss').plugins).toEqual([require('autoprefixer')])
87+
expect(findOptions(config, lang, 'css')).toEqual({
88+
sourceMap: false,
89+
importLoaders: 2
90+
})
91+
})
92+
})
93+
94+
test('override postcss config', () => {
95+
const config = genConfig({ postcss: {}})
96+
LANGS.forEach(lang => {
97+
const loader = lang === 'css' ? [] : LOADERS[lang]
98+
expect(findLoaders(config, lang)).toEqual(['vue-style', 'css', 'postcss'].concat(loader))
8699
expect(findOptions(config, lang, 'postcss').plugins).toBeFalsy()
100+
// assert css-loader options
87101
expect(findOptions(config, lang, 'css')).toEqual({
88102
sourceMap: false,
89103
importLoaders: 2
@@ -101,7 +115,7 @@ test('CSS Modules rules', () => {
101115
})
102116
LANGS.forEach(lang => {
103117
const expected = {
104-
importLoaders: 1, // no postcss-loader
118+
importLoaders: 2, // with postcss-loader
105119
sourceMap: false,
106120
modules: {
107121
localIdentName: `[name]_[local]_[hash:base64:5]`
@@ -140,7 +154,7 @@ test('Customized CSS Modules rules', () => {
140154

141155
LANGS.forEach(lang => {
142156
const expected = {
143-
importLoaders: 1, // no postcss-loader
157+
importLoaders: 2, // with postcss-loader
144158
sourceMap: false,
145159
modules: {
146160
localIdentName: `[folder]-[name]-[local][emoji]`
@@ -174,7 +188,7 @@ test('deprecate `css.modules` option', () => {
174188

175189
LANGS.forEach(lang => {
176190
const expected = {
177-
importLoaders: 1, // no postcss-loader
191+
importLoaders: 2, // with postcss-loader
178192
sourceMap: false,
179193
modules: {
180194
localIdentName: `[folder]-[name]-[local][emoji]`
@@ -211,7 +225,7 @@ test('favor `css.requireModuleExtension` over `css.modules`', () => {
211225

212226
LANGS.forEach(lang => {
213227
const expected = {
214-
importLoaders: 1, // no postcss-loader
228+
importLoaders: 2, // with postcss-loader
215229
sourceMap: false,
216230
modules: {
217231
localIdentName: `[folder]-[name]-[local][emoji]`
@@ -236,10 +250,10 @@ test('css.extract', () => {
236250
}, 'production')
237251
LANGS.forEach(lang => {
238252
const loader = lang === 'css' ? [] : LOADERS[lang]
239-
// when extract is false in production, even without postcss config,
240-
// an instance of postcss-loader is injected for inline minification.
241-
expect(findLoaders(config, lang)).toEqual(['vue-style', 'css', 'postcss'].concat(loader))
242-
expect(findOptions(config, lang, 'css').importLoaders).toBe(2)
253+
// when extract is false in production,
254+
// an additional instance of postcss-loader is injected for inline minification.
255+
expect(findLoaders(config, lang)).toEqual(['vue-style', 'css', 'postcss', 'postcss'].concat(loader))
256+
expect(findOptions(config, lang, 'css').importLoaders).toBe(3)
243257
expect(findOptions(config, lang, 'postcss').plugins).toBeTruthy()
244258
})
245259

@@ -380,10 +394,3 @@ test('should use dart sass implementation whenever possible', () => {
380394
expect(findOptions(config, 'sass', 'sass')).toMatchObject({ implementation: require('sass') })
381395
})
382396

383-
test('skip postcss-loader if no postcss config found', () => {
384-
const config = genConfig()
385-
LANGS.forEach(lang => {
386-
const loader = lang === 'css' ? [] : LOADERS[lang]
387-
expect(findLoaders(config, lang)).toEqual(['vue-style', 'css'].concat(loader))
388-
})
389-
})

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ module.exports = (api, options) => {
1414
devDependencies: {
1515
'vue-template-compiler': '^2.6.10'
1616
},
17-
'postcss': {
18-
'plugins': {
19-
'autoprefixer': {}
20-
}
21-
},
2217
browserslist: [
2318
'> 1%',
2419
'last 2 versions'

packages/@vue/cli-service/lib/config/css.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ module.exports = (api, rootOptions) => {
8282
'.postcssrc.json'
8383
]))
8484

85+
if (!hasPostCSSConfig) {
86+
loaderOptions.postcss = {
87+
plugins: [
88+
require('autoprefixer')
89+
]
90+
}
91+
}
92+
8593
// if building for production but not extracting CSS, we need to minimize
8694
// the embbeded inline CSS as they will not be going through the optimizing
8795
// plugin.
@@ -139,7 +147,7 @@ module.exports = (api, rootOptions) => {
139147
sourceMap,
140148
importLoaders: (
141149
1 + // stylePostLoader injected by vue-loader
142-
(hasPostCSSConfig ? 1 : 0) +
150+
1 + // postcss-loader
143151
(needInlineMinification ? 1 : 0)
144152
)
145153
}, loaderOptions.css)
@@ -168,12 +176,10 @@ module.exports = (api, rootOptions) => {
168176
})
169177
}
170178

171-
if (hasPostCSSConfig) {
172-
rule
173-
.use('postcss-loader')
174-
.loader(require.resolve('postcss-loader'))
175-
.options(Object.assign({ sourceMap }, loaderOptions.postcss))
176-
}
179+
rule
180+
.use('postcss-loader')
181+
.loader(require.resolve('postcss-loader'))
182+
.options(Object.assign({ sourceMap }, loaderOptions.postcss))
177183

178184
if (loader) {
179185
let resolvedLoader

packages/@vue/cli-ui-addon-webpack/.postcssrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/@vue/cli-ui-addon-widgets/postcss.config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/@vue/cli-ui/.postcssrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/@vue/cli/lib/Creator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ module.exports = class Creator extends EventEmitter {
429429
name: 'useConfigFiles',
430430
when: isManualMode,
431431
type: 'list',
432-
message: 'Where do you prefer placing config for Babel, PostCSS, ESLint, etc.?',
432+
message: 'Where do you prefer placing config for Babel, ESLint, etc.?',
433433
choices: [
434434
{
435435
name: 'In dedicated config files',

0 commit comments

Comments
 (0)