node-retrieve-globals
Advanced tools
Comparing version 5.0.0 to 6.0.0
{ | ||
"name": "node-retrieve-globals", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "Execute a string of JavaScript using Node.js and return the global variable values and functions.", | ||
@@ -22,10 +22,10 @@ "type": "module", | ||
"@zachleat/noop": "^1.0.3", | ||
"ava": "^6.0.1", | ||
"ava": "^6.1.2", | ||
"cross-env": "^7.0.3" | ||
}, | ||
"dependencies": { | ||
"acorn": "^8.8.2", | ||
"acorn-walk": "^8.3.1", | ||
"acorn": "^8.1.3", | ||
"acorn-walk": "^8.3.2", | ||
"esm-import-transformer": "^3.0.2" | ||
} | ||
} |
@@ -34,3 +34,3 @@ # node-retrieve-globals | ||
import { RetrieveGlobals } from "node-retrieve-globals"; | ||
// const { RetrieveGlobals } = require("node-retrieve-globals"); | ||
// const { RetrieveGlobals } = await import("node-retrieve-globals"); | ||
``` | ||
@@ -85,2 +85,9 @@ | ||
await vm.getGlobalContext({}, options); | ||
``` | ||
## Changelog | ||
* `v6.0.0` Changes `import` and `require` to be project relative (not relative to this package on the file system). | ||
* `v5.0.0` Removes sync API, swap to async-only. Better compatibility with `--experimental-vm-modules` Node flag. | ||
* `v4.0.0` Swap to use `Module._compile` as a workaround for #2 (Node regression with experimental modules API in Node v20.10+) | ||
* `v3.0.0` ESM-only package. Node 16+ |
@@ -7,8 +7,12 @@ import vm from "vm"; | ||
import { isSupported } from "./vmModules.js"; | ||
import { getWorkingDirectory } from "./util/getWorkingDirectory.js"; | ||
import { isSupported } from "./util/vmModules.js"; | ||
const IS_VM_MODULES_SUPPORTED = isSupported(); | ||
// `import` and `require` should both be relative to working directory (not this file) | ||
const WORKING_DIRECTORY = getWorkingDirectory(); | ||
// TODO (feature) option to change `require` home base | ||
const customRequire = createRequire(import.meta.url); | ||
const customRequire = createRequire(WORKING_DIRECTORY); | ||
@@ -298,3 +302,3 @@ class RetrieveGlobals { | ||
let m = new Module(); | ||
m._compile(execCode, import.meta.url); | ||
m._compile(execCode, WORKING_DIRECTORY); | ||
return m.exports; | ||
@@ -306,5 +310,3 @@ } | ||
// Warning: this option is part of the experimental modules API | ||
execOptions.importModuleDynamically = function(specifier) { | ||
return import(specifier); | ||
}; | ||
execOptions.importModuleDynamically = (specifier) => import(specifier); | ||
} | ||
@@ -317,3 +319,3 @@ | ||
initializeImportMeta: (meta, module) => { | ||
meta.url = this.options.filePath || module.identifier; | ||
meta.url = this.options.filePath || WORKING_DIRECTORY || module.identifier; | ||
}, | ||
@@ -320,0 +322,0 @@ ...execOptions, |
import test from "ava"; | ||
import { RetrieveGlobals } from "../retrieveGlobals.js"; | ||
import { isSupported } from "../vmModules.js"; | ||
import { isSupported } from "../util/vmModules.js"; | ||
import { getWorkingDirectory } from "../util/getWorkingDirectory.js"; | ||
@@ -260,2 +261,11 @@ const IS_VM_MODULES_SUPPORTED = isSupported(); | ||
}); | ||
test("import.meta.url has the current working directory (if not passed via filePath)", async t => { | ||
let vm = new RetrieveGlobals(`const b = import.meta.url;`, { | ||
// filePath: import.meta.url | ||
}); | ||
let ret = await vm.getGlobalContext(); | ||
t.is(ret.b, getWorkingDirectory()); | ||
}); | ||
} |
25215
8
578
91
23202
Updatedacorn@^8.1.3
Updatedacorn-walk@^8.3.2