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-23 04:09:03 +00:00
|
|
|
import PostcssConfig from './postcss'
|
2017-08-14 14:03:07 +00:00
|
|
|
|
2018-03-23 04:09:03 +00:00
|
|
|
export default class StyleLoader {
|
|
|
|
constructor(options, nuxt, { isServer }) {
|
|
|
|
this.isServer = isServer
|
|
|
|
this.dev = options.dev
|
|
|
|
this.srcDir = options.srcDir
|
|
|
|
this.assetsDir = options.dir.assets
|
|
|
|
this.staticDir = options.dir.static
|
|
|
|
this.extractCSS = options.build.extractCSS
|
|
|
|
this.resources = options.build.styleResources
|
|
|
|
this.sourceMap = Boolean(options.build.cssSourceMap)
|
2017-12-28 15:30:31 +00:00
|
|
|
|
2018-03-23 04:09:03 +00:00
|
|
|
if (options.build.postcss) {
|
|
|
|
this.postcssConfig = new PostcssConfig(options, nuxt)
|
|
|
|
}
|
|
|
|
}
|
2017-12-28 15:30:31 +00:00
|
|
|
|
2018-03-23 04:09:03 +00:00
|
|
|
normalize(loaders) {
|
|
|
|
loaders = Array.isArray(loaders) ? loaders : [loaders]
|
|
|
|
return loaders.map(loader => Object.assign(
|
|
|
|
{ options: { sourceMap: this.sourceMap } },
|
|
|
|
typeof loader === 'string' ? { loader } : loader
|
|
|
|
))
|
|
|
|
}
|
2017-12-28 15:30:31 +00:00
|
|
|
|
2018-03-23 04:09:03 +00:00
|
|
|
styleResource(ext, loaders) {
|
|
|
|
const extResource = this.resources[ext]
|
2018-03-21 11:34:16 +00:00
|
|
|
// style-resources-loader
|
|
|
|
// https://github.com/yenshih/style-resources-loader
|
2018-03-23 04:09:03 +00:00
|
|
|
if (extResource) {
|
|
|
|
const patterns = Array.isArray(extResource)
|
|
|
|
? extResource
|
|
|
|
: [extResource]
|
2017-08-14 14:03:07 +00:00
|
|
|
|
2018-03-21 11:34:16 +00:00
|
|
|
loaders.push({
|
|
|
|
loader: 'style-resources-loader',
|
2018-03-23 04:09:03 +00:00
|
|
|
options: Object.assign(
|
|
|
|
{ patterns },
|
|
|
|
this.resources.options || {}
|
|
|
|
)
|
2017-12-28 16:05:34 +00:00
|
|
|
})
|
|
|
|
}
|
2018-03-23 04:09:03 +00:00
|
|
|
}
|
2017-08-14 14:03:07 +00:00
|
|
|
|
2018-03-23 04:09:03 +00:00
|
|
|
postcss(loaders) {
|
2018-03-21 11:34:16 +00:00
|
|
|
// postcss-loader
|
|
|
|
// https://github.com/postcss/postcss-loader
|
2018-03-23 04:09:03 +00:00
|
|
|
if (this.postcssConfig) {
|
|
|
|
const config = this.postcssConfig.config()
|
|
|
|
if (config) {
|
|
|
|
loaders.unshift({
|
|
|
|
loader: 'postcss-loader',
|
|
|
|
options: Object.assign({ sourceMap: this.sourceMap }, config)
|
|
|
|
})
|
|
|
|
}
|
2017-08-14 14:03:07 +00:00
|
|
|
}
|
2018-03-23 04:09:03 +00:00
|
|
|
}
|
2017-08-14 14:03:07 +00:00
|
|
|
|
2018-03-23 04:09:03 +00:00
|
|
|
css(loaders) {
|
2018-03-21 11:34:16 +00:00
|
|
|
// css-loader
|
|
|
|
// https://github.com/webpack-contrib/css-loader
|
2018-03-23 04:09:03 +00:00
|
|
|
const cssLoaderAlias = {
|
|
|
|
[`/${this.assetsDir}`]: path.join(this.srcDir, this.assetsDir),
|
|
|
|
[`/${this.staticDir}`]: path.join(this.srcDir, this.staticDir)
|
|
|
|
}
|
2018-03-21 11:34:16 +00:00
|
|
|
|
|
|
|
loaders.unshift({
|
|
|
|
loader: 'css-loader',
|
|
|
|
options: {
|
2018-03-23 04:09:03 +00:00
|
|
|
sourceMap: this.sourceMap,
|
|
|
|
minimize: !this.dev,
|
2018-03-21 11:34:16 +00:00
|
|
|
importLoaders: loaders.length, // Important!
|
|
|
|
alias: cssLoaderAlias
|
|
|
|
}
|
|
|
|
})
|
2018-03-23 04:09:03 +00:00
|
|
|
}
|
2018-03-21 11:34:16 +00:00
|
|
|
|
2018-03-23 04:09:03 +00:00
|
|
|
extract(loaders) {
|
|
|
|
if (this.extractCSS && !this.isServer) {
|
|
|
|
loaders.unshift(MiniCssExtractPlugin.loader)
|
|
|
|
if (this.dev) {
|
|
|
|
// css-hot-loader
|
|
|
|
// https://github.com/shepherdwind/css-hot-loader
|
|
|
|
loaders.unshift({
|
|
|
|
loader: 'css-hot-loader',
|
|
|
|
options: { sourceMap: this.sourceMap }
|
|
|
|
})
|
2018-03-21 11:34:16 +00:00
|
|
|
}
|
2018-03-23 04:09:03 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vueStyle(loaders) {
|
|
|
|
// https://github.com/vuejs/vue-style-loader
|
|
|
|
loaders.unshift({
|
|
|
|
loader: 'vue-style-loader',
|
|
|
|
options: { sourceMap: this.sourceMap }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
apply(ext, loaders = []) {
|
|
|
|
// Normalize loaders
|
|
|
|
loaders = this.normalize(loaders)
|
|
|
|
|
|
|
|
// -- Configure additional loaders --
|
|
|
|
this.styleResource(ext, loaders)
|
|
|
|
this.postcss(loaders)
|
|
|
|
this.css(loaders)
|
|
|
|
if (!this.extract(loaders)) {
|
|
|
|
this.vueStyle(loaders)
|
2017-11-20 23:56:14 +00:00
|
|
|
}
|
2018-03-23 04:09:03 +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
|
|
|
}
|