Comparing version 0.7.1 to 0.8.0
53
index.js
/* eslint strict: off, node/no-unsupported-features: ["error", { version: 4 }] */ | ||
"use strict" | ||
const Module = module.constructor | ||
const fs = require("fs") | ||
const path = require("path") | ||
const util = require("util") | ||
const vm = require("vm") | ||
const zlib = require("zlib") | ||
const gunzipSync = require("zlib").gunzipSync | ||
const join = require("path").join | ||
const readFileSync = require("fs").readFileSync | ||
const esmPath = path.join(__dirname, "esm.js.gz") | ||
const inspectKey = util.inspect.custom || "inspect" | ||
const filePath = join(__dirname, "esm.js.gz") | ||
const content = gunzipSync(readFileSync(filePath)).toString() | ||
const mod = new Module(filePath, module.parent) | ||
const descriptor = Object.create(null) | ||
descriptor.value = () => "@std/esm enabled" | ||
mod.filename = filePath | ||
mod._compile(content, filePath) | ||
module.exports = mod.exports | ||
const mod = new module.constructor(module.id) | ||
mod.filename = __filename | ||
mod.parent = module.parent | ||
const scriptOptions = Object.create(null) | ||
scriptOptions.filename = esmPath | ||
const content = | ||
"(function(require,module,__filename){" + | ||
zlib.gunzipSync(fs.readFileSync(esmPath)).toString() + | ||
"\n})" | ||
const compiled = vm.runInThisContext(content, scriptOptions) | ||
function makeLoaderFunction() { | ||
compiled(require, mod, __filename) | ||
return mod.exports | ||
} | ||
const loader = makeLoaderFunction() | ||
module.exports = (mod, options) => { | ||
const type = typeof options | ||
if (options === true || | ||
type === "function" || | ||
(type === "object" && options !== null)) { | ||
return makeLoaderFunction()(mod, options) | ||
} | ||
return loader(mod, options) | ||
} | ||
Object.freeze(Object.defineProperty(module.exports, inspectKey, descriptor)) |
{ | ||
"name": "@std/esm", | ||
"version": "0.7.1", | ||
"version": "0.8.0", | ||
"description": "Enable ES modules in Node today!", | ||
@@ -5,0 +5,0 @@ "keywords": "commonjs, ecmascript, export, import, modules, node, require", |
@@ -22,2 +22,14 @@ # @std/esm | ||
By default, `@std/esm` **only** processes files of packages that opt-in | ||
with a `@std/esm` options object or `@std/esm` as a dependency, dev | ||
dependency, or peer dependency in their package.json. However, you can | ||
enable processing **all** files with specific options by passing an options | ||
object as the second argument or passing `true` to use the options from | ||
your package.json. | ||
```js | ||
const loader1 = require("@std/esm")(module, { cjs: true, esm: "js" }) | ||
const loader2 = require("@std/esm")(module, true) | ||
``` | ||
Enable ESM in the Node CLI by loading `@std/esm` with the [`-r` option](https://nodejs.org/api/cli.html#cli_r_require_module): | ||
@@ -24,0 +36,0 @@ |
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
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
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
52008
36
132
3