This commit is contained in:
chrosey
2017-09-13 07:52:34 +02:00
parent a1f16c37f4
commit 2340b0226b
24621 changed files with 2912161 additions and 149 deletions
+68
View File
@@ -0,0 +1,68 @@
var postcss = require('postcss')
var loaderUtils = require('loader-utils')
var loadPostcssConfig = require('./load-postcss-config')
var trim = require('./plugins/trim')
var scopeId = require('./plugins/scope-id')
module.exports = function (css, map) {
this.cacheable()
var cb = this.async()
var query = loaderUtils.getOptions(this) || {}
var vueOptions = this.options.__vueOptions__
if (!vueOptions) {
if (query.hasInlineConfig) {
this.emitError(
`\n [vue-loader] It seems you are using HappyPack with inline postcss ` +
`options for vue-loader. This is not supported because loaders running ` +
`in different threads cannot share non-serializable options. ` +
`It is recommended to use a postcss config file instead.\n` +
`\n See http://vue-loader.vuejs.org/en/features/postcss.html#using-a-config-file for more details.\n`
)
}
vueOptions = Object.assign({}, this.options.vue, this.vue)
}
// use the same config loading interface as postcss-loader
loadPostcssConfig(vueOptions.postcss).then(config => {
var plugins = [trim].concat(config.plugins)
var options = Object.assign({
to: this.resourcePath,
from: this.resourcePath,
map: false
}, config.options)
// add plugin for vue-loader scoped css rewrite
if (query.scoped) {
plugins.push(scopeId({ id: query.id }))
}
// source map
if (
this.sourceMap &&
!this.minimize &&
vueOptions.cssSourceMap !== false &&
process.env.NODE_ENV !== 'production' &&
!options.map
) {
options.map = {
inline: false,
annotation: false,
prev: map
}
}
return postcss(plugins)
.process(css, options)
.then(function (result) {
var map = result.map && result.map.toJSON()
cb(null, result.css, map)
return null // silence bluebird warning
})
}).catch(e => {
console.log(e)
cb(e)
})
}
+45
View File
@@ -0,0 +1,45 @@
var load = require('postcss-load-config')
var loaded
function isObject (val) {
return val && typeof val === 'object'
}
module.exports = function loadPostcssConfig (inlineConfig) {
if (process.env.VUE_LOADER_TEST || !loaded) {
loaded = load({}, null, { argv: false }).catch(() => {
// postcss-load-config throws error when no config file is found,
// but for us it's optional.
})
}
return loaded.then(config => {
var plugins = []
var options = {}
// inline postcss options for vue-loader
if (typeof inlineConfig === 'function') {
inlineConfig = inlineConfig.call(this, this)
}
if (Array.isArray(inlineConfig)) {
plugins = inlineConfig
} else if (isObject(inlineConfig)) {
plugins = inlineConfig.plugins || []
options = inlineConfig.options || {}
}
// merge postcss config file
if (config && config.plugins) {
plugins = plugins.concat(config.plugins)
}
if (config && config.options) {
options = Object.assign({}, config.options, options)
}
return {
plugins,
options
}
})
}
+27
View File
@@ -0,0 +1,27 @@
var postcss = require('postcss')
var selectorParser = require('postcss-selector-parser')
module.exports = postcss.plugin('add-id', function (opts) {
return function (root) {
root.each(function rewriteSelector (node) {
if (!node.selector) {
// handle media queries
if (node.type === 'atrule' && node.name === 'media') {
node.each(rewriteSelector)
}
return
}
node.selector = selectorParser(function (selectors) {
selectors.each(function (selector) {
var node = null
selector.each(function (n) {
if (n.type !== 'pseudo') node = n
})
selector.insertAfter(node, selectorParser.attribute({
attribute: opts.id
}))
})
}).process(node.selector).result
})
}
})
+11
View File
@@ -0,0 +1,11 @@
var postcss = require('postcss')
module.exports = postcss.plugin('trim', function (opts) {
return function (css) {
css.walk(function (node) {
if (node.type === 'rule' || node.type === 'atrule') {
node.raws.before = node.raws.after = '\n'
}
})
}
})