Skip to content

Commit 0fd75ea

Browse files
committed
fix
1 parent 5274610 commit 0fd75ea

4 files changed

Lines changed: 54 additions & 29 deletions

File tree

cspell.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@
318318
"thelarkinn",
319319
"behaviour",
320320
"WHATWG",
321-
"publicpath"
321+
"publicpath",
322+
"Uninstantiated"
322323
],
323324
"ignoreRegExpList": [
324325
"/Author.+/",

test/runner/RunnerHelpers.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ const getNodeVersion = () => process.versions.node.split(".").map(Number);
6060

6161
const ESModuleStatus = Object.freeze({
6262
Unlinked: "unlinked",
63-
Evaluated: "evaluated"
63+
Linked: "linked",
64+
Evaluated: "evaluated",
65+
/**
66+
* Present in `module.linkingStatus`
67+
* Compatible with Node.js v10
68+
* https://nodejs.org/docs/latest-v10.x/api/vm.html#vm_module_status
69+
*/
70+
Uninstantiated: "uninstantiated"
6471
});
6572

6673
module.exports = {

test/runner/asModule.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,17 @@ module.exports = async (something, context, options = {}) => {
3838
});
3939
if (options.esmReturnStatus === ESModuleStatus.Unlinked) return esm;
4040

41-
await esm.link(LINKER);
42-
// Node.js 10 needs instantiate
43-
if (major === 10 && esm.instantiate) esm.instantiate();
41+
if (major === 10) {
42+
if (esm.linkingStatus === ESModuleStatus.Unlinked) {
43+
await esm.link(LINKER);
44+
}
45+
if (esm.linkingStatus === ESModuleStatus.Linked) {
46+
esm.instantiate();
47+
}
48+
} else {
49+
await esm.link(LINKER);
50+
}
51+
4452
await esm.evaluate();
4553
return esm;
4654
};

test/runner/index.js

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -447,40 +447,49 @@ class TestRunner {
447447
const esm = getModuleInstance(modulePath, content);
448448
if (esmReturnStatus === ESModuleStatus.Unlinked) return esm;
449449

450-
const evaluate = async () => {
451-
// 1. Link all deps
452-
if (esm.status === ESModuleStatus.Unlinked) {
453-
await esm.link(
454-
async (specifier, referrer) =>
455-
// `linker` should return `vm.SourceTextModule`
456-
await asModule(
457-
await this.require(
458-
path.dirname(
459-
referrer.identifier || fileURLToPath(referrer.url)
460-
),
461-
specifier,
462-
{ esmReturnStatus: ESModuleStatus.Unlinked }
450+
const link = async () => {
451+
await esm.link(
452+
async (specifier, referencingModule) =>
453+
// `linker` should return `vm.SourceTextModule`
454+
await asModule(
455+
await this.require(
456+
path.dirname(
457+
referencingModule.identifier ||
458+
fileURLToPath(referencingModule.url)
463459
),
464-
referrer.context,
465-
{
466-
esmReturnStatus: ESModuleStatus.Unlinked
467-
}
468-
)
469-
);
460+
specifier,
461+
{ esmReturnStatus: ESModuleStatus.Unlinked }
462+
),
463+
referencingModule.context,
464+
{
465+
esmReturnStatus: ESModuleStatus.Unlinked
466+
}
467+
)
468+
);
469+
};
470+
471+
const run = async () => {
472+
// Link module dependencies
473+
if (major === 10) {
474+
if (esm.linkingStatus === ESModuleStatus.Unlinked) {
475+
await link();
476+
}
477+
if (esm.linkingStatus === ESModuleStatus.Linked) {
478+
esm.instantiate();
479+
}
480+
} else {
481+
await link();
470482
}
471483

472-
// 2. Evaluate the module
473-
// Node.js 10 needs instantiate
474-
if (major === 10 && esm.instantiate) esm.instantiate();
484+
// Evaluate the module
475485
await esm.evaluate();
476486
if (esmReturnStatus === ESModuleStatus.Evaluated) return esm;
477487

478-
// 3. Return module namespace
479488
const ns = esm.namespace;
480489
return ns.default && ns.default instanceof Promise ? ns.default : ns;
481490
};
482491

483-
return evaluate();
492+
return run();
484493
};
485494
}
486495

0 commit comments

Comments
 (0)