-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
47 lines (40 loc) · 1.29 KB
/
index.ts
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
import { Configuration, RuleSetRule } from "webpack";
const processed = new Set();
function addLinaria(config: Configuration) {
const rules = config.module?.rules as RuleSetRule[];
const babelLoaderIndex = rules?.findIndex(
(rule) =>
typeof rule !== "string" &&
(rule as RuleSetRule).loader?.toString().includes("babel-loader")
);
if (typeof babelLoaderIndex === "number" && babelLoaderIndex !== -1) {
const babelLoader = rules[babelLoaderIndex];
const babelLoaderClone = (({ test, exclude, ...o }) => o)(babelLoader);
rules[babelLoaderIndex] = {
test: babelLoader.test,
exclude: babelLoader.exclude,
use: [
babelLoaderClone,
{
loader: "@wyw-in-js/webpack-loader",
options: {
sourceMap: config.mode === "development",
},
},
],
};
} else {
throw new Error(
`NX Linaria: No \`babel-loader\` found. Make sure you have \`babel\` compiler selected in your \`project.json\`\n
https://nx.dev/packages/webpack/executors/webpack#compiler`
);
}
}
export function withLinaria() {
return function configure(config: Configuration): Configuration {
if (processed.has(config)) return config;
addLinaria(config);
processed.add(config);
return config;
};
}