forked from andywer/webpack-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (43 loc) · 1.51 KB
/
index.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
/**
* CSS Modules webpack block.
*
* @see https://github.com/webpack/css-loader
*/
module.exports = cssModules
/**
* @param {object} [options]
* @param {RegExp, Function, string} [options.exclude] Directories to exclude.
* @param {number} [options.importLoaders]
* @param {string} [options.localIdentName]
* @return {Function}
*/
function cssModules (options) {
options = options || {}
const localIdentDefault = String(process.env.NODE_ENV) === 'production'
? '[hash:base64:10]'
: '[name]--[local]--[hash:base64:5]'
const exclude = options.exclude || /\/node_modules\//
const importLoaders = options.importLoaders || 1
const localIdentName = options.localIdentName || localIdentDefault
return (fileTypes) => ({
module: {
loaders: [
{
test: fileTypes('text/css'),
exclude: Array.isArray(exclude) ? exclude : [ exclude ],
loaders: [ 'style-loader', 'css-loader?' + stringifyQueryParams({ importLoaders, localIdentName, modules: true }) ]
}
]
}
})
}
/**
* Cannot use `require('querystring').stringify()`, since css-loader params might
* contain special characters as `[`, `]` that would be escaped.
*/
function stringifyQueryParams (params) {
return Object.keys(params)
.filter((paramName) => (params[ paramName ] !== null && params[ paramName ] !== undefined))
.map((paramName) => (params[ paramName ] === true ? `${paramName}` : `${paramName}=${params[ paramName ]}`))
.join('&')
}