-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathbuild.ts
More file actions
executable file
·217 lines (190 loc) · 6.38 KB
/
build.ts
File metadata and controls
executable file
·217 lines (190 loc) · 6.38 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env node --disable-warning=ExperimentalWarning --experimental-strip-types
/**
* Copyright (c) Pooya Parsa <pooya@pi0.io>
*
* Please do not copy and paste the code, it is under development and planned to be released as an standalone package.
*/
import { fileURLToPath } from "node:url";
import { dirname, extname, join, relative } from "node:path";
import { glob, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import oxcTransform from "oxc-transform";
import oxcParser from "oxc-parser";
import oxcResolver from "oxc-resolver";
import MagicString from 'magic-string'
import { rolldown } from "rolldown";
import { builtinModules } from "node:module";
import pkg from "../package.json" with { type: "json" };
const rootDir = fileURLToPath(new URL("../", import.meta.url));
const start = Date.now();
console.log(`Cleaning up dist/ ...`);
await rm(join(rootDir, "dist"), { recursive: true, force: true });
console.log(`Bundling src/index...`);
await rolldownBuild(rootDir, "src/index.ts", "dist/index.mjs");
console.log(`Building src/runtime...`);
await generateNodeVersion(rootDir, "src/runtime/node/internal/process");
await transformDir(rootDir, "src/runtime", "dist/runtime");
console.log(`Build finished in ${Date.now() - start}ms`);
process.exit(0);
/**
* Transform all .ts modules in a directory using oxc-transform.
*/
async function transformDir(cwd: string, input: string, output: string) {
const start = Date.now();
const srcDir = join(cwd, input);
const distDir = join(cwd, output);
const promises: Promise<void>[] = [];
for await (const entryName of glob("**/*.*", { cwd: srcDir })) {
promises.push(
(async () => {
const entryPath = join(srcDir, entryName);
const ext = extname(entryPath);
switch (ext) {
case ".ts":
{
const transformed = await transformModule(entryPath);
const entryDistPath = join(
distDir,
entryName.replace(/\.ts$/, ".mjs"),
);
await mkdir(dirname(entryDistPath), { recursive: true });
await writeFile(entryDistPath, transformed.code, "utf8");
await writeFile(
entryDistPath.replace(/\.mjs$/, ".d.mts"),
transformed.declaration!,
"utf8",
);
}
break;
default:
{
const entryDistPath = join(distDir, entryName);
await mkdir(dirname(entryDistPath), { recursive: true });
await writeFile(entryDistPath, await readFile(entryPath), "utf8");
}
break;
}
})(),
);
}
await Promise.all(promises);
console.log(
`Transformed ${promises.length} files from ${input} into ${output} in ${Date.now() - start}ms`,
);
}
/**
* Transform a .ts module using oxc-transform.
*/
async function transformModule(entryPath: string) {
let sourceText = await readFile(entryPath, "utf8");
const sourceOptions = {
lang: "ts",
sourceType: "module",
} as const;
const parsed = oxcParser.parseSync(entryPath, sourceText, {
...sourceOptions,
});
if (parsed.errors.length > 0) {
throw new Error(`Errors while parsing ${entryPath}:`, {
cause: parsed.errors,
});
}
const magicString = new MagicString(sourceText)
// Rewrite relative imports
const updatedStarts = new Set<number>();
const rewriteSpecifier = (req: {
value: string;
start: number;
end: number;
}) => {
const moduleId = req.value;
if (!moduleId.startsWith(".")) {
return;
}
if (updatedStarts.has(req.start)) {
return; // prevent double rewritings
}
updatedStarts.add(req.start);
const resolvedAbsolute = resolvePath(moduleId, entryPath);
const newId = relative(
dirname(entryPath),
resolvedAbsolute.replace(/\.ts$/, ".mjs"),
);
magicString.remove(req.start, req.end);
magicString.prependLeft(
req.start,
JSON.stringify(newId.startsWith(".") ? newId : `./${newId}`),
);
};
for (const staticImport of parsed.module.staticImports) {
rewriteSpecifier(staticImport.moduleRequest);
}
for (const staticExport of parsed.module.staticExports) {
for (const staticExportEntry of staticExport.entries) {
if (staticExportEntry.moduleRequest) {
rewriteSpecifier(staticExportEntry.moduleRequest);
}
}
}
sourceText = magicString.toString();
const transformed = oxcTransform.transform(entryPath, sourceText, {
...sourceOptions,
cwd: dirname(entryPath),
typescript: { declaration: { stripInternal: true } },
});
const transformErrors = transformed.errors.filter(
(err) => !err.message.includes("--isolatedDeclarations"),
);
if (transformErrors.length > 0) {
// console.log(sourceText);
await writeFile(
"build-dump.ts",
`/** Error dump for ${entryPath} */\n\n` + sourceText,
"utf8",
);
throw new Error(
`Errors while transforming ${entryPath}: (hint: check build-dump.ts)`,
{
cause: transformErrors,
},
);
}
return transformed;
}
async function rolldownBuild(cwd: string, input: string, output: string) {
const start = Date.now();
const res = await rolldown({
cwd,
input: input,
external: [
...builtinModules,
...builtinModules.map((m) => `node:${m}`),
...Object.keys(pkg.dependencies),
],
});
await res.write({ file: output });
await res.close();
console.log(`Bundled ${input} into ${output} in ${Date.now() - start}ms`);
}
function resolvePath(id: string, parent: string) {
for (const suffix of ["", "/index"]) {
for (const ext of ["", ".ts", ".mjs", ".cjs"]) {
const resolved = oxcResolver.sync(
dirname(parent),
`${id}${suffix}${ext}`,
).path;
if (resolved) {
return resolved;
}
}
}
const error = new Error(`Cannot resolve "${id}" from "${parent}"`);
Error.captureStackTrace?.(error, resolvePath);
throw error;
}
async function generateNodeVersion(rootDir: string, outPath: string) {
const m = (await readFile(join(rootDir, ".nvmrc"), "utf8")).match(/(?<version>\d+\.\d+\.\d+)/);
if (!m?.groups?.version) {
throw new Error('.nvrmc does not contain a valid Node version');
}
await writeFile(join(rootDir, outPath, 'node-version.ts'), `// Extracted from .nvmrc\nexport const NODE_VERSION = ${JSON.stringify(m.groups.version)};\n`, 'utf8');
}