Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: use Wasm CJS lexer when available #35583

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions benchmark/esm/cjs-parse.js
@@ -0,0 +1,40 @@
'use strict';
const fs = require('fs');
const path = require('path');
const common = require('../common.js');
const { strictEqual } = require('assert');

const tmpdir = require('../../test/common/tmpdir');
const benchmarkDirectory =
path.resolve(tmpdir.path, 'benchmark-esm-parse');

const bench = common.createBenchmark(main, {
n: [1e2]
});

async function main({ n }) {
tmpdir.refresh();

fs.mkdirSync(benchmarkDirectory);

let sampleSource = 'try {\n';
for (let i = 0; i < 1000; i++) {
sampleSource += 'sample.js(() => file = /test/);\n';
}
sampleSource += '} catch {}\nexports.p = 5;\n';

for (let i = 0; i < n; i++) {
const sampleFile = path.join(benchmarkDirectory, `sample${i}.js`);
fs.writeFileSync(sampleFile, sampleSource);
}

bench.start();
for (let i = 0; i < n; i++) {
const sampleFile = path.join(benchmarkDirectory, `sample${i}.js`);
const m = await import('file:' + sampleFile);
strictEqual(m.p, 5);
}
bench.end(n);

tmpdir.refresh();
}
1 change: 1 addition & 0 deletions deps/cjs-module-lexer/dist/lexer.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions deps/cjs-module-lexer/dist/lexer.mjs

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion lib/internal/modules/esm/translators.js
Expand Up @@ -56,9 +56,20 @@ const { getOptionValue } = require('internal/options');
const experimentalImportMetaResolve =
getOptionValue('--experimental-import-meta-resolve');
const asyncESM = require('internal/process/esm_loader');
const cjsParse = require('internal/deps/cjs-module-lexer/lexer');
const { emitWarningSync } = require('internal/process/warning');

let cjsParse;
async function initCJSParse() {
if (typeof WebAssembly === 'undefined') {
cjsParse = require('internal/deps/cjs-module-lexer/lexer');
} else {
const { parse, init } =
require('internal/deps/cjs-module-lexer/dist/lexer');
await init();
cjsParse = parse;
}
}

const translators = new SafeMap();
exports.translators = translators;
exports.enrichCJSError = enrichCJSError;
Expand Down Expand Up @@ -164,6 +175,7 @@ translators.set('commonjs', async function commonjsStrategy(url, isMain) {
if (isWindows)
filename = StringPrototypeReplace(filename, winSepRegEx, '\\');

if (!cjsParse) await initCJSParse();
const { module, exportNames } = cjsPreparseModuleExports(filename);
const namesWithDefault = exportNames.has('default') ?
[...exportNames] : ['default', ...exportNames];
Expand Down
1 change: 1 addition & 0 deletions node.gyp
Expand Up @@ -275,6 +275,7 @@
'deps/acorn-plugins/acorn-private-methods/index.js',
'deps/acorn-plugins/acorn-static-class-features/index.js',
'deps/cjs-module-lexer/lexer.js',
'deps/cjs-module-lexer/dist/lexer.js',
],
'node_mksnapshot_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)node_mksnapshot<(EXECUTABLE_SUFFIX)',
'mkcodecache_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)mkcodecache<(EXECUTABLE_SUFFIX)',
Expand Down
7 changes: 7 additions & 0 deletions test/benchmark/test-benchmark-esm.js
@@ -0,0 +1,7 @@
'use strict';

require('../common');

const runBenchmark = require('../common/benchmark');

runBenchmark('esm', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
1 change: 0 additions & 1 deletion test/parallel/test-bootstrap-modules.js
Expand Up @@ -55,7 +55,6 @@ const expectedModules = new Set([
'NativeModule internal/modules/cjs/helpers',
'NativeModule internal/modules/cjs/loader',
'NativeModule internal/modules/esm/create_dynamic_module',
'NativeModule internal/deps/cjs-module-lexer/lexer',
'NativeModule internal/modules/esm/get_format',
'NativeModule internal/modules/esm/get_source',
'NativeModule internal/modules/esm/loader',
Expand Down