2018-03-16 19:11:24 +00:00
|
|
|
import path from 'path'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-03-21 10:16:03 +00:00
|
|
|
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
import postcssConfig from './postcss'
|
2017-08-14 14:03:07 +00:00
|
|
|
|
2018-03-21 11:34:16 +00:00
|
|
|
export default ({isVueLoader = false, isServer}) => {
|
|
|
|
return function styleLoader(ext, loaders = []) {
|
|
|
|
const sourceMap = Boolean(this.options.build.cssSourceMap)
|
2017-12-28 15:30:31 +00:00
|
|
|
|
2018-03-21 11:34:16 +00:00
|
|
|
// Normalize loaders
|
|
|
|
loaders = (Array.isArray(loaders) ? loaders : [loaders]).map(loader =>
|
|
|
|
Object.assign(
|
|
|
|
{ options: { sourceMap } },
|
|
|
|
typeof loader === 'string' ? { loader } : loader
|
|
|
|
)
|
2018-01-13 05:22:11 +00:00
|
|
|
)
|
2017-12-28 15:30:31 +00:00
|
|
|
|
2018-03-21 11:34:16 +00:00
|
|
|
// -- Configure additional loaders --
|
2017-12-28 15:30:31 +00:00
|
|
|
|
2018-03-21 11:34:16 +00:00
|
|
|
// style-resources-loader
|
|
|
|
// https://github.com/yenshih/style-resources-loader
|
|
|
|
if (this.options.build.styleResources[ext]) {
|
|
|
|
const patterns = Array.isArray(this.options.build.styleResources[ext])
|
|
|
|
? this.options.build.styleResources[ext]
|
|
|
|
: [this.options.build.styleResources[ext]]
|
|
|
|
const options = Object.assign(
|
|
|
|
{},
|
|
|
|
this.options.build.styleResources.options || {},
|
|
|
|
{ patterns }
|
|
|
|
)
|
2017-08-14 14:03:07 +00:00
|
|
|
|
2018-03-21 11:34:16 +00:00
|
|
|
loaders.push({
|
|
|
|
loader: 'style-resources-loader',
|
|
|
|
options
|
2017-12-28 16:05:34 +00:00
|
|
|
})
|
|
|
|
}
|
2017-08-14 14:03:07 +00:00
|
|
|
|
2018-03-21 11:34:16 +00:00
|
|
|
// postcss-loader
|
|
|
|
// vue-loader already provides it's own
|
|
|
|
// https://github.com/postcss/postcss-loader
|
|
|
|
if (!isVueLoader) {
|
|
|
|
const _postcssConfig = postcssConfig.call(this)
|
2018-02-03 11:10:06 +00:00
|
|
|
|
2018-03-21 11:34:16 +00:00
|
|
|
if (_postcssConfig) {
|
|
|
|
loaders.unshift({
|
|
|
|
loader: 'postcss-loader',
|
|
|
|
options: Object.assign({ sourceMap }, _postcssConfig)
|
|
|
|
})
|
|
|
|
}
|
2017-08-14 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 11:34:16 +00:00
|
|
|
// css-loader
|
|
|
|
// https://github.com/webpack-contrib/css-loader
|
|
|
|
const cssLoaderAlias = {}
|
|
|
|
cssLoaderAlias[`/${this.options.dir.assets}`] = path.join(this.options.srcDir, this.options.dir.assets)
|
|
|
|
cssLoaderAlias[`/${this.options.dir.static}`] = path.join(this.options.srcDir, this.options.dir.static)
|
|
|
|
|
|
|
|
loaders.unshift({
|
|
|
|
loader: 'css-loader',
|
|
|
|
options: {
|
|
|
|
sourceMap,
|
|
|
|
minimize: !this.options.dev,
|
|
|
|
importLoaders: loaders.length, // Important!
|
|
|
|
alias: cssLoaderAlias
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// -- With extractCSS --
|
2018-03-21 12:45:58 +00:00
|
|
|
if (this.options.build.extractCSS) {
|
|
|
|
if (!isServer) {
|
|
|
|
loaders.unshift(MiniCssExtractPlugin.loader)
|
|
|
|
if (this.options.dev) {
|
|
|
|
// css-hot-loader
|
|
|
|
// https://github.com/shepherdwind/css-hot-loader
|
|
|
|
loaders.unshift({
|
|
|
|
loader: 'css-hot-loader',
|
|
|
|
options: { sourceMap }
|
|
|
|
})
|
|
|
|
}
|
2018-03-21 11:34:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Prepare vue-style-loader
|
|
|
|
// https://github.com/vuejs/vue-style-loader
|
2018-03-21 10:16:03 +00:00
|
|
|
loaders.unshift({
|
2018-03-21 11:34:16 +00:00
|
|
|
loader: 'vue-style-loader',
|
2018-03-21 10:16:03 +00:00
|
|
|
options: { sourceMap }
|
|
|
|
})
|
2017-11-20 23:56:14 +00:00
|
|
|
}
|
2018-03-21 11:34:16 +00:00
|
|
|
return loaders
|
2017-11-20 23:56:14 +00:00
|
|
|
}
|
2017-08-14 14:03:07 +00:00
|
|
|
}
|