forked from milahu/vite-plugin-tree-sitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
271 lines (238 loc) · 8.54 KB
/
index.js
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
vite-plugin-treesitter
based on
https://github.com/nshen/vite-plugin-treesitter/blob/main/src/index.ts
https://github.com/tree-sitter/tree-sitter/blob/master/cli/src/wasm.rs
https://github.com/tree-sitter/tree-sitter/blob/master/script/build-wasm
*/
import fs from 'fs';
import path from 'path';
import child_process from 'child_process';
/*
vite.config.js
import treeSitterPlugin from 'vite-plugin-tree-sitter';
plugins: [
treeSitterPlugin([
'tree-sitter-javascript', // npm package
'./path/to/tree-sitter-html', // local package
]),
],
*/
export default function (packages, options) {
// parse arguments
if (!packages) packages = [];
if (!options) options = {};
// TODO refactor ...
const localPathList = packages.filter(path => path.startsWith('./'));
const npmPathList = packages.filter(path => !path.startsWith('./'));
const prefix = `@vite-plugin-tree-sitter@`;
const wasmPackOutputPath = 'pkg'; // TODO
// from ../../my-crate -> my_crate_bg.wasm
const wasmNameOfPath = (localPath) => {
return path.basename(localPath).replace(/\-/g, '_') + '_bg.wasm'; // TODO why _bg ?
};
// filename -> { path, isNodeModule }
// TODO filename collisions?
const wasmMap = new Map();
// TODO better?
// at least make sure that path exists
wasmMap.set(
'tree-sitter.wasm',
{
path: 'node_modules/web-tree-sitter/tree-sitter.wasm',
isNodeModule: true
}
);
// 'my_crate_bg.wasm': {path:'../../my_crate/pkg/my_crate_bg.wasm', isNodeModule: false}
localPathList.forEach((localPath) => {
const wasmName = wasmNameOfPath(localPath);
const wasm = {
path: path.join(localPath, wasmPackOutputPath, wasmName),
isNodeModule: false
};
wasmMap.set(wasmName, wasm);
});
// 'my_crate_bg.wasm': { path: 'node_modules/my_crate/my_crate_bg.wasm', isNodeModule: true }
npmPathList.forEach((npmPath) => {
const wasmName = wasmNameOfPath(npmPath);
const wasm = {
path: path.join('node_modules', npmPath, wasmName),
isNodeModule: true
};
wasmMap.set(wasmName, wasm);
});
let config_base;
let config_assetsDir;
return { // plugin object
name: 'vite-plugin-tree-sitter',
enforce: 'pre',
configResolved(resolvedConfig) {
config_base = resolvedConfig.base;
config_assetsDir = resolvedConfig.build.assetsDir;
},
resolveId(id) {
//console.log(`vite-plugin-tree-sitter: resolveId? ${id}`)
if (id.includes('.wasm')) {
console.log(`vite-plugin-tree-sitter: resolveId? ${id}`);
}
for (let i = 0; i < localPathList.length; i++) {
if (path.basename(localPathList[i]) === id) {
console.log(`vite-plugin-tree-sitter: resolveId! ${id}`)
return prefix + id;
}
}
return null;
},
async load(id) {
//console.log(`vite-plugin-tree-sitter: load? ${id}`)
if (id.includes('.wasm')) {
console.log(`vite-plugin-tree-sitter: load? ${id}`)
}
if (id.startsWith(prefix)) {
console.log(`vite-plugin-tree-sitter: load! ${id}`)
id = id.slice(prefix.length);
const modulejs = path.join(
'./node_modules',
id,
id.replace(/\-/g, '_') + '.js'
);
console.log(`vite-plugin-tree-sitter: load: read code from ${modulejs}`)
const code = await fs.promises.readFile(modulejs, {
encoding: 'utf8'
});
return code;
}
},
async buildStart(_inputOptions) {
async function prepareBuild(pkgPath, isNodeModule) {
const pkgPathFull = isNodeModule
? path.join('node_modules', pkgPath)
: path.join(pkgPath, pkg);
const pkgName = path.basename(pkgPath);
if (!fs.existsSync(pkgPathFull)) {
if (isNodeModule) {
console.error(`vite-plugin-tree-sitter: cannot find npm module ${pkgPathFull}`);
} else {
console.error(`vite-plugin-tree-sitter: cannot find local module ${pkgPathFull}`);
}
}
if (!isNodeModule) {
// copy pkg generated by treesitter to node_modules
try {
await fs.copy(pkgPath, path.join('node_modules', pkgName));
} catch (error) {
this.error(`copy crates failed`);
}
}
// compile if necessary
const grammar_name = (pkgName.match(/^tree-sitter-(.+)$/) || [])[1];
if (!grammar_name) {
console.error(`vite-plugin-tree-sitter: cannot parse tree-sitter grammar_name from pkgName ${pkgName}`);
}
//const outDir = 'node_modules/.vite'; // this folder is removed by vite
const outDir = 'dist/assets';
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir, { recursive: true });
}
const outBasePath = `${outDir}/tree-sitter-${grammar_name}`;
//const outJsPath = `${outBasePath}.js`;
const outWasmPath = `${outBasePath}.wasm`;
// based on https://github.com/tree-sitter/tree-sitter/blob/master/cli/src/wasm.rs
const compileArgs = [
'emcc',
'-v', // verbose
'-Os',
'-fno-exceptions',
'-s', 'WASM=1',
'-s', 'SIDE_MODULE=1', // produce only *.wasm file -> is only a "side module" for other *.wasm file
'-s', 'TOTAL_MEMORY=33554432',
'-s', 'NODEJS_CATCH_EXIT=0',
'-s', `EXPORTED_FUNCTIONS=["_tree_sitter_${grammar_name}"]`,
/* debug
'-s', 'ASSERTIONS=1',
'-s', 'SAFE_HEAP=1',
*/
//'-o', outJsPath, // passing *.js will produce *.js and *.wasm files
'-o', outWasmPath, // passing *.js will produce *.js and *.wasm files
'-I', `${pkgPathFull}/src`,
`${pkgPathFull}/src/parser.c`,
`${pkgPathFull}/src/scanner.c`, // TODO glob: *.c | *.cc | *.cpp
// TODO add -xc++ for scanner.cc / scanner.cpp
];
console.log(`vite-plugin-tree-sitter: compile ${pkgPathFull} -> ${outWasmPath}`)
const emccEnv = { ...process.env };
delete emccEnv.NODE; // fix warning: honoring legacy environment variable `NODE`
const emccProcess = child_process.spawnSync(compileArgs[0], compileArgs.slice(1), {
stdio: [null, 'pipe', 'pipe'],
//stdio: 'inherit',
env: emccEnv,
encoding: 'utf8'
});
function printEmccOutput() {
console.log('emcc output:');
console.log(emccProcess.stdout);
console.log('emcc error:');
console.log(emccProcess.stderr);
}
if (emccProcess.status != 0) {
console.error(`vite-plugin-tree-sitter: buildStart: compile error: code ${emccProcess.status}`)
if (emccProcess.status == null) {
console.error(`vite-plugin-tree-sitter: buildStart: compile error: emcc not found?`)
}
printEmccOutput();
}
if (!fs.existsSync(outWasmPath)) {
console.error(`vite-plugin-tree-sitter: buildStart: compile error: output file is missing`)
printEmccOutput();
}
/*
else {
console.error(`vite-plugin-tree-sitter: buildStart: compile ok: ${outWasmPath}`)
}
*/
wasmMap.set(path.basename(outWasmPath), { path: outWasmPath, isNodeModule });
};
for await (const localPath of localPathList) {
await prepareBuild(localPath, false);
}
for await (const localPath of npmPathList) {
await prepareBuild(localPath, true);
}
},
configureServer({ middlewares }) {
return () => {
// send 'root/pkg/xxx.wasm' file to user
middlewares.use((req, res, next) => {
if (req.url) {
const urlName = path.basename(req.url);
res.setHeader(
'Cache-Control',
'no-cache, no-store, must-revalidate'
);
const wasm = wasmMap.get(urlName);
if (wasm) {
console.log(`vite-plugin-tree-sitter: serve ${req.url} -> ${wasm.path}`)
res.writeHead(200, { 'Content-Type': 'application/wasm' });
fs.createReadStream(wasm.path).pipe(res);
} else {
next();
}
}
});
};
},
// TODO ...
/* this kills the vite devserver when its trying to restart (after config reload)
buildEnd() {
// copy xxx.wasm files to /assets/xxx.wasm
wasmMap.forEach((crate, fileName) => {
this.emitFile({
type: 'asset',
fileName: `assets/${fileName}`,
source: fs.readFileSync(crate.path)
});
});
}
*/
};
}