|
| 1 | +/* eslint strict: off, node/no-unsupported-features: ["error", { version: 4 }] */ |
| 2 | +"use strict" |
| 3 | + |
| 4 | +const fs = require("fs") |
| 5 | +const path = require("path") |
| 6 | +const util = require("util") |
| 7 | +const vm = require("vm") |
| 8 | +const zlib = require("zlib") |
| 9 | + |
| 10 | +const esmPath = path.resolve(__dirname, "esm.js.gz") |
| 11 | +const inspectKey = util.inspect.custom || "inspect" |
| 12 | + |
| 13 | +const descriptor = Object.create(null) |
| 14 | +descriptor.value = () => "@std/esm enabled" |
| 15 | + |
| 16 | +const mod = new module.constructor(module.id) |
| 17 | +mod.filename = __filename |
| 18 | +mod.parent = module.parent |
| 19 | + |
| 20 | +const scriptOptions = Object.create(null) |
| 21 | +scriptOptions.filename = __filename |
| 22 | + |
| 23 | +const content = |
| 24 | + "(function(require,module,__filename){" + |
| 25 | + zlib.gunzipSync(fs.readFileSync(esmPath)).toString() + |
| 26 | + "\n})" |
| 27 | + |
| 28 | +const compiled = vm.runInThisContext(content, scriptOptions) |
| 29 | + |
| 30 | +function makeLoaderFunction() { |
| 31 | + compiled(require, mod, __filename) |
| 32 | + return mod.exports |
| 33 | +} |
| 34 | + |
| 35 | +const loader = makeLoaderFunction() |
| 36 | + |
| 37 | +module.exports = (mod, options) => { |
| 38 | + const type = typeof options |
| 39 | + |
| 40 | + if (options === true || |
| 41 | + type === "function" || |
| 42 | + (type === "object" && options !== null)) { |
| 43 | + return makeLoaderFunction()(mod, options) |
| 44 | + } |
| 45 | + |
| 46 | + return loader(mod, options) |
| 47 | +} |
| 48 | + |
| 49 | +Object.freeze(Object.defineProperty(module.exports, inspectKey, descriptor)) |
0 commit comments