-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathesbuild.config.js
More file actions
92 lines (80 loc) · 2.27 KB
/
esbuild.config.js
File metadata and controls
92 lines (80 loc) · 2.27 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
import esbuild from "esbuild";
import path from "node:path";
import { fileURLToPath } from "node:url";
import fs from "node:fs/promises";
import process from "node:process";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const args = process.argv.slice(2);
const pkgFlagIndex = args.indexOf("--package");
if (pkgFlagIndex === -1 || pkgFlagIndex + 1 >= args.length) {
console.error("Missing --package <path>");
process.exit(1);
}
const packagePath = args[pkgFlagIndex + 1];
const packageDir = path.isAbsolute(packagePath)
? packagePath
: path.resolve(process.cwd(), packagePath);
const entryFile = path.join(packageDir, "mod.ts");
try {
await fs.access(entryFile);
} catch {
console.error(`Expected entrypoint: ${entryFile}`);
process.exit(1);
}
// recursively discover .ts files
async function* walk(dir) {
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const e of entries) {
const full = path.join(dir, e.name);
if (e.isDirectory()) {
if (e.name === "dist") continue;
yield* walk(full);
} else if (e.isFile() && e.name.endsWith(".ts")) {
if (e.name.endsWith(".test.ts")) continue;
if (e.name.endsWith(".bench.ts")) continue;
yield full;
}
}
}
const allTs = [];
for await (const f of walk(packageDir)) allTs.push(f);
const rewriteTsImports = {
name: "rewrite-ts-imports",
setup(build) {
build.onLoad({ filter: /\.[tj]s$/ }, async args => {
const source = await fs.readFile(args.path, "utf8");
const rewritten = source.replace(
/from\s+["'](\.?\.?\/[^"']+)\.ts["']/g,
(_, importPath) => `from "${importPath}.js"`
);
return {
contents: rewritten,
loader: path.extname(args.path).slice(1),
};
});
},
};
console.log('Building with esbuild...');
let success = false;
try {
await esbuild.build({
entryPoints: allTs,
outdir: path.join(packageDir, "dist"),
format: "esm",
bundle: false,
sourcemap: false,
splitting: false,
platform: "node",
target: "esnext",
logLevel: "info",
loader: { ".ts": "ts" },
plugins: [rewriteTsImports],
});
success = true;
}
catch (e) {
console.error(`esbuild error: ${e}`);
}
if (!success) process.exit(0);
console.info('Done!');