Nuxt/lib/builder/webpack/utils/style-loader.js

90 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-03-16 19:11:24 +00:00
import path from 'path'
2018-03-16 19:52:17 +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-22 09:11:16 +00:00
export default ({ isServer }) => {
return function styleLoader(ext, loaders = []) {
const sourceMap = Boolean(this.options.build.cssSourceMap)
// 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
)
// -- Configure additional loaders --
// 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
loaders.push({
loader: 'style-resources-loader',
options
2017-12-28 16:05:34 +00:00
})
}
2017-08-14 14:03:07 +00:00
// postcss-loader
// https://github.com/postcss/postcss-loader
2018-03-22 09:11:16 +00:00
const _postcssConfig = postcssConfig.call(this)
2018-02-03 11:10:06 +00:00
2018-03-22 09:11:16 +00:00
if (_postcssConfig) {
loaders.unshift({
loader: 'postcss-loader',
options: Object.assign({ sourceMap }, _postcssConfig)
})
2017-08-14 14:03:07 +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 --
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 }
})
}
}
} else {
// Prepare vue-style-loader
// https://github.com/vuejs/vue-style-loader
loaders.unshift({
loader: 'vue-style-loader',
options: { sourceMap }
})
}
return loaders
}
2017-08-14 14:03:07 +00:00
}