|
1 | 1 | var postcss = require('postcss') |
2 | | -var selectorParser = require('postcss-selector-parser') |
3 | 2 | var loaderUtils = require('loader-utils') |
| 3 | +var loadPostcssConfig = require('postcss-load-config') |
| 4 | +var selectorParser = require('postcss-selector-parser') |
4 | 5 |
|
5 | 6 | var addId = postcss.plugin('add-id', function (opts) { |
6 | 7 | return function (root) { |
@@ -42,67 +43,75 @@ module.exports = function (css, map) { |
42 | 43 | var cb = this.async() |
43 | 44 |
|
44 | 45 | var query = loaderUtils.parseQuery(this.query) |
45 | | - var options = this.options.__vueOptions__ |
46 | | - var postcssOptions = options.postcss |
| 46 | + var vueOptions = this.options.__vueOptions__ |
47 | 47 |
|
48 | | - // postcss plugins |
49 | | - var plugins |
50 | | - if (Array.isArray(postcssOptions)) { |
51 | | - plugins = postcssOptions |
52 | | - } else if (typeof postcssOptions === 'function') { |
53 | | - plugins = postcssOptions.call(this, this) |
54 | | - } else if (isObject(postcssOptions) && postcssOptions.plugins) { |
55 | | - plugins = postcssOptions.plugins |
56 | | - } |
57 | | - plugins = [trim].concat(plugins || []) |
| 48 | + // use the same config loading interface as postcss-loader |
| 49 | + loadPostcssConfig({ webpack: this }).catch(() => { |
| 50 | + // postcss-load-config throws error when no config file is found, |
| 51 | + // but for us it's optional. |
| 52 | + }).then(config => { |
| 53 | + var plugins |
| 54 | + var options |
58 | 55 |
|
59 | | - // scoped css |
60 | | - if (query.scoped) { |
61 | | - plugins.push(addId({ id: query.id })) |
62 | | - } |
| 56 | + // inline postcss options for vue-loader |
| 57 | + var rawInlineOptions = vueOptions.postcss |
| 58 | + if (typeof rawInlineOptions === 'function') { |
| 59 | + rawInlineOptions = rawInlineOptions.call(this, this) |
| 60 | + } |
| 61 | + if (Array.isArray(rawInlineOptions)) { |
| 62 | + plugins = rawInlineOptions |
| 63 | + } else if (isObject(rawInlineOptions)) { |
| 64 | + plugins = rawInlineOptions.plugins |
| 65 | + options = rawInlineOptions.options |
| 66 | + } |
63 | 67 |
|
64 | | - // postcss options, for source maps |
65 | | - var file = this.resourcePath |
66 | | - var opts |
67 | | - opts = { |
68 | | - from: file, |
69 | | - to: file, |
70 | | - map: false |
71 | | - } |
72 | | - if ( |
73 | | - this.sourceMap && |
74 | | - !this.minimize && |
75 | | - options.cssSourceMap !== false && |
76 | | - process.env.NODE_ENV !== 'production' && |
77 | | - !(isObject(postcssOptions) && postcssOptions.options && postcssOptions.map) |
78 | | - ) { |
79 | | - opts.map = { |
80 | | - inline: false, |
81 | | - annotation: false, |
82 | | - prev: map |
| 68 | + plugins = [trim].concat(plugins || []) |
| 69 | + options = Object.assign({ |
| 70 | + to: this.resourcePath, |
| 71 | + from: this.resourcePath, |
| 72 | + map: false |
| 73 | + }, options) |
| 74 | + |
| 75 | + // merge postcss config file |
| 76 | + if (config && config.plugins) { |
| 77 | + plugins = plugins.concat(config.plugins) |
| 78 | + } |
| 79 | + if (config && config.options) { |
| 80 | + options = Object.assign({}, config.options, options) |
| 81 | + } |
| 82 | + |
| 83 | + // add plugin for vue-loader scoped css rewrite |
| 84 | + if (query.scoped) { |
| 85 | + plugins.push(addId({ id: query.id })) |
83 | 86 | } |
84 | | - } |
85 | 87 |
|
86 | | - // postcss options from configuration |
87 | | - if (isObject(postcssOptions) && postcssOptions.options) { |
88 | | - for (var option in postcssOptions.options) { |
89 | | - if (!opts.hasOwnProperty(option)) { |
90 | | - opts[option] = postcssOptions.options[option] |
| 88 | + // source map |
| 89 | + if ( |
| 90 | + this.sourceMap && |
| 91 | + !this.minimize && |
| 92 | + vueOptions.cssSourceMap !== false && |
| 93 | + process.env.NODE_ENV !== 'production' && |
| 94 | + !options.map |
| 95 | + ) { |
| 96 | + options.map = { |
| 97 | + inline: false, |
| 98 | + annotation: false, |
| 99 | + prev: map |
91 | 100 | } |
92 | 101 | } |
93 | | - } |
94 | 102 |
|
95 | | - postcss(plugins) |
96 | | - .process(css, opts) |
97 | | - .then(function (result) { |
98 | | - var map = result.map && result.map.toJSON() |
99 | | - cb(null, result.css, map) |
100 | | - return null // silence bluebird warning |
101 | | - }) |
102 | | - .catch(function (e) { |
103 | | - console.log(e) |
104 | | - cb(e) |
105 | | - }) |
| 103 | + postcss(plugins) |
| 104 | + .process(css, options) |
| 105 | + .then(function (result) { |
| 106 | + var map = result.map && result.map.toJSON() |
| 107 | + cb(null, result.css, map) |
| 108 | + return null // silence bluebird warning |
| 109 | + }) |
| 110 | + .catch(function (e) { |
| 111 | + console.log(e) |
| 112 | + cb(e) |
| 113 | + }) |
| 114 | + }) |
106 | 115 | } |
107 | 116 |
|
108 | 117 | function isObject (val) { |
|
0 commit comments