-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpackFinal.js
77 lines (68 loc) · 1.67 KB
/
webpackFinal.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
const defaultOptions = {
default: {
importLoaders: 1,
modules: {
localIdentName: "[path][name]__[local]--[hash:base64:5]",
},
},
vue3: {
modules: {
localIdentName: "[path][name]__[local]--[hash:base64:5]",
},
}
}
export async function webpackFinal(config = {}, options = {}) {
const { module = {} } = config;
const { framework } = options;
const {
cssModulesLoaderOptions = defaultOptions[framework] || defaultOptions.default,
} = options;
const newConfig = {
...config,
module: {
...module,
rules: [...(module.rules || [])],
},
};
const cssLoaderRule = newConfig.module.rules.find(
(rule) => rule?.test?.toString() === "/\\.css$/"
);
switch (framework) {
case "vue3":
cssLoaderRule.use = cssLoaderRule.use.map((item) => {
if (item?.loader?.match(/[\/\\]css-loader/g)) {
return {
...item,
options: {
...item.options,
...cssModulesLoaderOptions,
},
};
}
return item;
})
break;
default:
if (cssLoaderRule) {
newConfig.module.rules.push({
...cssLoaderRule,
test: /\.module\.css$/,
use: cssLoaderRule.use.map((item) => {
if (item?.loader?.match(/[\/\\]css-loader/g)) {
return {
...item,
options: {
...item.options,
...cssModulesLoaderOptions,
},
};
}
return item;
}),
});
cssLoaderRule.exclude = /\.module\.css$/;
}
break;
}
return newConfig;
}