Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions benchmark/esm/cjs-parse.js

This file was deleted.

41 changes: 41 additions & 0 deletions benchmark/esm/import-cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';
const common = require('../common.js');
const assert = require('assert');
const tmpdir = require('../../test/common/tmpdir');
const fixtures = require('../../test/common/fixtures.js');
const fs = require('fs');

// This fixture has 2000+ exports.
const fixtureFile = fixtures.path('snapshot', 'typescript.js');
// This is fixed - typescript.js is slow to load, so 10 is enough.
// And we want to measure either 10 warm loads, or 1 cold load (but compare.js would run it many times)
const runs = 10;
const bench = common.createBenchmark(main, {
type: ['cold', 'warm'],
}, {
setup() {
tmpdir.refresh();
fs.cpSync(fixtureFile, tmpdir.resolve(`imported-cjs-initial.js`));
for (let i = 0; i < runs; i++) {
fs.cpSync(fixtureFile, tmpdir.resolve(`imported-cjs-${i}.js`));
}
},
});

async function main({ type }) {
if (type === 'cold') {
bench.start();
await import(tmpdir.fileURL(`imported-cjs-initial.js`));
bench.end(1);
} else {
await import(tmpdir.fileURL(`imported-cjs-initial.js`)); // Initialize the wasm first.
bench.start();
let result;
for (let i = 0; i < runs; i++) {
result = await import(tmpdir.fileURL(`imported-cjs-${i}.js`));
}
bench.end(runs);
const mod = require(fixtures.path('snapshot', 'typescript.js'));
assert.deepStrictEqual(Object.keys(mod), Object.keys(result.default));
}
}
11 changes: 8 additions & 3 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ function getSource(url) {
let cjsParse;
/**
* Initializes the CommonJS module lexer parser using the JavaScript version.
* TODO(joyeecheung): Use `require('internal/deps/cjs-module-lexer/dist/lexer').initSync()`
* when cjs-module-lexer 1.4.0 is rolled in.
*/
function initCJSParseSync() {
if (cjsParse === undefined) {
cjsParse = require('internal/deps/cjs-module-lexer/lexer').parse;
if (typeof WebAssembly !== 'undefined') {
const { parse, initSync } = require('internal/deps/cjs-module-lexer/dist/lexer');
initSync();
cjsParse = parse;
} else {
const { parse } = require('internal/deps/cjs-module-lexer/lexer');
cjsParse = parse;
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/es-module/test-import-cjs-jitless.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Flags: --jitless

// Tests that importing a CJS module works in JIT-less mode (i.e. falling back to the
// JS parser if WASM is not available).
import '../common/index.mjs';
import '../fixtures/empty.cjs';
Loading