Skip to content

Commit

Permalink
fix: skip postcss-loader if no postcss config is present
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 26, 2018
1 parent 3a2c6cd commit 1142339
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
8 changes: 7 additions & 1 deletion packages/@vue/cli-service-global/lib/createConfigPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module.exports = function createConfigPlugin (context, entry) {
}

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

// set inline eslint options
// check eslint config presence
// otherwise eslint-loader goes all the way up to look for eslintrc, can be
// messed up when the project is inside another project.
const ESLintConfigFile = findExisting(context, [
'.eslintrc.js',
'.eslintrc.yaml',
Expand All @@ -86,6 +90,8 @@ module.exports = function createConfigPlugin (context, entry) {
const hasESLintConfig = ESLintConfigFile === 'package.json'
? !!(require(path.join(context, 'package.json')).eslintConfig)
: !!ESLintConfigFile

// set inline eslint options
config.module
.rule('eslint')
.include
Expand Down
13 changes: 11 additions & 2 deletions packages/@vue/cli-service/__tests__/css.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const expectedCssLoaderModulesOptions = {
}

test('default loaders', () => {
const config = genConfig()
const config = genConfig({ postcss: {}})

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

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

test('css.sourceMap', () => {
const config = genConfig({
postcss: {},
vue: {
css: {
sourceMap: true
Expand Down Expand Up @@ -158,3 +159,11 @@ test('css.loaderOptions', () => {
expect(findOptions(config, 'sass', 'sass')).toEqual({ data, indentedSyntax: true, sourceMap: false })
expect(findOptionsForVue(config, 'sass', 'sass')).toEqual({ data, indentedSyntax: true, sourceMap: false })
})

test('skip postcss-loader if no postcss config found', () => {
const config = genConfig()
LANGS.forEach(lang => {
const loader = lang === 'css' ? [] : LOADERS[lang]
expect(findLoaders(config, lang)).toEqual(['vue-style', 'css'].concat(loader))
})
})
26 changes: 25 additions & 1 deletion packages/@vue/cli-service/lib/config/css.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
const fs = require('fs')
const path = require('path')

const findExisting = (context, files) => {
for (const file of files) {
if (fs.existsSync(path.join(context, file))) {
return file
}
}
}

module.exports = (api, options) => {
api.chainWebpack(webpackConfig => {
const CSSLoaderResolver = require('../webpack/CSSLoaderResolver')
Expand All @@ -6,9 +17,22 @@ module.exports = (api, options) => {
const isProd = process.env.NODE_ENV === 'production'
const userOptions = options.css || {}
const extract = isProd && userOptions.extract !== false

// check if the project has a valid postcss config
// if it doesn't, don't use postcss-loader for direct style imports
// because otherwise it would throw error when attempting to load postcss config
const hasPostCSSConfig = !!(api.service.pkg.postcss || findExisting(api.resolve('.'), [
'.postcssrc',
'.postcssrc.js',
'postcss.config.js',
'.postcssrc.yaml',
'.postcssrc.json'
]))

const baseOptions = Object.assign({}, userOptions, {
extract,
minimize: isProd
minimize: isProd,
postcss: hasPostCSSConfig
})

const resolver = new CSSLoaderResolver(baseOptions)
Expand Down
4 changes: 3 additions & 1 deletion packages/@vue/cli-service/lib/webpack/CSSLoaderResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@ module.exports = class CSSLoaderResolver {
* @param {boolean} [options.modules=undefined] Enable CSS modules.
* @param {boolean} [options.extract=undefined] Extract CSS.
* @param {boolean} [options.minimize=undefined] Minimize CSS.
* @param {boolean} [options.postcss=undefined] Enable postcss-loader.
* @param {Object} [options.loaderOptions={}] Options to pass on to loaders.
*/
constructor ({
sourceMap,
modules,
extract,
minimize,
postcss,
loaderOptions
} = {}) {
this.postcss = true // true by default, turned off if generating for vue-loader
this.cssLoader = 'css-loader'
this.fallbackLoader = 'vue-style-loader'
this.sourceMap = sourceMap
this.extract = extract
this.minimize = minimize
this.modules = modules
this.postcss = postcss
this.loaderOptions = loaderOptions || {}
}

Expand Down

0 comments on commit 1142339

Please sign in to comment.