-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
get_style_rule.js
62 lines (54 loc) · 1.56 KB
/
get_style_rule.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const path = require('path')
const devServer = require('../dev_server')
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 getStyleRule = (test, modules = false, preprocessors = []) => {
const extractOptions = {
fallback: styleLoader,
use: [
{
loader: 'css-loader',
options: {
minimize: isProduction,
sourceMap: true,
importLoaders: 2,
modules
}
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
config: { path: postcssConfigPath }
}
},
...preprocessors
]
}
const options = modules ? { include: /\.module\.[a-z]+$/ } : { exclude: /\.module\.[a-z]+$/ }
// For production extract styles to a separate bundle
const extractCSSLoader = Object.assign(
{},
{ test, use: ExtractTextPlugin.extract(extractOptions) },
options
)
// For hot-reloading use regular loaders
const inlineCSSLoader = Object.assign(
{},
{ test, use: [styleLoader].concat(extractOptions.use) },
options
)
return extractCSS ? extractCSSLoader : inlineCSSLoader
}
module.exports = getStyleRule