Skip to content

Commit 1142339

Browse files
committed
fix: skip postcss-loader if no postcss config is present
1 parent 3a2c6cd commit 1142339

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

packages/@vue/cli-service-global/lib/createConfigPlugin.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module.exports = function createConfigPlugin (context, entry) {
2121
}
2222

2323
// ensure loaders can be resolved properly
24+
// this is done by locating vue's install location (which is a
25+
// dependency of the global service)
2426
const modulePath = path.resolve(require.resolve('vue'), '../../../')
2527
config.resolveLoader
2628
.modules
@@ -74,7 +76,9 @@ module.exports = function createConfigPlugin (context, entry) {
7476
.use('babel-loader')
7577
.tap(() => babelOptions)
7678

77-
// set inline eslint options
79+
// check eslint config presence
80+
// otherwise eslint-loader goes all the way up to look for eslintrc, can be
81+
// messed up when the project is inside another project.
7882
const ESLintConfigFile = findExisting(context, [
7983
'.eslintrc.js',
8084
'.eslintrc.yaml',
@@ -86,6 +90,8 @@ module.exports = function createConfigPlugin (context, entry) {
8690
const hasESLintConfig = ESLintConfigFile === 'package.json'
8791
? !!(require(path.join(context, 'package.json')).eslintConfig)
8892
: !!ESLintConfigFile
93+
94+
// set inline eslint options
8995
config.module
9096
.rule('eslint')
9197
.include

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const expectedCssLoaderModulesOptions = {
5959
}
6060

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

6464
LANGS.forEach(lang => {
6565
const loader = lang === 'css' ? [] : LOADERS[lang]
@@ -81,7 +81,7 @@ test('default loaders', () => {
8181
})
8282

8383
test('production defaults', () => {
84-
const config = genConfig({}, 'production')
84+
const config = genConfig({ postcss: {}}, 'production')
8585
const extractLoaderPath = require.resolve('extract-text-webpack-plugin/dist/loader')
8686
LANGS.forEach(lang => {
8787
const loader = lang === 'css' ? [] : LOADERS[lang]
@@ -124,6 +124,7 @@ test('css.extract', () => {
124124

125125
test('css.sourceMap', () => {
126126
const config = genConfig({
127+
postcss: {},
127128
vue: {
128129
css: {
129130
sourceMap: true
@@ -158,3 +159,11 @@ test('css.loaderOptions', () => {
158159
expect(findOptions(config, 'sass', 'sass')).toEqual({ data, indentedSyntax: true, sourceMap: false })
159160
expect(findOptionsForVue(config, 'sass', 'sass')).toEqual({ data, indentedSyntax: true, sourceMap: false })
160161
})
162+
163+
test('skip postcss-loader if no postcss config found', () => {
164+
const config = genConfig()
165+
LANGS.forEach(lang => {
166+
const loader = lang === 'css' ? [] : LOADERS[lang]
167+
expect(findLoaders(config, lang)).toEqual(['vue-style', 'css'].concat(loader))
168+
})
169+
})

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

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
4+
const findExisting = (context, files) => {
5+
for (const file of files) {
6+
if (fs.existsSync(path.join(context, file))) {
7+
return file
8+
}
9+
}
10+
}
11+
112
module.exports = (api, options) => {
213
api.chainWebpack(webpackConfig => {
314
const CSSLoaderResolver = require('../webpack/CSSLoaderResolver')
@@ -6,9 +17,22 @@ module.exports = (api, options) => {
617
const isProd = process.env.NODE_ENV === 'production'
718
const userOptions = options.css || {}
819
const extract = isProd && userOptions.extract !== false
20+
21+
// check if the project has a valid postcss config
22+
// if it doesn't, don't use postcss-loader for direct style imports
23+
// because otherwise it would throw error when attempting to load postcss config
24+
const hasPostCSSConfig = !!(api.service.pkg.postcss || findExisting(api.resolve('.'), [
25+
'.postcssrc',
26+
'.postcssrc.js',
27+
'postcss.config.js',
28+
'.postcssrc.yaml',
29+
'.postcssrc.json'
30+
]))
31+
932
const baseOptions = Object.assign({}, userOptions, {
1033
extract,
11-
minimize: isProd
34+
minimize: isProd,
35+
postcss: hasPostCSSConfig
1236
})
1337

1438
const resolver = new CSSLoaderResolver(baseOptions)

packages/@vue/cli-service/lib/webpack/CSSLoaderResolver.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,24 @@ module.exports = class CSSLoaderResolver {
1515
* @param {boolean} [options.modules=undefined] Enable CSS modules.
1616
* @param {boolean} [options.extract=undefined] Extract CSS.
1717
* @param {boolean} [options.minimize=undefined] Minimize CSS.
18+
* @param {boolean} [options.postcss=undefined] Enable postcss-loader.
1819
* @param {Object} [options.loaderOptions={}] Options to pass on to loaders.
1920
*/
2021
constructor ({
2122
sourceMap,
2223
modules,
2324
extract,
2425
minimize,
26+
postcss,
2527
loaderOptions
2628
} = {}) {
27-
this.postcss = true // true by default, turned off if generating for vue-loader
2829
this.cssLoader = 'css-loader'
2930
this.fallbackLoader = 'vue-style-loader'
3031
this.sourceMap = sourceMap
3132
this.extract = extract
3233
this.minimize = minimize
3334
this.modules = modules
35+
this.postcss = postcss
3436
this.loaderOptions = loaderOptions || {}
3537
}
3638

0 commit comments

Comments
 (0)