Skip to content

Commit a8a38eb

Browse files
Jinjiangyyx990803
authored andcommitted
[fixed #965] more friendly error message for loading PostCSS config (#1095)
* more friendly error message for loading postcss config fix #965 * added an argument in the callback of test() to test webpack warnings * removed duplicated expect code * supported analysing webpack & jsdom errors * added test case for postcss config file error
1 parent cdbf8a8 commit a8a38eb

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

lib/style-compiler/load-postcss-config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ module.exports = function loadPostcssConfig (loaderContext, inlineConfig = {}) {
2323
loaded = load(ctx, config.path, { argv: false }).catch(err => {
2424
// postcss-load-config throws error when no config file is found,
2525
// but for us it's optional. only emit other errors
26-
if (err.message.indexOf('No PostCSS Config found') < 0) {
27-
loaderContext.emitError(err)
26+
if (err.message.indexOf('No PostCSS Config found') >= 0) {
27+
return
2828
}
29+
const friendlyErr = new Error(`Error on Loading PostCSS Config: ${err.message}`)
30+
Error.captureStackTrace(friendlyErr, err)
31+
loaderContext.emitError(friendlyErr)
2932
})
3033
}
3134

test/test.js

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function genId (file) {
3939
return hash(path.join('test', 'fixtures', file))
4040
}
4141

42-
function bundle (options, cb) {
42+
function bundle (options, cb, wontThrowError) {
4343
const vueOptions = options.vue
4444
delete options.vue
4545
const config = Object.assign({}, globalConfig, options)
@@ -54,41 +54,41 @@ function bundle (options, cb) {
5454
const webpackCompiler = webpack(config)
5555
webpackCompiler.outputFileSystem = mfs
5656
webpackCompiler.run((err, stats) => {
57-
expect(err).to.be.null
58-
if (stats.compilation.errors.length) {
59-
stats.compilation.errors.forEach((err) => {
60-
console.error(err.message)
61-
})
62-
}
63-
if (stats.compilation.errors) {
64-
stats.compilation.errors.forEach(err => {
65-
console.error(err.message)
66-
})
57+
const errors = stats.compilation.errors
58+
if (!wontThrowError) {
59+
expect(err).to.be.null
60+
if (errors && errors.length) {
61+
errors.forEach(error => {
62+
console.error(error.message)
63+
})
64+
}
65+
expect(errors).to.be.empty
6766
}
68-
expect(stats.compilation.errors).to.be.empty
69-
cb(mfs.readFileSync('/test.build.js').toString(), stats.compilation.warnings)
67+
cb(mfs.readFileSync('/test.build.js').toString(), stats, err)
7068
})
7169
}
7270

73-
function test (options, assert) {
74-
bundle(options, (code, warnings) => {
71+
function test (options, assert, wontThrowError) {
72+
bundle(options, (code, stats, err) => {
7573
jsdom.env({
7674
html: '<!DOCTYPE html><html><head></head><body></body></html>',
7775
src: [code],
78-
done: (err, window) => {
79-
if (err) {
80-
console.log(err[0].data.error.stack)
81-
expect(err).to.be.null
76+
done: (errors, window) => {
77+
if (errors) {
78+
console.log(errors[0].data.error.stack)
79+
if (!wontThrowError) {
80+
expect(errors).to.be.null
81+
}
8282
}
8383
const module = interopDefault(window.vueModule)
8484
const instance = {}
8585
if (module && module.beforeCreate) {
8686
module.beforeCreate.forEach(hook => hook.call(instance))
8787
}
88-
assert(window, module, window.vueModule, instance)
88+
assert(window, module, window.vueModule, instance, errors, { stats, err })
8989
}
9090
})
91-
})
91+
}, wontThrowError)
9292
}
9393

9494
function mockRender (options, data = {}) {
@@ -511,7 +511,23 @@ describe('vue-loader', () => {
511511
})
512512
})
513513

514-
it('postcss options', done => {
514+
it('load postcss config file with js syntax error', done => {
515+
fs.writeFileSync('.postcssrc.js', 'module.exports = { hello }')
516+
test({
517+
entry: './test/fixtures/basic.vue'
518+
}, (window, module, vueModule, instance, jsdomErr, webpackInfo) => {
519+
const { stats: { compilation: { warnings, errors }}, err } = webpackInfo
520+
expect(jsdomErr).to.be.null
521+
expect(err).to.be.null
522+
expect(warnings).to.be.empty
523+
expect(errors.length).to.equal(1)
524+
expect(errors[0].message).match(/^Error on Loading PostCSS Config\:/)
525+
fs.unlinkSync('.postcssrc.js')
526+
done()
527+
}, true)
528+
})
529+
530+
it('postcss lang', done => {
515531
test({
516532
entry: './test/fixtures/postcss-lang.vue'
517533
}, (window) => {

0 commit comments

Comments
 (0)