/
css.js
39 lines (33 loc) · 1.16 KB
/
css.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const path = require('path')
const { dev_server: devServer } = require('../config')
const postcssConfigPath = path.resolve(process.cwd(), '.postcssrc.yml')
const isProduction = process.env.NODE_ENV === 'production'
const inDevServer = process.argv.find(v => v.includes('webpack-dev-server'))
const isHMR = inDevServer && (devServer && devServer.hmr)
const extractCSS = !(isHMR) || isProduction
const styleLoader = {
loader: 'style-loader',
options: {
hmr: isHMR,
sourceMap: true
}
}
const extractOptions = {
fallback: styleLoader,
use: [
{ loader: 'css-loader', options: { minimize: isProduction, sourceMap: true, importLoaders: 2 } },
{ loader: 'postcss-loader', options: { sourceMap: true, config: { path: postcssConfigPath } } }
]
}
// For production extract styles to a separate bundle
const extractCSSLoader = {
test: /\.(css)$/i,
use: ExtractTextPlugin.extract(extractOptions)
}
// For hot-reloading use regular loaders
const inlineCSSLoader = {
test: /\.(css)$/i,
use: [styleLoader].concat(extractOptions.use)
}
module.exports = extractCSS ? extractCSSLoader : inlineCSSLoader