Repository navigation
Expand file tree
/
Copy pathesbuild.js
More file actions
124 lines (116 loc) · 3.92 KB
/
Copy pathesbuild.js
File metadata and controls
124 lines (116 loc) · 3.92 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
const esbuild = require("esbuild");
const fs = require("node:fs/promises");
const path = require("node:path");
const production = process.argv.includes("--production");
const watch = process.argv.includes("--watch");
/**
* @type {import('esbuild').Plugin}
*/
const esbuildProblemMatcherPlugin = {
name: "esbuild-problem-matcher",
setup(build) {
build.onStart(() => {
console.log("[watch] build started");
});
build.onEnd((result) => {
result.errors.forEach(({ text, location }) => {
console.error(`✘ [ERROR] ${text}`);
console.error(
` ${location.file}:${location.line}:${location.column}:`,
);
});
console.log("[watch] build finished");
});
},
};
async function copyTreeSitterAssets() {
const runtimeWasmPath = require.resolve(
"web-tree-sitter/web-tree-sitter.wasm",
);
const runtimeOutputDirectory = path.join(
"dist",
"node_modules",
"web-tree-sitter",
);
await fs.mkdir(runtimeOutputDirectory, { recursive: true });
await fs.copyFile(runtimeWasmPath, path.join(runtimeOutputDirectory, "web-tree-sitter.wasm"));
const packageJson = require("./package.json");
const grammarPackages = Object.keys(packageJson.dependencies).filter(
(name) => name.startsWith("tree-sitter-") && name !== "tree-sitter-wasm",
);
for (const packageName of grammarPackages) {
const packagePath = require.resolve(`${packageName}/package.json`);
const directory = path.dirname(packagePath);
const files = await fs.readdir(directory);
const wasmFiles = files.filter((file) => file.endsWith(".wasm"));
if (wasmFiles.length === 0) {
throw new Error(`Grammar package ${packageName} does not contain a WASM asset.`);
}
const output = path.join("dist", "node_modules", packageName);
await fs.mkdir(output, { recursive: true });
await fs.copyFile(packagePath, path.join(output, "package.json"));
await Promise.all(wasmFiles.map((file) => fs.copyFile(path.join(directory, file), path.join(output, file))));
}
const wasmPackage = "tree-sitter-wasm";
const wasmPackageEntry = require.resolve(wasmPackage);
const wasmPackageDirectory = path.dirname(wasmPackageEntry);
const wasmOutput = path.join("dist", "node_modules", wasmPackage);
await fs.mkdir(path.join(wasmOutput, "out"), { recursive: true });
await Promise.all([
fs.copyFile(
path.join(wasmPackageDirectory, "package.json"),
path.join(wasmOutput, "package.json"),
),
fs.copyFile(
wasmPackageEntry,
path.join(wasmOutput, path.basename(wasmPackageEntry)),
),
]);
await Promise.all(
["go/tree-sitter-go.wasm", "rust/tree-sitter-rust.wasm"].map(async (file) => {
const output = path.join(wasmOutput, "out", file);
await fs.mkdir(path.dirname(output), { recursive: true });
await fs.copyFile(path.join(wasmPackageDirectory, "out", file), output);
}),
);
}
async function main() {
await copyTreeSitterAssets();
const contexts = await Promise.all([
esbuild.context({
entryPoints: ["src/extension.ts"],
bundle: true,
format: "cjs",
minify: production,
sourcemap: !production,
sourcesContent: false,
platform: "node",
outfile: "dist/extension.js",
external: ["vscode"],
logLevel: "silent",
plugins: [esbuildProblemMatcherPlugin],
}),
esbuild.context({
entryPoints: ["src/webview/App.tsx"],
bundle: true,
format: "iife",
minify: production,
sourcemap: !production,
sourcesContent: false,
platform: "browser",
outfile: "dist/webview/main.js",
logLevel: "silent",
plugins: [esbuildProblemMatcherPlugin],
}),
]);
if (watch) {
await Promise.all(contexts.map((ctx) => ctx.watch()));
} else {
await Promise.all(contexts.map((ctx) => ctx.rebuild()));
await Promise.all(contexts.map((ctx) => ctx.dispose()));
}
}
main().catch((e) => {
console.error(e);
process.exit(1);
});