-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.cjs
More file actions
88 lines (72 loc) · 2.21 KB
/
Copy pathplugin.cjs
File metadata and controls
88 lines (72 loc) · 2.21 KB
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
const path = require("node:path");
const loaderPath = path.resolve(__dirname, "loader.cjs");
const TURBOPACK_RULE_GLOB = "*.{tsx,jsx}";
function resolvePaths(projectDir, paths, fallback) {
const entries = Array.isArray(paths) && paths.length > 0 ? paths : fallback;
return entries.map((entry) =>
path.isAbsolute(entry) ? entry : path.resolve(projectDir, entry)
);
}
function createLoaderOptions(projectRoot, include, exclude) {
return {
projectRoot,
includePaths: resolvePaths(projectRoot, include, [projectRoot]),
excludePaths: resolvePaths(projectRoot, exclude, [])
};
}
function mergeTurbopackRule(existingRule, newRule) {
if (!existingRule) {
return newRule;
}
if (Array.isArray(existingRule)) {
return [...existingRule, newRule];
}
return [existingRule, newRule];
}
function withNextray(nextConfig = {}, options = {}) {
const {
enabled = true,
include = ["."],
exclude = []
} = options;
const projectRoot = process.cwd();
const loaderOptions = createLoaderOptions(projectRoot, include, exclude);
const userWebpack = nextConfig.webpack;
const nextTurbopackConfig = { ...(nextConfig.turbopack ?? {}) };
if (enabled) {
const currentRules = { ...(nextTurbopackConfig.rules ?? {}) };
const nextrayRule = {
condition: "development",
loaders: [{ loader: loaderPath, options: loaderOptions }]
};
currentRules[TURBOPACK_RULE_GLOB] = mergeTurbopackRule(
currentRules[TURBOPACK_RULE_GLOB],
nextrayRule
);
nextTurbopackConfig.rules = currentRules;
}
return {
...nextConfig,
turbopack: nextTurbopackConfig,
webpack(config, context) {
const nextWebpackConfig =
typeof userWebpack === "function" ? userWebpack(config, context) || config : config;
if (enabled && context.dev) {
nextWebpackConfig.module.rules.unshift({
test: /\.[jt]sx$/,
include: loaderOptions.includePaths,
exclude: [/node_modules/, /\.next/],
enforce: "pre",
use: [
{
loader: loaderPath,
options: loaderOptions
}
]
});
}
return nextWebpackConfig;
}
};
}
module.exports = { withNextray };