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
123 lines (112 loc) · 3.52 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const merge = require('deepmerge');
module.exports = (neutrino, opts = {}) => {
const modules = 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: {},
hot: true,
hotUseId: 'hot',
modules,
modulesTest,
modulesSuffix: '-modules',
exclude: modules && modulesTest,
loaders: [],
extractId: 'extract',
extract: {
plugin: {
filename: neutrino.options.command === 'build' ? '[name].[contenthash].css' : '[name].css',
ignoreOrder: opts.modules !== false,
allChunks: true
}
}
}, 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}`,
hotUseId: `${options.hotUseId}${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: require.resolve('style-loader'),
options: options.style,
useId: options.styleUseId
},
{
loader: require.resolve('css-loader'),
options: options.css,
useId: options.cssUseId
},
...options.loaders
]
.map((loader, index) => {
const obj = typeof loader === 'object' ? loader : { loader };
return Object.assign(obj, {
useId: obj.useId || `${options.cssUseId}-${index}`
});
});
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) {
const styleEntries = styleRule.uses.entries();
const useIds = Object.keys(styleEntries).filter(key => key !== options.styleUseId);
const extractLoader = Object.assign({
use: useIds.map(key => ({
loader: styleEntries[key].get('loader'),
options: styleEntries[key].get('options')
})),
fallback: {
loader: styleEntries[options.styleUseId].get('loader'),
options: styleEntries[options.styleUseId].get('options')
}
}, options.extract.loader || {});
styleRule
.uses
.clear()
.end()
.when(options.hot, (rule) => {
rule.use(options.hotUseId)
.loader(require.resolve('css-hot-loader'))
.when(options.hot !== true, use => use.options(options.hot));
});
ExtractTextPlugin
.extract(extractLoader)
.forEach(({ loader, options }) => {
styleRule
.use(loader)
.loader(loader)
.options(options);
});
neutrino.config
.plugin(options.extractId)
.use(ExtractTextPlugin, [options.extract.plugin]);
}
});
};