/
styles.js
53 lines (46 loc) · 1.43 KB
/
styles.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
import autoprefixer from 'autoprefixer'
import ExtractTextPlugin from 'extract-text-webpack-plugin'
import _ from 'lodash'
const _loaders = {
css: null,
scss: 'sass-loader',
sass: 'sass-loader?indentedSyntax',
}
const cssModulesConfig = '?modules&camelCase=dashes&localIdentName=[path][name]---[local]---[hash:base64:8]'
const postcssLoader = {
loader: 'postcss-loader',
options: {
plugins: () => [autoprefixer({browsers: 'last 2 version'})],
},
}
const cssLoader = (modules) => `css-loader${modules ? cssModulesConfig : ''}`
const use = (modules, ext) => _.compact([cssLoader(modules), postcssLoader, _loaders[ext]])
function rule(transform, ext) {
return {
test: new RegExp(`\\.${ext}$`),
oneOf: [
{
resourceQuery: /module/,
use: transform(use(true, ext)),
},
{
use: transform(use(false, ext)),
},
],
}
}
export function loaders(options) {
const transform = options.useDevServer
? (l) => ['style-loader', ...l]
: (l) => ExtractTextPlugin.extract({fallback: 'style-loader', use: l})
return _.keys(_loaders).map(_.partial(rule, transform))
}
// Render styles into separate cacheable file to prevent FOUC and
// optimize for critical rendering path.
export function plugins(options) {
return options.useDevServer
? []
: [new ExtractTextPlugin({
filename: options.useHashedAssetNames ? 'app.[contenthash].css' : 'app.css',
allChunks: true})]
}