Comparing version 0.18.0 to 0.19.0
57
index.js
@@ -10,46 +10,43 @@ /* eslint strict: off, node/no-unsupported-features: ["error", { version: 4 }] */ | ||
// Guard against poorly mocked module constructors. | ||
const Module = module.constructor.length > 1 | ||
? module.constructor | ||
: require("module") | ||
// Guard against mocked environments (e.g. Jest). | ||
const useBuiltins = module.constructor.length > 1 | ||
const esmPath = path.resolve(__dirname, "esm.js.gz") | ||
const inspectKey = util.inspect.custom || "inspect" | ||
const Module = useBuiltins ? module.constructor : require("module") | ||
const descriptor = Object.create(null) | ||
descriptor.value = () => "@std/esm enabled" | ||
const stdMod = new Module(module.id, null) | ||
const stdPath = path.resolve(__dirname, "esm.js.gz") | ||
const stdReq = useBuiltins ? require : stdMod.require | ||
const mod = new Module(module.id, null) | ||
mod.filename = __filename | ||
mod.parent = module.parent | ||
stdMod.filename = __filename | ||
stdMod.parent = module.parent | ||
const content = | ||
"(function(require,module,__shared__){" + | ||
zlib.gunzipSync(fs.readFileSync(stdPath)) + | ||
"\n})" | ||
const scriptOptions = Object.create(null) | ||
scriptOptions.filename = __filename | ||
const content = | ||
"(function(require,module,__filename,__options){" + | ||
zlib.gunzipSync(fs.readFileSync(esmPath)) + | ||
"\n})" | ||
const compiled = vm.runInThisContext(content, scriptOptions) | ||
function makeLoaderFunction(options) { | ||
compiled(require, mod, __filename, options) | ||
return mod.exports | ||
function load() { | ||
compiled(stdReq, stdMod, shared) | ||
return stdMod.exports | ||
} | ||
const loader = makeLoaderFunction() | ||
function makeRequireFunction(mod, options) { | ||
return load()(mod, options) | ||
} | ||
module.exports = (mod, options) => { | ||
const type = typeof options | ||
let shared | ||
shared = load() | ||
if (options === true || | ||
type === "function" || | ||
(type === "object" && options !== null)) { | ||
return makeLoaderFunction(options)(mod, options) | ||
} | ||
const descriptor = Object.create(null) | ||
descriptor.value = () => "@std/esm enabled" | ||
return loader(mod) | ||
} | ||
const inspectKey = util.inspect.custom || "inspect" | ||
Object.defineProperty(makeRequireFunction, inspectKey, descriptor) | ||
Object.freeze(Object.defineProperty(module.exports, inspectKey, descriptor)) | ||
Object.freeze(makeRequireFunction) | ||
module.exports = makeRequireFunction |
{ | ||
"name": "@std/esm", | ||
"version": "0.18.0", | ||
"version": "0.19.0", | ||
"description": "Enable ES modules in Node today!", | ||
@@ -5,0 +5,0 @@ "keywords": "commonjs, ecmascript, export, import, modules, node, require", |
@@ -18,13 +18,7 @@ # @std/esm | ||
```js | ||
require = require("@std/esm")(module) | ||
// Provide options as a parameter or options file. | ||
require = require("@std/esm")(module, options) | ||
module.exports = require("./main.mjs").default | ||
``` | ||
Enable for **all** modules, including dependencies, by providing | ||
`options` or `true` to use your options file. | ||
```js | ||
require = require("@std/esm")(module, options) | ||
``` | ||
Enable ESM in the Node CLI with the [`-r` option](https://nodejs.org/api/cli.html#cli_r_require_module): | ||
@@ -107,2 +101,3 @@ | ||
<li><code>"js"</code> and other files with <code>import</code>, <code>export</code>, or <a href="https://github.com/tc39/proposal-modules-pragma"><code>"use module"</code></a> as ESM</li> | ||
<li><code>"cjs"</code> shorthand for <code>"@std/esm":{"cjs":true,"esm":"js"}</code></li> | ||
</ul> | ||
@@ -120,3 +115,3 @@ </td> | ||
<td> | ||
<p>A boolean to support top-level <code>await</code> in the main ES module.</p> | ||
<p>A boolean to support top-level <code>await</code> in the main module <i>(requires Node 7.6+)</i>.</p> | ||
</td> | ||
@@ -248,3 +243,2 @@ </tr> | ||
* Load `@std/esm` with the *“require”* option of | ||
[`ava`](https://github.com/avajs/ava#options), | ||
[`mocha`](https://mochajs.org/#-r---require-module-name), | ||
@@ -251,0 +245,0 @@ [`nyc`](https://github.com/istanbuljs/nyc#require-additional-modules), and |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65035
37
251
5
1