modern-node-polyfills
Advanced tools
Comparing version 0.0.9 to 0.1.0
"use strict"; | ||
import "./chunk-EZUCZHGV.js"; | ||
@@ -3,0 +4,0 @@ // global.ts |
@@ -9,4 +9,4 @@ import { RollupInjectOptions as RollupInjectOptions$1 } from '@rollup/plugin-inject'; | ||
declare function polyfillPath(module: string): Promise<string>; | ||
declare function polyfillContent(module: string): Promise<string>; | ||
declare function polyfillPath(mod: string): Promise<string>; | ||
declare function polyfillContent(mod: string): Promise<string>; | ||
declare type Globals = { | ||
@@ -17,4 +17,12 @@ __filename?: string; | ||
declare function inject(content: string, options: RollupInjectOptions, id?: string): Promise<string>; | ||
declare function polyfillGlobals(content: string, globals?: Globals): Promise<string>; | ||
declare type InjectmentWithNull = string | [string, string] | null; | ||
declare type Modules = Partial<{ | ||
process: InjectmentWithNull; | ||
Buffer: InjectmentWithNull; | ||
global: InjectmentWithNull; | ||
setImmediate: InjectmentWithNull; | ||
clearImmediate: InjectmentWithNull; | ||
}>; | ||
declare function polyfillGlobals(content: string, globals?: Globals, mods?: Modules): Promise<string>; | ||
export { inject, polyfillContent, polyfillGlobals, polyfillPath }; |
"use strict"; | ||
import { | ||
__require | ||
} from "./chunk-EZUCZHGV.js"; | ||
// index.ts | ||
import { resolve as resolve2, join } from "path"; | ||
import { builtinModules } from "module"; | ||
import { builtinModules, createRequire } from "module"; | ||
@@ -37,3 +40,3 @@ // node_modules/.pnpm/resolve.exports@1.1.0/node_modules/resolve.exports/dist/index.mjs | ||
if (exports) { | ||
let { browser, require: require3, unsafe, conditions = [] } = options; | ||
let { browser, require: require2, unsafe, conditions = [] } = options; | ||
let target = toName(name, entry); | ||
@@ -46,3 +49,3 @@ if (target[0] !== ".") | ||
let allows = /* @__PURE__ */ new Set(["default", ...conditions]); | ||
unsafe || allows.add(require3 ? "require" : "import"); | ||
unsafe || allows.add(require2 ? "require" : "import"); | ||
unsafe || allows.add(browser ? "browser" : "node"); | ||
@@ -77,3 +80,4 @@ let key, tmp, isSingle = false; | ||
import { loadPackageJSON, resolveModule } from "local-pkg"; | ||
import { createRequire } from "module"; | ||
import { parse as _parse } from "acorn"; | ||
import { build } from "esbuild"; | ||
@@ -1507,14 +1511,15 @@ // plugin.ts | ||
// index.ts | ||
import { parse as _parse } from "acorn"; | ||
import { build } from "esbuild"; | ||
var require2 = createRequire(import.meta.url); | ||
async function polyfillPath(module) { | ||
if (module.startsWith("node:")) { | ||
module = module.replace("node:", ""); | ||
if (!globalThis.require) { | ||
const require2 = createRequire(import.meta.url); | ||
globalThis.require = require2; | ||
} | ||
async function polyfillPath(mod) { | ||
if (mod.startsWith("node:")) { | ||
mod = mod.replace("node:", ""); | ||
} | ||
if (!builtinModules.includes(module)) | ||
throw new Error(`Node.js does not have ${module} in its builtin modules`); | ||
const jspmPath = resolve2(require2.resolve(`@jspm/core/nodelibs/${module}`), "../../.."); | ||
if (!builtinModules.includes(mod)) | ||
throw new Error(`Node.js does not have ${mod} in its builtin modules`); | ||
const jspmPath = resolve2(__require.resolve(`@jspm/core/nodelibs/${mod}`), "../../.." + (mod.includes("/") ? "/.." : "")); | ||
const jspmPackageJson = await loadPackageJSON(jspmPath); | ||
const exportPath = resolve(jspmPackageJson, `./nodelibs/${module}`, { | ||
const exportPath = resolve(jspmPackageJson, `./nodelibs/${mod}`, { | ||
browser: true | ||
@@ -1528,4 +1533,4 @@ }); | ||
} | ||
async function polyfillContent(module) { | ||
const exportFullPath = await polyfillPath(module); | ||
async function polyfillContent(mod) { | ||
const exportFullPath = await polyfillPath(mod); | ||
const content = (await build({ | ||
@@ -1545,3 +1550,3 @@ write: false, | ||
} | ||
async function polyfillGlobals(content, globals = {}) { | ||
async function polyfillGlobals(content, globals = {}, mods = {}) { | ||
return inject2(content, { | ||
@@ -1552,11 +1557,15 @@ expressions: { | ||
}, | ||
modules: { | ||
modules: removeEmpty({ | ||
process: await polyfillPath("process"), | ||
Buffer: [await polyfillPath("buffer"), "Buffer"], | ||
global: "modern-node-polyfills/global", | ||
global: __require.resolve("modern-node-polyfills/global"), | ||
setImmediate: [await polyfillPath("timers"), "setImmediate"], | ||
clearImmediate: [await polyfillPath("timers"), "clearImmediate"] | ||
} | ||
clearImmediate: [await polyfillPath("timers"), "clearImmediate"], | ||
...mods | ||
}) | ||
}, globals.__filename); | ||
} | ||
function removeEmpty(obj) { | ||
return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v != null)); | ||
} | ||
export { | ||
@@ -1563,0 +1572,0 @@ inject2 as inject, |
{ | ||
"name": "modern-node-polyfills", | ||
"version": "0.0.9", | ||
"version": "0.1.0", | ||
"description": "modern polyfills for node native modules based on jspm-core", | ||
@@ -26,3 +26,4 @@ "type": "module", | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs" | ||
"require": "./dist/index.cjs", | ||
"default": "./dist/index.js" | ||
}, | ||
@@ -32,3 +33,4 @@ "./global": { | ||
"import": "./dist/global.js", | ||
"require": "./dist/global.cjs" | ||
"require": "./dist/global.cjs", | ||
"default": "./dist/global.js" | ||
} | ||
@@ -56,3 +58,7 @@ }, | ||
"vitest": "^0.16.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Aslemammad/modern-node-polyfills.git" | ||
} | ||
} |
@@ -57,3 +57,25 @@ # modern-node-polyfills | ||
``` | ||
As a third argument, it's possible to change the path of those global polyfills or | ||
even disable them using `null`. | ||
```ts | ||
const content = await polyfillGlobals( | ||
` | ||
console.log(global) | ||
console.log(process); | ||
console.log(Buffer); | ||
console.log(setImmediate); | ||
console.log(clearImmediate); | ||
`, | ||
{}, | ||
{ | ||
process: "/here/process.js", | ||
Buffer: null, | ||
global: null, | ||
setImmediate: null, | ||
clearImmediate: null, | ||
} | ||
); | ||
``` | ||
# Contribution | ||
@@ -60,0 +82,0 @@ Feel free to let me know what you need for this package or what issue you have, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
104848
10
3164
84
8