This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathindex.js
84 lines (78 loc) · 2.43 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const merge = require('deepmerge');
module.exports = (neutrino, opts = {}) => {
const modules = 'modules' in opts ? opts.modules : true;
const modulesTest = opts.modulesTest || neutrino.regexFromExtensions(['module.css']);
const options = merge({
test: neutrino.regexFromExtensions(['css']),
ruleId: 'style',
styleUseId: 'style',
cssUseId: 'css',
css: {
importLoaders: opts.loaders ? opts.loaders.length : 0
},
style: {},
modules,
modulesTest,
modulesSuffix: '-modules',
exclude: modules && modulesTest,
loaders: [],
extractId: 'extract',
extract: {
loader: {},
plugin: {
filename: process.env.NODE_ENV === 'production'
? '[name].[contenthash:8].css'
: '[name].css'
}
}
}, opts);
const rules = [options];
if (options.modules) {
rules.push(
merge(options, {
test: options.modulesTest,
exclude: options.modulesExclude,
ruleId: `${options.ruleId}${options.modulesSuffix}`,
styleUseId: `${options.styleUseId}${options.modulesSuffix}`,
cssUseId: `${options.cssUseId}${options.modulesSuffix}`,
extractId: `${options.extractId}${options.modulesSuffix}`,
css: {
modules: options.modules
}
})
);
}
rules.forEach(options => {
const styleRule = neutrino.config.module.rule(options.ruleId);
const loaders = [
{
loader: options.extract ? require.resolve('mini-css-extract-plugin/dist/loader') : require.resolve('style-loader'),
options: options.extract ? options.extract.loader : options.style,
useId: options.extract ? options.extractId : options.styleUseId
},
{
loader: require.resolve('css-loader'),
options: options.css,
useId: options.cssUseId
},
...options.loaders
]
.map((loader, index) => ({
useId: `${options.cssUseId}-${index}`,
...(typeof loader === 'object' ? loader : { loader })
}));
loaders.forEach(loader => {
styleRule
.test(options.test)
.when(options.exclude, rule => rule.exclude.add(options.exclude))
.use(loader.useId)
.loader(loader.loader)
.when(loader.options, use => use.options(loader.options));
});
});
if (options.extract) {
neutrino.config
.plugin(options.extractId)
.use(require.resolve('mini-css-extract-plugin'), [options.extract.plugin]);
}
};