Skip to content

Commit

Permalink
Test with --experimental-vm-modules for now until #2
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 14, 2023
1 parent 97bc1c9 commit 7be7c48
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"main": "retrieveGlobals.js",
"scripts": {
"test": "npx ava"
"test": "NODE_OPTIONS='--experimental-vm-modules' npx ava"
},
"repository": {
"type": "git",
Expand Down
5 changes: 4 additions & 1 deletion retrieveGlobals.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class RetrieveGlobals {
// to `const ___ = await import(___)`
// to emulate *some* import syntax.
// Doesn’t currently work with aliases (mod as name) or namespaced imports (* as name).
if(this.options.transformEsmImports) {
if(this.options.transformEsmImports === "require") {
let it = new ImportTransformer(this.originalCode);
this.code = it.transformToRequire();
} else if(this.options.transformEsmImports) {
let it = new ImportTransformer(this.originalCode);
this.code = it.transformToDynamicImport();
} else {
Expand Down
42 changes: 30 additions & 12 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,6 @@ test("destructured assignment via Array", async t => {
t.is(ret.b, 2);
});


test("dynamic import", async t => {
let vm = new RetrieveGlobals(`const { noop } = await import("@zachleat/noop");`);
let ret = await vm.getGlobalContext(undefined, {
dynamicImport: true
});
t.is(typeof ret.noop, "function");
});

test("global: same console.log", async t => {
let vm = new RetrieveGlobals(`const b = console.log`);
let ret = await vm.getGlobalContext(undefined, {
Expand Down Expand Up @@ -147,14 +138,41 @@ const b = 1;`);
t.is(ret.b, 1);
});

test("ESM import", async t => {
test("dynamic import (no code transformation) (requires --experimental-vm-modules in Node v20.10)", async t => {
let vm = new RetrieveGlobals(`const { noop } = await import("@zachleat/noop");`);
let ret = await vm.getGlobalContext(undefined, {
dynamicImport: true
});
t.is(typeof ret.noop, "function");
});

test("ESM import (requires --experimental-vm-modules in Node v20.10)", async t => {
let vm = new RetrieveGlobals(`import { noop } from "@zachleat/noop";
const b = 1;`, {
transformEsmImports: true,
});
let ret = await vm.getGlobalContext(undefined, {
dynamicImport: true
dynamicImport: true,
});
t.is(typeof ret.noop, "function");
t.is(ret.b, 1);
});
});

test("ESM import (fallback to `require`)", async t => {
let vm = new RetrieveGlobals(`import { noop } from "@zachleat/noop";
const b = 1;`, {
transformEsmImports: "require",
});

let ret = await vm.getGlobalContext(undefined, {
addRequire: true,
});

t.is(typeof ret.noop, "function");
t.is(ret.b, 1);
});

// test("__filename", t => {
// let vm = new RetrieveGlobals("var b = __filename;");
// t.deepEqual(vm.getGlobalContextSync({}, { reuseGlobal: true }), { b: __filename });
// });

0 comments on commit 7be7c48

Please sign in to comment.