@@ -4,56 +4,7 @@ import { mkdir, readFile } from "node:fs/promises";
4
4
import { isFile } from "./isFile.js" ;
5
5
import { isFileOrDoesNotExist } from "./isFileOrDoesNotExist.js" ;
6
6
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" ;
57
8
58
9
export async function compileApplicationToWasm ( input , output , wasmEngine ) {
59
10
try {
0 commit comments