Skip to content

Commit cb2f6ff

Browse files
guybedfordMylesBorins
authored andcommitted
module: use Wasm CJS lexer when available
PR-URL: #35583 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]>
1 parent 4d1d3f4 commit cb2f6ff

File tree

7 files changed

+70
-2
lines changed

7 files changed

+70
-2
lines changed

‎benchmark/esm/cjs-parse.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
constfs=require('fs');
3+
constpath=require('path');
4+
constcommon=require('../common.js');
5+
const{ strictEqual }=require('assert');
6+
7+
consttmpdir=require('../../test/common/tmpdir');
8+
constbenchmarkDirectory=
9+
path.resolve(tmpdir.path,'benchmark-esm-parse');
10+
11+
constbench=common.createBenchmark(main,{
12+
n: [1e2]
13+
});
14+
15+
asyncfunctionmain({ n }){
16+
tmpdir.refresh();
17+
18+
fs.mkdirSync(benchmarkDirectory);
19+
20+
letsampleSource='try{\n';
21+
for(leti=0;i<1000;i++){
22+
sampleSource+='sample.js(() => file = /test/);\n';
23+
}
24+
sampleSource+='} catch{}\nexports.p = 5;\n';
25+
26+
for(leti=0;i<n;i++){
27+
constsampleFile=path.join(benchmarkDirectory,`sample${i}.js`);
28+
fs.writeFileSync(sampleFile,sampleSource);
29+
}
30+
31+
bench.start();
32+
for(leti=0;i<n;i++){
33+
constsampleFile=path.join(benchmarkDirectory,`sample${i}.js`);
34+
constm=awaitimport('file:'+sampleFile);
35+
strictEqual(m.p,5);
36+
}
37+
bench.end(n);
38+
39+
tmpdir.refresh();
40+
}

‎deps/cjs-module-lexer/dist/lexer.js‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎deps/cjs-module-lexer/dist/lexer.mjs‎

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

‎lib/internal/modules/esm/translators.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,26 @@ const{getOptionValue } = require('internal/options');
5656
constexperimentalImportMetaResolve=
5757
getOptionValue('--experimental-import-meta-resolve');
5858
constasyncESM=require('internal/process/esm_loader');
59-
constcjsParse=require('internal/deps/cjs-module-lexer/lexer');
6059
const{ emitWarningSync }=require('internal/process/warning');
6160

61+
letcjsParse;
62+
asyncfunctioninitCJSParse(){
63+
if(typeofWebAssembly!=='undefined'){
64+
const{ parse, init }=
65+
require('internal/deps/cjs-module-lexer/dist/lexer');
66+
awaitinit();
67+
letexports;
68+
try{
69+
({ exports }=parse('exports.a=1'));
70+
if(exports.length===1){
71+
cjsParse=parse;
72+
return;
73+
}
74+
}catch{}
75+
}
76+
cjsParse=require('internal/deps/cjs-module-lexer/lexer');
77+
}
78+
6279
consttranslators=newSafeMap();
6380
exports.translators=translators;
6481
exports.enrichCJSError=enrichCJSError;
@@ -164,6 +181,7 @@ translators.set('commonjs', async function commonjsStrategy(url, isMain){
164181
if(isWindows)
165182
filename=StringPrototypeReplace(filename,winSepRegEx,'\\');
166183

184+
if(!cjsParse)awaitinitCJSParse();
167185
const{ module, exportNames }=cjsPreparseModuleExports(filename);
168186
constnamesWithDefault=exportNames.has('default') ?
169187
[...exportNames] : ['default', ...exportNames];

‎node.gyp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@
253253
'deps/acorn-plugins/acorn-private-methods/index.js',
254254
'deps/acorn-plugins/acorn-static-class-features/index.js',
255255
'deps/cjs-module-lexer/lexer.js',
256+
'deps/cjs-module-lexer/dist/lexer.js',
256257
],
257258
'node_mksnapshot_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)node_mksnapshot<(EXECUTABLE_SUFFIX)',
258259
'mkcodecache_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)mkcodecache<(EXECUTABLE_SUFFIX)',
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
construnBenchmark=require('../common/benchmark');
6+
7+
runBenchmark('esm',{NODEJS_BENCHMARK_ZERO_ALLOWED: 1});

‎test/parallel/test-bootstrap-modules.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ const expectedModules = new Set([
5353
'NativeModule internal/modules/cjs/helpers',
5454
'NativeModule internal/modules/cjs/loader',
5555
'NativeModule internal/modules/esm/create_dynamic_module',
56-
'NativeModule internal/deps/cjs-module-lexer/lexer',
5756
'NativeModule internal/modules/esm/get_format',
5857
'NativeModule internal/modules/esm/get_source',
5958
'NativeModule internal/modules/esm/loader',

0 commit comments

Comments
(0)