Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
module: use Wasm CJS lexer when available
PR-URL: #35583
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
  • Loading branch information
guybedford authored and MylesBorins committed Nov 16, 2020
1 parent a93ca2d commit 354f358
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 2 deletions.
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.

20 changes: 19 additions & 1 deletion lib/internal/modules/esm/translators.js
Expand Up @@ -56,7 +56,24 @@ 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');

let cjsParse;
async function initCJSParse() {
if (typeof WebAssembly !== 'undefined') {
const { parse, init } =
require('internal/deps/cjs-module-lexer/dist/lexer');
await init();
let exports;
try {
({ exports } = parse('exports.a=1'));
if (exports.length === 1) {
cjsParse = parse;
return;
}
} catch {}
}
cjsParse = require('internal/deps/cjs-module-lexer/lexer');
}

const translators = new SafeMap();
exports.translators = translators;
Expand Down Expand Up @@ -167,6 +184,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 @@ -250,6 +250,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 @@ -54,7 +54,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

0 comments on commit 354f358

Please sign in to comment.