esbuild-plugins-node-modules-polyfill
Advanced tools
Comparing version 1.2.1-next.dc2eb77.0 to 1.3.0-next.63ea1c2.0
@@ -14,3 +14,3 @@ import { Plugin } from 'esbuild'; | ||
}; | ||
modules?: string[]; | ||
modules?: string[] | Record<string, boolean | 'empty'>; | ||
name?: string; | ||
@@ -17,0 +17,0 @@ namespace?: string; |
@@ -118,7 +118,12 @@ 'use strict'; | ||
var nodeModulesPolyfillPlugin = /* @__PURE__ */ __name((options = {}) => { | ||
const { globals = {}, modules = module$1.builtinModules, namespace = NAME, name = NAME } = options; | ||
const { globals = {}, modules: modulesOption = module$1.builtinModules, namespace = NAME, name = NAME } = options; | ||
if (namespace.endsWith("commonjs")) { | ||
throw new Error(`namespace ${namespace} must not end with commonjs`); | ||
} | ||
if (namespace.endsWith("empty")) { | ||
throw new Error(`namespace ${namespace} must not end with empty`); | ||
} | ||
const modules = Array.isArray(modulesOption) ? Object.fromEntries(modulesOption.map((mod) => [mod, true])) : modulesOption; | ||
const commonjsNamespace = `${namespace}-commonjs`; | ||
const emptyNamespace = `${namespace}-empty`; | ||
return { | ||
@@ -139,13 +144,32 @@ name, | ||
} | ||
onLoad({ filter: /.*/, namespace: emptyNamespace }, () => { | ||
return { | ||
loader: "js", | ||
// Use an empty CommonJS module here instead of ESM to avoid | ||
// "No matching export" errors in esbuild for anything that | ||
// is imported from this file. | ||
contents: "module.exports = {}" | ||
}; | ||
}); | ||
onLoad({ filter: /.*/, namespace }, loader); | ||
onLoad({ filter: /.*/, namespace: commonjsNamespace }, loader); | ||
const filter = new RegExp( | ||
`^(?:node:)?(?:${modules.filter((mod) => module$1.builtinModules.includes(mod)).map(escapeRegex).join("|")})$` | ||
`^(?:node:)?(?:${Object.keys(modules).filter((moduleName) => module$1.builtinModules.includes(moduleName)).map(escapeRegex).join("|")})$` | ||
); | ||
const resolver = /* @__PURE__ */ __name(async (args) => { | ||
const ignoreRequire = args.namespace === commonjsNamespace; | ||
const polyfill = await getCachedPolyfillPath(args.path).catch(() => null); | ||
const moduleName = normalizeNodeBuiltinPath(args.path); | ||
if (!modules[moduleName]) { | ||
return; | ||
} | ||
if (modules[moduleName] === "empty") { | ||
return { | ||
namespace: emptyNamespace, | ||
path: args.path | ||
}; | ||
} | ||
const polyfill = await getCachedPolyfillPath(moduleName).catch(() => null); | ||
if (!polyfill) { | ||
return; | ||
} | ||
const ignoreRequire = args.namespace === commonjsNamespace; | ||
const isCommonjs = !ignoreRequire && args.kind === "require-call"; | ||
@@ -152,0 +176,0 @@ return { |
{ | ||
"name": "esbuild-plugins-node-modules-polyfill", | ||
"version": "1.2.1-next.dc2eb77.0", | ||
"version": "1.3.0-next.63ea1c2.0", | ||
"description": "Polyfills nodejs builtin modules and globals for the browser.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -53,2 +53,17 @@ <div align="center"> | ||
Optionally provide empty polyfills: | ||
```ts | ||
import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill'; | ||
import { build } from 'esbuild'; | ||
build({ | ||
plugins: [nodeModulesPolyfillPlugin({ | ||
modules: { | ||
fs: 'empty', | ||
crypto: true, | ||
} | ||
})], | ||
}); | ||
``` | ||
Optionally inject globals when detected: | ||
@@ -55,0 +70,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
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
33419
206
97