From c511a1acdf1f5457eb37f402b77955e00a90500b Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 22 Nov 2023 14:55:13 +0100 Subject: [PATCH] feat: polyfill `node:module` --- README.md | 2 +- src/runtime/node/module/index.ts | 52 ++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/runtime/node/module/index.ts diff --git a/README.md b/README.md index 27adb83c..d638e5b5 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ Using this preset, we can convert a code that is depending on Node.js to work an | [node:http](https://nodejs.org/api/http.html) | Polyfilled | [unenv/node/http](./src/runtime/node/http) | | [node:https](https://nodejs.org/api/https.html) | Mocked | - | | [node:inspector](https://nodejs.org/api/inspector.html) | Mocked | - | -| [node:module](https://nodejs.org/api/module.html) | Mocked | - | +| [node:module](https://nodejs.org/api/module.html) | Polyfilled | [unenv/node/module](./src/runtime/node/module) - | | [node:net](https://nodejs.org/api/net.html) | Polyfilled | [unenv/node/net](./src/runtime/node/net) | | [node:os](https://nodejs.org/api/os.html) | Mocked | - | | [node:path](https://nodejs.org/api/path.html) | Polyfilled | [unenv/node/path](./src/runtime/node/path) | diff --git a/src/runtime/node/module/index.ts b/src/runtime/node/module/index.ts new file mode 100644 index 00000000..4f862653 --- /dev/null +++ b/src/runtime/node/module/index.ts @@ -0,0 +1,52 @@ +// https://nodejs.org/api/module.html +import type nodeModule from "node:module"; +import { notImplemented, notImplementedClass } from "../../_internal/utils"; + +export const builtinModules: typeof nodeModule.builtinModules = []; + +export const createRequire = notImplemented( + "module.createRequire", +) as typeof nodeModule.createRequire; + +export const runMain = notImplemented( + "module.runMain", +) as typeof nodeModule.runMain; + +export const isBuiltin = notImplemented( + "module.isBuiltin", +) as typeof nodeModule.isBuiltin; + +export const register = notImplemented( + "module.register", +) as typeof nodeModule.register; + +export const syncBuiltinESMExports = notImplemented( + "module.syncBuiltinESMExports", +) as typeof nodeModule.syncBuiltinESMExports; + +export const findSourceMap = notImplemented( + "module.syncBuiltinESMExports", +) as typeof nodeModule.findSourceMap; + +export const wrap = notImplemented("module.wrap") as typeof nodeModule.wrap; + +export const Module = notImplementedClass( + "module.Module", +) as typeof nodeModule.Module; + +export const SourceMap = notImplementedClass( + "module.SourceMap", +) as typeof nodeModule.SourceMap; + +export default { + Module, + SourceMap, + builtinModules, + createRequire, + runMain, + wrap, + isBuiltin, + register, + syncBuiltinESMExports, + findSourceMap, +};