Skip to content

Commit 884e357

Browse files
Jake ChampionJakeChampion
authored andcommitted
refactor: move precompilation functionality into it's own file
1 parent 1c7a732 commit 884e357

File tree

2 files changed

+51
-50
lines changed

2 files changed

+51
-50
lines changed

src/compileApplicationToWasm.js

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,7 @@ import { mkdir, readFile } from "node:fs/promises";
44
import { isFile } from "./isFile.js";
55
import { isFileOrDoesNotExist } from "./isFileOrDoesNotExist.js";
66
import wizer from "@jakechampion/wizer";
7-
import Parser, { Query } from "tree-sitter";
8-
import JavaScript from "tree-sitter-javascript";
9-
10-
function findRegexLiterals(source) {
11-
const parser = new Parser();
12-
parser.setLanguage(JavaScript);
13-
14-
const tree = parser.parse(source);
15-
const query = new Query(
16-
JavaScript,
17-
"(regex pattern: (regex_pattern) @pattern flags: (regex_flags)? @flags)"
18-
);
19-
const regexLiterals = [];
20-
for (const m of query.matches(tree.rootNode)) {
21-
regexLiterals.push({
22-
pattern: m.captures[0].node.text,
23-
flags: m.captures[1]?.node.text || "",
24-
});
25-
}
26-
return regexLiterals;
27-
}
28-
29-
const PREAMBLE = `;{
30-
// Precompiled regular expressions
31-
const precompile = (r) => { r.exec('a'); r.exec('\\u1000'); };`;
32-
const POSTAMBLE = "}";
33-
34-
// TODO: This should also detect and update sourcemaps if they are present, otherwise the sourcemaps would be incorrect.
35-
//
36-
/// Emit a block of javascript that will pre-compile the regular expressions given. As spidermonkey
37-
/// will intern regular expressions, duplicating them at the top level and testing them with both
38-
/// an ascii and utf8 string should ensure that they won't be re-compiled when run in the fetch
39-
/// handler.
40-
function precompile(inputApplication) {
41-
let lits = findRegexLiterals(inputApplication);
42-
43-
if (lits.length === 0) {
44-
return inputApplication;
45-
}
46-
47-
return (
48-
PREAMBLE +
49-
lits
50-
.map((regex) => {
51-
return `precompile(/${regex.pattern}/${regex.flags});`;
52-
})
53-
.join("\n") +
54-
POSTAMBLE
55-
);
56-
}
7+
import { precompile } from "./precompile.js";
578

589
export async function compileApplicationToWasm(input, output, wasmEngine) {
5910
try {

src/precompile.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Parser, { Query } from "tree-sitter";
2+
import JavaScript from "tree-sitter-javascript";
3+
4+
function findRegexLiterals(source) {
5+
const parser = new Parser();
6+
parser.setLanguage(JavaScript);
7+
8+
const tree = parser.parse(source);
9+
const query = new Query(
10+
JavaScript,
11+
"(regex pattern: (regex_pattern) @pattern flags: (regex_flags)? @flags)"
12+
);
13+
const regexLiterals = [];
14+
for (const m of query.matches(tree.rootNode)) {
15+
regexLiterals.push({
16+
pattern: m.captures[0].node.text,
17+
flags: m.captures[1]?.node.text || "",
18+
});
19+
}
20+
return regexLiterals;
21+
}
22+
23+
const PREAMBLE = `;{
24+
// Precompiled regular expressions
25+
const precompile = (r) => { r.exec('a'); r.exec('\\u1000'); };`;
26+
const POSTAMBLE = "}";
27+
28+
// TODO: This should also detect and update sourcemaps if they are present, otherwise the sourcemaps would be incorrect.
29+
//
30+
/// Emit a block of javascript that will pre-compile the regular expressions given. As spidermonkey
31+
/// will intern regular expressions, duplicating them at the top level and testing them with both
32+
/// an ascii and utf8 string should ensure that they won't be re-compiled when run in the fetch
33+
/// handler.
34+
export function precompile(inputApplication) {
35+
let lits = findRegexLiterals(inputApplication);
36+
37+
if (lits.length === 0) {
38+
return inputApplication;
39+
}
40+
41+
return (
42+
PREAMBLE +
43+
lits
44+
.map((regex) => {
45+
return `precompile(/${regex.pattern}/${regex.flags});`;
46+
})
47+
.join("\n") +
48+
POSTAMBLE + inputApplication
49+
);
50+
}

0 commit comments

Comments
 (0)