2017-08-14 14:03:07 +00:00
|
|
|
import ExtractTextPlugin from 'extract-text-webpack-plugin'
|
|
|
|
import { join } from 'path'
|
|
|
|
|
|
|
|
export default function styleLoader (ext, loaders = [], isVueLoader = false) {
|
|
|
|
// Normalize loaders
|
|
|
|
loaders = (Array.isArray(loaders) ? loaders : [loaders]).map(loader => {
|
|
|
|
if (typeof loader === 'string') {
|
2017-08-21 20:08:39 +00:00
|
|
|
loader = { loader }
|
2017-08-14 14:03:07 +00:00
|
|
|
}
|
2017-08-21 20:08:39 +00:00
|
|
|
return Object.assign({
|
|
|
|
options: {
|
|
|
|
sourceMap: this.options.build.cssSourceMap
|
|
|
|
}
|
|
|
|
}, loader)
|
2017-08-14 14:03:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// https://github.com/postcss/postcss-loader
|
|
|
|
let postcssLoader
|
2017-08-14 21:13:08 +00:00
|
|
|
if (!isVueLoader && this.options.build.postcss) {
|
2017-08-14 14:03:07 +00:00
|
|
|
postcssLoader = {
|
|
|
|
loader: 'postcss-loader',
|
2017-08-15 01:06:56 +00:00
|
|
|
options: this.options.build.postcss
|
|
|
|
}
|
2017-08-14 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/webpack-contrib/css-loader
|
|
|
|
const cssLoader = {
|
|
|
|
loader: 'css-loader',
|
|
|
|
options: {
|
|
|
|
minimize: true,
|
|
|
|
importLoaders: 1,
|
|
|
|
sourceMap: this.options.build.cssSourceMap,
|
|
|
|
alias: {
|
|
|
|
'/static': join(this.options.srcDir, 'static'),
|
|
|
|
'/assets': join(this.options.srcDir, 'assets')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/vuejs/vue-style-loader
|
|
|
|
const vueStyleLoader = {
|
|
|
|
loader: 'vue-style-loader',
|
|
|
|
options: {
|
|
|
|
sourceMap: this.options.build.cssSourceMap
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 16:42:11 +00:00
|
|
|
if (this.options.build.extractCSS && !this.options.dev) {
|
2017-08-14 14:03:07 +00:00
|
|
|
return ExtractTextPlugin.extract({
|
|
|
|
fallback: vueStyleLoader,
|
|
|
|
use: [
|
|
|
|
cssLoader,
|
|
|
|
postcssLoader,
|
|
|
|
...loaders
|
|
|
|
].filter(l => l)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
vueStyleLoader,
|
|
|
|
cssLoader,
|
|
|
|
postcssLoader,
|
|
|
|
...loaders
|
|
|
|
].filter(l => l)
|
|
|
|
}
|