refactor sryle-loader

makes code more clear and (temporary) fixing dev source maps when extractCSS is on
This commit is contained in:
Pooya Parsa 2017-12-28 19:00:31 +03:30
parent 6fabea392d
commit 4e22416002
3 changed files with 55 additions and 81 deletions

View File

@ -1,7 +1,6 @@
const ExtractTextPlugin = require('extract-text-webpack-plugin') const ExtractTextPlugin = require('extract-text-webpack-plugin')
const { cloneDeep } = require('lodash') const { cloneDeep } = require('lodash')
const { join, resolve } = require('path') const { join, resolve } = require('path')
const webpack = require('webpack')
const { isUrl, urlJoin } = require('../../common/utils') const { isUrl, urlJoin } = require('../../common/utils')
const TimeFixPlugin = require('./plugins/timefix') const TimeFixPlugin = require('./plugins/timefix')
const WarnFixPlugin = require('./plugins/warnfix') const WarnFixPlugin = require('./plugins/warnfix')
@ -116,25 +115,6 @@ module.exports = function webpackBaseConfig({ name, isServer }) {
config.plugins.push(new ExtractTextPlugin(extractOptions)) config.plugins.push(new ExtractTextPlugin(extractOptions))
} }
// --------------------------------------
// Dev specific config
// --------------------------------------
if (this.options.dev) {
//
}
// --------------------------------------
// Production specific config
// --------------------------------------
if (!this.options.dev) {
// This is needed in webpack 2 for minify CSS
config.plugins.push(
new webpack.LoaderOptionsPlugin({
minimize: true
})
)
}
// Clone deep avoid leaking config between Client and Server // Clone deep avoid leaking config between Client and Server
return cloneDeep(config) return cloneDeep(config)
} }

View File

@ -62,13 +62,6 @@ module.exports = function webpackServerConfig() {
} }
}) })
// --------------------------------------
// Production specific config
// --------------------------------------
if (!this.options.dev) {
}
// Extend config // Extend config
if (typeof this.options.build.extend === 'function') { if (typeof this.options.build.extend === 'function') {
const isDev = this.options.dev const isDev = this.options.dev

View File

@ -2,75 +2,76 @@ const ExtractTextPlugin = require('extract-text-webpack-plugin')
const { join } = require('path') const { join } = require('path')
module.exports = function styleLoader(ext, loaders = [], isVueLoader = false) { module.exports = function styleLoader(ext, loaders = [], isVueLoader = false) {
const sourceMap = Boolean(this.options.build.cssSourceMap)
// Normalize loaders // Normalize loaders
loaders = (Array.isArray(loaders) ? loaders : [loaders]).map(loader => { loaders = (Array.isArray(loaders) ? loaders : [loaders])
if (typeof loader === 'string') { .map(loader => Object.assign({ options: { sourceMap } }, typeof loader === 'string' ? { loader } : loader))
loader = { loader }
// Prepare vue-style-loader
// https://github.com/vuejs/vue-style-loader
const vueStyleLoader = {
loader: 'vue-style-loader',
options: { sourceMap }
} }
return Object.assign({
options: { // -- Configure additional loaders --
sourceMap: this.options.build.cssSourceMap
} // style-resources-loader
}, loader) // https://github.com/yenshih/style-resources-loader
if (this.options.build.styleResources) {
loaders.push({
loader: 'style-resources-loader',
options: Object.assign({ sourceMap }, this.options.build.styleResources)
}) })
}
// postcss-loader
// vue-loader already provides it's own
// https://github.com/postcss/postcss-loader // https://github.com/postcss/postcss-loader
let postcssLoader
if (!isVueLoader && this.options.build.postcss) { if (!isVueLoader && this.options.build.postcss) {
postcssLoader = { loaders.unshift({
loader: 'postcss-loader', loader: 'postcss-loader',
options: this.options.build.postcss options: Object.assign({ sourceMap }, this.options.build.postcss)
} })
} }
// css-loader
// https://github.com/webpack-contrib/css-loader // https://github.com/webpack-contrib/css-loader
const cssLoader = { loaders.unshift({
loader: 'css-loader', loader: 'css-loader',
options: { options: {
minimize: true, sourceMap,
importLoaders: 1, minimize: !this.options.dev,
sourceMap: this.options.build.cssSourceMap, importLoaders: loaders.length, // Important!
alias: { alias: {
'/static': join(this.options.srcDir, 'static'), '/static': join(this.options.srcDir, 'static'),
'/assets': join(this.options.srcDir, 'assets') '/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
}
}
if (this.options.build.extractCSS) {
const extraction = ExtractTextPlugin.extract({
fallback: vueStyleLoader,
use: [
cssLoader,
postcssLoader,
...loaders
].filter(l => l)
}) })
return this.options.dev ? ['css-hot-loader'].concat(extraction) : extraction
// -- With extractCSS --
// TODO: Temporary disabled in dev mode for fixing source maps
// (We need `source-map` devtool for *.css modules)
if (this.options.build.extractCSS && !this.options.dev) {
// ExtractTextPlugin
// https://github.com/webpack-contrib/extract-text-webpack-plugin
const extractLoader = ExtractTextPlugin.extract({
use: loaders,
fallback: vueStyleLoader
})
// css-hot-loader
// https://github.com/shepherdwind/css-hot-loader
const hotLoader = {
loader: 'css-hot-loader',
options: { sourceMap }
} }
// https://github.com/yenshih/style-resources-loader return this.options.dev ? [ hotLoader ].concat(extractLoader) : extractLoader
let styleResourcesLoader
if (this.options.build.styleResources) {
styleResourcesLoader = {
loader: 'style-resources-loader',
options: this.options.build.styleResources
}
} }
return [ // -- Without extractCSS --
vueStyleLoader, return [ vueStyleLoader ].concat(loaders)
cssLoader,
postcssLoader,
...loaders,
styleResourcesLoader
].filter(l => l)
} }