Repository navigation
Expand file tree
/
Copy pathrslib.config.js
More file actions
146 lines (140 loc) · 4.16 KB
/
Copy pathrslib.config.js
File metadata and controls
146 lines (140 loc) · 4.16 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import path from "node:path";
import { defineConfig, mergeRslibConfig } from "@rslib/core";
const root = import.meta.dirname;
const components = [
"core",
"parser",
"generator",
"transformer",
"core-browser",
"formatter",
"formatter-js",
"formatter-prettier-plugin",
];
const pifeHint = "@swc-next-pife-(start|end)";
const pifeComment = new RegExp(`/\\* ${pifeHint} \\*/`, "g");
const baseConfig = defineConfig({
lib: components.map((component) => {
const directory = path.join(root, "npm", component);
const source = path.join(directory, "src-js");
const eagerDecoder = component === "core" || component === "parser";
return {
id: component,
format: "esm",
bundle: true,
syntax: "es2022",
source: {
entry: {
index: path.join(source, "index.js"),
},
},
...(eagerDecoder && { tools: { rspack: restoreDecoderHints } }),
output: {
target: "node",
distPath: { root: path.join(directory, "dist") },
// Native/Wasm build tools also write artifacts to dist.
cleanDistPath: false,
copy: [
{ from: path.join(source, "{*.d.ts,binding.js}"), to: "[name][ext]" },
],
autoExternal: false,
externals: [
"./binding.js",
"./swc-next-core.browser.mjs",
"@swc-next/walker",
],
minify: {
jsOptions: {
minimizerOptions: {
compress: {
module: true,
toplevel: false,
inline: 0,
reduce_vars: false,
},
mangle: { toplevel: false },
...(eagerDecoder && {
format: { comments: { regex: pifeHint } },
}),
},
},
},
},
};
}),
});
export default mergeRslibConfig(baseConfig, {
lib: [
{
id: "core-browser",
output: {
target: "web",
},
},
{
id: "formatter",
source: {
entry: {
document: path.join(root, "npm/formatter/src-js/document.js"),
},
},
},
{
id: "formatter-prettier-plugin",
output: {
externals: [
"@swc-next/formatter-js",
"prettier/doc",
"prettier/plugins/estree",
],
},
},
],
});
// =================================== AI Slop ================================================
function restoreDecoderHints(_, { appendPlugins, rspack }) {
const name = "DecoderCompilationHints";
appendPlugins({
apply(compiler) {
compiler.hooks.thisCompilation.tap(name, (compilation) => {
// Restore parentheses after SWC minifies the bundle and before source maps are emitted.
compilation.hooks.processAssets.tap(
{
name,
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE + 1,
},
() => {
const { source } = compilation.getAsset("index.js");
const code = source.source().toString();
const hints = [...code.matchAll(pifeComment)];
if (hints.length !== 10) {
throw new Error(
"Expected five pairs of decoder compilation hints",
);
}
const updated = new rspack.sources.ReplaceSource(source);
let depth = 0;
for (const hint of hints) {
const [comment, edge] = hint;
const start = hint.index;
const end = start + comment.length;
const opening = edge === "start";
const nextToFunction = opening
? /^\s*function\b/.test(code.slice(end))
: /}\s*$/.test(code.slice(0, start));
depth += opening ? 1 : -1;
if (depth < 0 || !nextToFunction) {
throw new Error("Misplaced decoder compilation hint");
}
updated.replace(start, end - 1, opening ? "(" : ")");
}
if (depth !== 0) {
throw new Error("Unbalanced decoder compilation hints");
}
compilation.updateAsset("index.js", updated);
},
);
});
},
});
}