🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

vite-plugin-node-polyfills

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-node-polyfills - npm Package Compare versions

Comparing version
0.22.0
to
0.23.0
+2
-2
dist/index.cjs

@@ -1,3 +0,3 @@

"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("node:module"),B=require("@rollup/plugin-inject"),x=require("node-stdlib-browser"),P=require("node-stdlib-browser/helpers/rollup/plugin"),S=require("node-stdlib-browser/helpers/esbuild/plugin");var g=typeof document<"u"?document.currentScript:null;const c=l=>l&&l.__esModule?l:{default:l},T=c(B),j=c(x),q=c(S),v=(l,e)=>d(l)===d(e),s=(l,e)=>l?l===!0?!0:l===e:!1,R=l=>l.startsWith("node:"),$=l=>{const e=l.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return new RegExp(`^${e}$`)},d=l=>l.replace(/^node:/,""),p={buffer:["import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'","globalThis.Buffer = globalThis.Buffer || __buffer_polyfill"],global:["import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'","globalThis.global = globalThis.global || __global_polyfill"],process:["import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'","globalThis.process = globalThis.process || __process_polyfill"]},O=(l={})=>{const e={include:[],exclude:[],overrides:{},protocolImports:!0,...l,globals:{Buffer:!0,global:!0,process:!0,...l.globals}},m=o=>e.include.length>0?!e.include.some(r=>v(o,r)):e.exclude.some(r=>v(o,r)),h=o=>{if(s(e.globals.Buffer,"dev")&&/^buffer$/.test(o))return"vite-plugin-node-polyfills/shims/buffer";if(s(e.globals.global,"dev")&&/^global$/.test(o))return"vite-plugin-node-polyfills/shims/global";if(s(e.globals.process,"dev")&&/^process$/.test(o))return"vite-plugin-node-polyfills/shims/process";if(o in e.overrides)return e.overrides[o]},a=Object.entries(j.default).reduce((o,[r,i])=>(!e.protocolImports&&R(r)||m(r)||(o[r]=h(d(r))||i),o),{}),u=y.createRequire(typeof document>"u"?require("url").pathToFileURL(__filename).href:g&&g.src||new URL("index.cjs",document.baseURI).href),f=[...s(e.globals.Buffer,"dev")?[u.resolve("vite-plugin-node-polyfills/shims/buffer")]:[],...s(e.globals.global,"dev")?[u.resolve("vite-plugin-node-polyfills/shims/global")]:[],...s(e.globals.process,"dev")?[u.resolve("vite-plugin-node-polyfills/shims/process")]:[]],b=[...s(e.globals.Buffer,"dev")?p.buffer:[],...s(e.globals.global,"dev")?p.global:[],...s(e.globals.process,"dev")?p.process:[],""].join(`
`);return{name:"vite-plugin-node-polyfills",config:(o,r)=>{const i=r.command==="serve";return{build:{rollupOptions:{onwarn:(t,n)=>{P.handleCircularDependancyWarning(t,()=>{if(o.build?.rollupOptions?.onwarn)return o.build.rollupOptions.onwarn(t,n);n(t)})},plugins:[{...T.default({...s(e.globals.Buffer,"build")?{Buffer:"vite-plugin-node-polyfills/shims/buffer"}:{},...s(e.globals.global,"build")?{global:"vite-plugin-node-polyfills/shims/global"}:{},...s(e.globals.process,"build")?{process:"vite-plugin-node-polyfills/shims/process"}:{}})}]}},esbuild:{banner:i?b:void 0},optimizeDeps:{exclude:[...f],esbuildOptions:{banner:i?{js:b}:void 0,define:{...i&&s(e.globals.Buffer,"dev")?{Buffer:"Buffer"}:{},...i&&s(e.globals.global,"dev")?{global:"global"}:{},...i&&s(e.globals.process,"dev")?{process:"process"}:{}},inject:[...f],plugins:[q.default(a),{name:"vite-plugin-node-polyfills-shims-resolver",setup(t){for(const n of f){const _=$(n);t.onResolve({filter:_},()=>({external:!1,path:n}))}}}]}},resolve:{alias:{...a}}}}}};exports.nodePolyfills=O;
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const B=require("node:module"),j=require("@rollup/plugin-inject"),T=require("node-stdlib-browser"),x=require("node-stdlib-browser/helpers/rollup/plugin"),P=require("node-stdlib-browser/helpers/esbuild/plugin");var v=typeof document<"u"?document.currentScript:null;const d=l=>l&&l.__esModule?l:{default:l},S=d(j),q=d(T),O=d(P),h=(l,e)=>c(l)===c(e),s=(l,e)=>l?l===!0?!0:l===e:!1,R=l=>l.startsWith("node:"),$=l=>{const e=l.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return new RegExp(`^${e}$`)},c=l=>l.replace(/^node:/,""),p={buffer:["import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'","globalThis.Buffer = globalThis.Buffer || __buffer_polyfill"],global:["import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'","globalThis.global = globalThis.global || __global_polyfill"],process:["import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'","globalThis.process = globalThis.process || __process_polyfill"]},w=(l={})=>{const e={include:[],exclude:[],overrides:{},protocolImports:!0,...l,globals:{Buffer:!0,global:!0,process:!0,...l.globals}},m=o=>e.include.length>0?!e.include.some(r=>h(o,r)):e.exclude.some(r=>h(o,r)),_=o=>{if(s(e.globals.Buffer,"dev")&&/^buffer$/.test(o))return"vite-plugin-node-polyfills/shims/buffer";if(s(e.globals.global,"dev")&&/^global$/.test(o))return"vite-plugin-node-polyfills/shims/global";if(s(e.globals.process,"dev")&&/^process$/.test(o))return"vite-plugin-node-polyfills/shims/process";if(o in e.overrides)return e.overrides[o]},a=Object.entries(q.default).reduce((o,[r,i])=>(!e.protocolImports&&R(r)||m(r)||(o[r]=_(c(r))||i),o),{}),u=B.createRequire(typeof document>"u"?require("url").pathToFileURL(__filename).href:v&&v.src||new URL("index.cjs",document.baseURI).href),f=[...s(e.globals.Buffer,"dev")?[u.resolve("vite-plugin-node-polyfills/shims/buffer")]:[],...s(e.globals.global,"dev")?[u.resolve("vite-plugin-node-polyfills/shims/global")]:[],...s(e.globals.process,"dev")?[u.resolve("vite-plugin-node-polyfills/shims/process")]:[]],b=[...s(e.globals.Buffer,"dev")?p.buffer:[],...s(e.globals.global,"dev")?p.global:[],...s(e.globals.process,"dev")?p.process:[],""].join(`
`);return{name:"vite-plugin-node-polyfills",config:(o,r)=>{const i=r.command==="serve",g={...s(e.globals.Buffer,"build")?{Buffer:"vite-plugin-node-polyfills/shims/buffer"}:{},...s(e.globals.global,"build")?{global:"vite-plugin-node-polyfills/shims/global"}:{},...s(e.globals.process,"build")?{process:"vite-plugin-node-polyfills/shims/process"}:{}};return{build:{rollupOptions:{onwarn:(t,n)=>{x.handleCircularDependancyWarning(t,()=>{if(o.build?.rollupOptions?.onwarn)return o.build.rollupOptions.onwarn(t,n);n(t)})},plugins:Object.keys(g).length>0?[S.default(g)]:[]}},esbuild:{banner:i?b:void 0},optimizeDeps:{exclude:[...f],esbuildOptions:{banner:i?{js:b}:void 0,define:{...i&&s(e.globals.Buffer,"dev")?{Buffer:"Buffer"}:{},...i&&s(e.globals.global,"dev")?{global:"global"}:{},...i&&s(e.globals.process,"dev")?{process:"process"}:{}},inject:[...f],plugins:[O.default(a),{name:"vite-plugin-node-polyfills-shims-resolver",setup(t){for(const n of f){const y=$(n);t.onResolve({filter:y},()=>({external:!1,path:n}))}}}]}},resolve:{alias:{...a}}}}}};exports.nodePolyfills=w;
//# sourceMappingURL=index.cjs.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.cjs","sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { BooleanOrBuildTarget, ModuleName, ModuleNameWithoutNodePrefix } from './index'\n\nexport const compareModuleNames = (moduleA: ModuleName, moduleB: ModuleName) => {\n return withoutNodeProtocol(moduleA) === withoutNodeProtocol(moduleB)\n}\n\nexport const isEnabled = (value: BooleanOrBuildTarget, mode: 'build' | 'dev') => {\n if (!value) return false\n if (value === true) return true\n\n return value === mode\n}\n\nexport const isNodeProtocolImport = (name: string) => {\n return name.startsWith('node:')\n}\n\nexport const toRegExp = (text: string) => {\n // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping\n const escapedText = text.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')\n\n return new RegExp(`^${escapedText}$`)\n}\n\nexport const withoutNodeProtocol = (name: ModuleName): ModuleNameWithoutNodePrefix => {\n return name.replace(/^node:/, '') as ModuleNameWithoutNodePrefix\n}\n","import { createRequire } from 'node:module'\nimport inject from '@rollup/plugin-inject'\nimport stdLibBrowser from 'node-stdlib-browser'\nimport { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin'\nimport esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'\nimport type { Plugin } from 'vite'\nimport { compareModuleNames, isEnabled, isNodeProtocolImport, toRegExp, withoutNodeProtocol } from './utils'\n\nexport type BuildTarget = 'build' | 'dev'\nexport type BooleanOrBuildTarget = boolean | BuildTarget\nexport type ModuleName = keyof typeof stdLibBrowser\nexport type ModuleNameWithoutNodePrefix<T = ModuleName> = T extends `node:${infer P}` ? P : never\n\nexport type PolyfillOptions = {\n /**\n * Includes specific modules. If empty, includes all modules\n * @example\n *\n * ```ts\n * nodePolyfills({\n * include: ['fs', 'path'],\n * })\n * ```\n */\n include?: ModuleNameWithoutNodePrefix[],\n /**\n * @example\n *\n * ```ts\n * nodePolyfills({\n * exclude: ['fs', 'path'],\n * })\n * ```\n */\n exclude?: ModuleNameWithoutNodePrefix[],\n /**\n * Specify whether specific globals should be polyfilled.\n *\n * @example\n *\n * ```ts\n * nodePolyfills({\n * globals: {\n * Buffer: false,\n * global: true,\n * process: 'build',\n * },\n * })\n * ```\n */\n globals?: {\n Buffer?: BooleanOrBuildTarget,\n global?: BooleanOrBuildTarget,\n process?: BooleanOrBuildTarget,\n },\n /**\n * Specify alternative modules to use in place of the default polyfills.\n *\n * @example\n *\n * ```ts\n * nodePolyfills({\n * overrides: {\n * fs: 'memfs',\n * },\n * })\n * ```\n */\n overrides?: { [Key in ModuleNameWithoutNodePrefix]?: string },\n /**\n * Specify whether the Node protocol version of an import (e.g. `node:buffer`) should be polyfilled too.\n *\n * @default true\n */\n protocolImports?: boolean,\n}\n\nexport type PolyfillOptionsResolved = {\n include: ModuleNameWithoutNodePrefix[],\n exclude: ModuleNameWithoutNodePrefix[],\n globals: {\n Buffer: BooleanOrBuildTarget,\n global: BooleanOrBuildTarget,\n process: BooleanOrBuildTarget,\n },\n overrides: { [Key in ModuleNameWithoutNodePrefix]?: string },\n protocolImports: boolean,\n}\n\nconst globalShimBanners = {\n buffer: [\n `import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'`,\n `globalThis.Buffer = globalThis.Buffer || __buffer_polyfill`,\n ],\n global: [\n `import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'`,\n `globalThis.global = globalThis.global || __global_polyfill`,\n ],\n process: [\n `import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'`,\n `globalThis.process = globalThis.process || __process_polyfill`,\n ],\n}\n\n/**\n * Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.\n *\n * @example\n *\n * ```ts\n * // vite.config.ts\n * import { defineConfig } from 'vite'\n * import { nodePolyfills } from 'vite-plugin-node-polyfills'\n *\n * export default defineConfig({\n * plugins: [\n * nodePolyfills({\n * // Specific modules that should not be polyfilled.\n * exclude: [],\n * // Whether to polyfill specific globals.\n * globals: {\n * Buffer: true, // can also be 'build', 'dev', or false\n * global: true,\n * process: true,\n * },\n * // Whether to polyfill `node:` protocol imports.\n * protocolImports: true,\n * }),\n * ],\n * })\n * ```\n */\nexport const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {\n const optionsResolved: PolyfillOptionsResolved = {\n include: [],\n exclude: [],\n overrides: {},\n protocolImports: true,\n ...options,\n globals: {\n Buffer: true,\n global: true,\n process: true,\n ...options.globals,\n },\n }\n\n const isExcluded = (moduleName: ModuleName) => {\n if (optionsResolved.include.length > 0) {\n return !optionsResolved.include.some((includedName) => compareModuleNames(moduleName, includedName))\n }\n\n return optionsResolved.exclude.some((excludedName) => compareModuleNames(moduleName, excludedName))\n }\n\n const toOverride = (name: ModuleNameWithoutNodePrefix): string | void => {\n if (isEnabled(optionsResolved.globals.Buffer, 'dev') && /^buffer$/.test(name)) {\n return 'vite-plugin-node-polyfills/shims/buffer'\n }\n\n if (isEnabled(optionsResolved.globals.global, 'dev') && /^global$/.test(name)) {\n return 'vite-plugin-node-polyfills/shims/global'\n }\n\n if (isEnabled(optionsResolved.globals.process, 'dev') && /^process$/.test(name)) {\n return 'vite-plugin-node-polyfills/shims/process'\n }\n\n if (name in optionsResolved.overrides) {\n return optionsResolved.overrides[name]\n }\n }\n\n const polyfills = (Object.entries(stdLibBrowser) as Array<[ModuleName, string]>).reduce<Record<ModuleName, string>>((included, [name, value]) => {\n if (!optionsResolved.protocolImports) {\n if (isNodeProtocolImport(name)) {\n return included\n }\n }\n\n if (!isExcluded(name)) {\n included[name] = toOverride(withoutNodeProtocol(name)) || value\n }\n\n return included\n }, {} as Record<ModuleName, string>)\n\n const require = createRequire(import.meta.url)\n const globalShimPaths = [\n ...((isEnabled(optionsResolved.globals.Buffer, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/buffer')] : []),\n ...((isEnabled(optionsResolved.globals.global, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/global')] : []),\n ...((isEnabled(optionsResolved.globals.process, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/process')] : []),\n ]\n\n const globalShimsBanner = [\n ...((isEnabled(optionsResolved.globals.Buffer, 'dev')) ? globalShimBanners.buffer : []),\n ...((isEnabled(optionsResolved.globals.global, 'dev')) ? globalShimBanners.global : []),\n ...((isEnabled(optionsResolved.globals.process, 'dev')) ? globalShimBanners.process : []),\n ``,\n ].join('\\n')\n\n return {\n name: 'vite-plugin-node-polyfills',\n config: (config, env) => {\n const isDev = env.command === 'serve'\n\n return {\n build: {\n rollupOptions: {\n onwarn: (warning, rollupWarn) => {\n handleCircularDependancyWarning(warning, () => {\n if (config.build?.rollupOptions?.onwarn) {\n return config.build.rollupOptions.onwarn(warning, rollupWarn)\n }\n\n rollupWarn(warning)\n })\n },\n plugins: [\n {\n ...inject({\n // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite\n ...(isEnabled(optionsResolved.globals.Buffer, 'build') ? { Buffer: 'vite-plugin-node-polyfills/shims/buffer' } : {}),\n ...(isEnabled(optionsResolved.globals.global, 'build') ? { global: 'vite-plugin-node-polyfills/shims/global' } : {}),\n ...(isEnabled(optionsResolved.globals.process, 'build') ? { process: 'vite-plugin-node-polyfills/shims/process' } : {}),\n }),\n },\n ],\n },\n },\n esbuild: {\n // In dev, the global polyfills need to be injected as a banner in order for isolated scripts (such as Vue SFCs) to have access to them.\n banner: isDev ? globalShimsBanner : undefined,\n },\n optimizeDeps: {\n exclude: [\n ...globalShimPaths,\n ],\n esbuildOptions: {\n banner: isDev ? { js: globalShimsBanner } : undefined,\n // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209\n define: {\n ...((isDev && isEnabled(optionsResolved.globals.Buffer, 'dev')) ? { Buffer: 'Buffer' } : {}),\n ...((isDev && isEnabled(optionsResolved.globals.global, 'dev')) ? { global: 'global' } : {}),\n ...((isDev && isEnabled(optionsResolved.globals.process, 'dev')) ? { process: 'process' } : {}),\n },\n inject: [\n ...globalShimPaths,\n ],\n plugins: [\n esbuildPlugin(polyfills),\n // Supress the 'injected path \"...\" cannot be marked as external' error in Vite 4 (emitted by esbuild).\n // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1469\n {\n name: 'vite-plugin-node-polyfills-shims-resolver',\n setup(build) {\n for (const globalShimPath of globalShimPaths) {\n const globalShimsFilter = toRegExp(globalShimPath)\n\n // https://esbuild.github.io/plugins/#on-resolve\n build.onResolve({ filter: globalShimsFilter }, () => {\n return {\n // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1468\n external: false,\n path: globalShimPath,\n }\n })\n }\n },\n },\n ],\n },\n },\n resolve: {\n // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L150\n alias: {\n ...polyfills,\n },\n },\n }\n },\n }\n}\n"],"names":["compareModuleNames","moduleA","moduleB","withoutNodeProtocol","isEnabled","value","mode","isNodeProtocolImport","name","toRegExp","text","escapedText","globalShimBanners","nodePolyfills","options","optionsResolved","isExcluded","moduleName","includedName","excludedName","toOverride","polyfills","stdLibBrowser","included","require","createRequire","_documentCurrentScript","globalShimPaths","globalShimsBanner","config","env","isDev","warning","rollupWarn","handleCircularDependancyWarning","inject","esbuildPlugin","build","globalShimPath","globalShimsFilter"],"mappings":"sZAEaA,EAAqB,CAACC,EAAqBC,IAC/CC,EAAoBF,CAAO,IAAME,EAAoBD,CAAO,EAGxDE,EAAY,CAACC,EAA6BC,IAChDD,EACDA,IAAU,GAAa,GAEpBA,IAAUC,EAHE,GAMRC,EAAwBC,GAC5BA,EAAK,WAAW,OAAO,EAGnBC,EAAYC,GAAiB,CAExC,MAAMC,EAAcD,EAAK,QAAQ,sBAAuB,MAAM,EAE9D,OAAO,IAAI,OAAO,IAAIC,CAAW,GAAG,CACtC,EAEaR,EAAuBK,GAC3BA,EAAK,QAAQ,SAAU,EAAE,ECgE5BI,EAAoB,CACxB,OAAQ,CACN,0EACA,4DACF,EACA,OAAQ,CACN,0EACA,4DACF,EACA,QAAS,CACP,4EACA,+DACF,CACF,EA8BaC,EAAgB,CAACC,EAA2B,KAAe,CACtE,MAAMC,EAA2C,CAC/C,QAAS,CAAC,EACV,QAAS,CAAC,EACV,UAAW,CAAC,EACZ,gBAAiB,GACjB,GAAGD,EACH,QAAS,CACP,OAAQ,GACR,OAAQ,GACR,QAAS,GACT,GAAGA,EAAQ,OACb,CAAA,EAGIE,EAAcC,GACdF,EAAgB,QAAQ,OAAS,EAC5B,CAACA,EAAgB,QAAQ,KA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AAQ,QAAS,KAAK,EAAK,CAAE,QAAS,SAAU,EAAI,CAAC,CAC/F,EACA,OAAQ,CACN,GAAGY,CACL,EACA,QAAS,CACPS,EAAAA,QAAcf,CAAS,EAGvB,CACE,KAAM,4CACN,MAAMgB,EAAO,CACX,UAAWC,KAAkBX,EAAiB,CACtC,MAAAY,EAAoB9B,EAAS6B,CAAc,EAGjDD,EAAM,UAAU,CAAE,OAAQE,GAAqB,KACtC,CAEL,SAAU,GACV,KAAMD,CAAA,EAET,CACH,CACF,CACF,CACF,CACF,CACF,EACA,QAAS,CAEP,MAAO,CACL,GAAGjB,CACL,CACF,CAAA,CAEJ,CAAA,CAEJ"}
{"version":3,"file":"index.cjs","sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { BooleanOrBuildTarget, ModuleName, ModuleNameWithoutNodePrefix } from './index'\n\nexport const compareModuleNames = (moduleA: ModuleName, moduleB: ModuleName) => {\n return withoutNodeProtocol(moduleA) === withoutNodeProtocol(moduleB)\n}\n\nexport const isEnabled = (value: BooleanOrBuildTarget, mode: 'build' | 'dev') => {\n if (!value) return false\n if (value === true) return true\n\n return value === mode\n}\n\nexport const isNodeProtocolImport = (name: string) => {\n return name.startsWith('node:')\n}\n\nexport const toRegExp = (text: string) => {\n // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping\n const escapedText = text.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')\n\n return new RegExp(`^${escapedText}$`)\n}\n\nexport const withoutNodeProtocol = (name: ModuleName): ModuleNameWithoutNodePrefix => {\n return name.replace(/^node:/, '') as ModuleNameWithoutNodePrefix\n}\n","import { createRequire } from 'node:module'\nimport inject from '@rollup/plugin-inject'\nimport stdLibBrowser from 'node-stdlib-browser'\nimport { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin'\nimport esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'\nimport type { Plugin } from 'vite'\nimport { compareModuleNames, isEnabled, isNodeProtocolImport, toRegExp, withoutNodeProtocol } from './utils'\n\nexport type BuildTarget = 'build' | 'dev'\nexport type BooleanOrBuildTarget = boolean | BuildTarget\nexport type ModuleName = keyof typeof stdLibBrowser\nexport type ModuleNameWithoutNodePrefix<T = ModuleName> = T extends `node:${infer P}` ? P : never\n\nexport type PolyfillOptions = {\n /**\n * Includes specific modules. If empty, includes all modules\n * @example\n *\n * ```ts\n * nodePolyfills({\n * include: ['fs', 'path'],\n * })\n * ```\n */\n include?: ModuleNameWithoutNodePrefix[],\n /**\n * @example\n *\n * ```ts\n * nodePolyfills({\n * exclude: ['fs', 'path'],\n * })\n * ```\n */\n exclude?: ModuleNameWithoutNodePrefix[],\n /**\n * Specify whether specific globals should be polyfilled.\n *\n * @example\n *\n * ```ts\n * nodePolyfills({\n * globals: {\n * Buffer: false,\n * global: true,\n * process: 'build',\n * },\n * })\n * ```\n */\n globals?: {\n Buffer?: BooleanOrBuildTarget,\n global?: BooleanOrBuildTarget,\n process?: BooleanOrBuildTarget,\n },\n /**\n * Specify alternative modules to use in place of the default polyfills.\n *\n * @example\n *\n * ```ts\n * nodePolyfills({\n * overrides: {\n * fs: 'memfs',\n * },\n * })\n * ```\n */\n overrides?: { [Key in ModuleNameWithoutNodePrefix]?: string },\n /**\n * Specify whether the Node protocol version of an import (e.g. `node:buffer`) should be polyfilled too.\n *\n * @default true\n */\n protocolImports?: boolean,\n}\n\nexport type PolyfillOptionsResolved = {\n include: ModuleNameWithoutNodePrefix[],\n exclude: ModuleNameWithoutNodePrefix[],\n globals: {\n Buffer: BooleanOrBuildTarget,\n global: BooleanOrBuildTarget,\n process: BooleanOrBuildTarget,\n },\n overrides: { [Key in ModuleNameWithoutNodePrefix]?: string },\n protocolImports: boolean,\n}\n\nconst globalShimBanners = {\n buffer: [\n `import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'`,\n `globalThis.Buffer = globalThis.Buffer || __buffer_polyfill`,\n ],\n global: [\n `import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'`,\n `globalThis.global = globalThis.global || __global_polyfill`,\n ],\n process: [\n `import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'`,\n `globalThis.process = globalThis.process || __process_polyfill`,\n ],\n}\n\n/**\n * Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.\n *\n * @example\n *\n * ```ts\n * // vite.config.ts\n * import { defineConfig } from 'vite'\n * import { nodePolyfills } from 'vite-plugin-node-polyfills'\n *\n * export default defineConfig({\n * plugins: [\n * nodePolyfills({\n * // Specific modules that should not be polyfilled.\n * exclude: [],\n * // Whether to polyfill specific globals.\n * globals: {\n * Buffer: true, // can also be 'build', 'dev', or false\n * global: true,\n * process: true,\n * },\n * // Whether to polyfill `node:` protocol imports.\n * protocolImports: true,\n * }),\n * ],\n * })\n * ```\n */\nexport const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {\n const optionsResolved: PolyfillOptionsResolved = {\n include: [],\n exclude: [],\n overrides: {},\n protocolImports: true,\n ...options,\n globals: {\n Buffer: true,\n global: true,\n process: true,\n ...options.globals,\n },\n }\n\n const isExcluded = (moduleName: ModuleName) => {\n if (optionsResolved.include.length > 0) {\n return !optionsResolved.include.some((includedName) => compareModuleNames(moduleName, includedName))\n }\n\n return optionsResolved.exclude.some((excludedName) => compareModuleNames(moduleName, excludedName))\n }\n\n const toOverride = (name: ModuleNameWithoutNodePrefix): string | void => {\n if (isEnabled(optionsResolved.globals.Buffer, 'dev') && /^buffer$/.test(name)) {\n return 'vite-plugin-node-polyfills/shims/buffer'\n }\n\n if (isEnabled(optionsResolved.globals.global, 'dev') && /^global$/.test(name)) {\n return 'vite-plugin-node-polyfills/shims/global'\n }\n\n if (isEnabled(optionsResolved.globals.process, 'dev') && /^process$/.test(name)) {\n return 'vite-plugin-node-polyfills/shims/process'\n }\n\n if (name in optionsResolved.overrides) {\n return optionsResolved.overrides[name]\n }\n }\n\n const polyfills = (Object.entries(stdLibBrowser) as Array<[ModuleName, string]>).reduce<Record<ModuleName, string>>((included, [name, value]) => {\n if (!optionsResolved.protocolImports) {\n if (isNodeProtocolImport(name)) {\n return included\n }\n }\n\n if (!isExcluded(name)) {\n included[name] = toOverride(withoutNodeProtocol(name)) || value\n }\n\n return included\n }, {} as Record<ModuleName, string>)\n\n const require = createRequire(import.meta.url)\n const globalShimPaths = [\n ...((isEnabled(optionsResolved.globals.Buffer, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/buffer')] : []),\n ...((isEnabled(optionsResolved.globals.global, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/global')] : []),\n ...((isEnabled(optionsResolved.globals.process, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/process')] : []),\n ]\n\n const globalShimsBanner = [\n ...((isEnabled(optionsResolved.globals.Buffer, 'dev')) ? globalShimBanners.buffer : []),\n ...((isEnabled(optionsResolved.globals.global, 'dev')) ? globalShimBanners.global : []),\n ...((isEnabled(optionsResolved.globals.process, 'dev')) ? globalShimBanners.process : []),\n ``,\n ].join('\\n')\n\n return {\n name: 'vite-plugin-node-polyfills',\n config: (config, env) => {\n const isDev = env.command === 'serve'\n\n const shimsToInject = {\n // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite\n ...(isEnabled(optionsResolved.globals.Buffer, 'build') ? { Buffer: 'vite-plugin-node-polyfills/shims/buffer' } : {}),\n ...(isEnabled(optionsResolved.globals.global, 'build') ? { global: 'vite-plugin-node-polyfills/shims/global' } : {}),\n ...(isEnabled(optionsResolved.globals.process, 'build') ? { process: 'vite-plugin-node-polyfills/shims/process' } : {}),\n }\n\n return {\n build: {\n rollupOptions: {\n onwarn: (warning, rollupWarn) => {\n handleCircularDependancyWarning(warning, () => {\n if (config.build?.rollupOptions?.onwarn) {\n return config.build.rollupOptions.onwarn(warning, rollupWarn)\n }\n\n rollupWarn(warning)\n })\n },\n plugins: Object.keys(shimsToInject).length > 0 ? [inject(shimsToInject)] : [],\n },\n },\n esbuild: {\n // In dev, the global polyfills need to be injected as a banner in order for isolated scripts (such as Vue SFCs) to have access to them.\n banner: isDev ? globalShimsBanner : undefined,\n },\n optimizeDeps: {\n exclude: [\n ...globalShimPaths,\n ],\n esbuildOptions: {\n banner: isDev ? { js: globalShimsBanner } : undefined,\n // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209\n define: {\n ...((isDev && isEnabled(optionsResolved.globals.Buffer, 'dev')) ? { Buffer: 'Buffer' } : {}),\n ...((isDev && isEnabled(optionsResolved.globals.global, 'dev')) ? { global: 'global' } : {}),\n ...((isDev && isEnabled(optionsResolved.globals.process, 'dev')) ? { process: 'process' } : {}),\n },\n inject: [\n ...globalShimPaths,\n ],\n plugins: [\n esbuildPlugin(polyfills),\n // Supress the 'injected path \"...\" cannot be marked as external' error in Vite 4 (emitted by esbuild).\n // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1469\n {\n name: 'vite-plugin-node-polyfills-shims-resolver',\n setup(build) {\n for (const globalShimPath of globalShimPaths) {\n const globalShimsFilter = toRegExp(globalShimPath)\n\n // https://esbuild.github.io/plugins/#on-resolve\n build.onResolve({ filter: globalShimsFilter }, () => {\n return {\n // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1468\n external: false,\n path: globalShimPath,\n }\n })\n }\n },\n },\n ],\n },\n },\n resolve: {\n // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L150\n alias: {\n ...polyfills,\n },\n },\n }\n },\n }\n}\n"],"names":["compareModuleNames","moduleA","moduleB","withoutNodeProtocol","isEnabled","value","mode","isNodeProtocolImport","name","toRegExp","text","escapedText","globalShimBanners","nodePolyfills","options","optionsResolved","isExcluded","moduleName","includedName","excludedName","toOverride","polyfills","stdLibBrowser","included","require","createRequire","_documentCurrentScript","globalShimPaths","globalShimsBanner","config","env","isDev","shimsToInject","warning","rollupWarn","handleCircularDependancyWarning","inject","esbuildPlugin","build","globalShimPath","globalShimsFilter"],"mappings":"sZAEaA,EAAqB,CAACC,EAAqBC,IAC/CC,EAAoBF,CAAO,IAAME,EAAoBD,CAAO,EAGxDE,EAAY,CAACC,EAA6BC,IAChDD,EACDA,IAAU,GAAa,GAEpBA,IAAUC,EAHE,GAMRC,EAAwBC,GAC5BA,EAAK,WAAW,OAAO,EAGnBC,EAAYC,GAAiB,CAExC,MAAMC,EAAcD,EAAK,QAAQ,sBAAuB,MAAM,EAE9D,OAAO,IAAI,OAAO,IAAIC,CAAW,GAAG,CACtC,EAEaR,EAAuBK,GAC3BA,EAAK,QAAQ,SAAU,EAAE,ECgE5BI,EAAoB,CACxB,OAAQ,CACN,0EACA,4DACF,EACA,OAAQ,CACN,0EACA,4DACF,EACA,QAAS,CACP,4EACA,+DACF,CACF,EA8BaC,EAAgB,CAACC,EAA2B,KAAe,CACtE,MAAMC,EAA2C,CAC/C,QAAS,CAAC,EACV,QAAS,CAAC,EACV,UAAW,CAAC,EACZ,gBAAiB,GACjB,GAAGD,EACH,QAAS,CACP,OAAQ,GACR,OAAQ,GACR,QAAS,GACT,GAAGA,EAAQ,OACb,CAAA,EAGIE,EAAcC,GACdF,EAAgB,QAAQ,OAAS,EAC5B,CAACA,EAAgB,QAAQ,KAAMG,GAAiBlB,EAAmBiB,EAAYC,CAAY,CAAC,EAG9FH,EAAgB,QAAQ,KAAMI,GAAiBnB,EAAmBiB,EAAYE,CAAY,CAAC,EAG9FC,EAAcZ,GAAqD,CACnE,GAAAJ,EAAUW,EAAgB,QAAQ,OAAQ,KAAK,GAAK,WAAW,KAAKP,CAAI,EACnE,MAAA,0CAGL,GAAAJ,EAAUW,EAAgB,QAAQ,OAAQ,KAAK,GAAK,WAAW,KAAKP,CAAI,EACnE,MA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AM,UAAU,CAAE,OAAQE,GAAqB,KACtC,CAEL,SAAU,GACV,KAAMD,CAAA,EAET,CACH,CACF,CACF,CACF,CACF,CACF,EACA,QAAS,CAEP,MAAO,CACL,GAAGlB,CACL,CACF,CAAA,CAEJ,CAAA,CAEJ"}

@@ -1,7 +0,7 @@

import { createRequire as h } from "node:module";
import y from "@rollup/plugin-inject";
import _ from "node-stdlib-browser";
import { handleCircularDependancyWarning as B } from "node-stdlib-browser/helpers/rollup/plugin";
import x from "node-stdlib-browser/helpers/esbuild/plugin";
const c = (s, l) => b(s) === b(l), e = (s, l) => s ? s === !0 ? !0 : s === l : !1, T = (s) => s.startsWith("node:"), $ = (s) => {
import { createRequire as y } from "node:module";
import _ from "@rollup/plugin-inject";
import B from "node-stdlib-browser";
import { handleCircularDependancyWarning as x } from "node-stdlib-browser/helpers/rollup/plugin";
import T from "node-stdlib-browser/helpers/esbuild/plugin";
const d = (s, l) => b(s) === b(l), e = (s, l) => s ? s === !0 ? !0 : s === l : !1, j = (s) => s.startsWith("node:"), O = (s) => {
const l = s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");

@@ -22,3 +22,3 @@ return new RegExp(`^${l}$`);

]
}, w = (s = {}) => {
}, E = (s = {}) => {
const l = {

@@ -36,3 +36,3 @@ include: [],

}
}, d = (o) => l.include.length > 0 ? !l.include.some((r) => c(o, r)) : l.exclude.some((r) => c(o, r)), m = (o) => {
}, m = (o) => l.include.length > 0 ? !l.include.some((r) => d(o, r)) : l.exclude.some((r) => d(o, r)), v = (o) => {
if (e(l.globals.Buffer, "dev") && /^buffer$/.test(o))

@@ -46,3 +46,3 @@ return "vite-plugin-node-polyfills/shims/buffer";

return l.overrides[o];
}, g = Object.entries(_).reduce((o, [r, i]) => (!l.protocolImports && T(r) || d(r) || (o[r] = m(b(r)) || i), o), {}), p = h(import.meta.url), f = [
}, g = Object.entries(B).reduce((o, [r, i]) => (!l.protocolImports && j(r) || m(r) || (o[r] = v(b(r)) || i), o), {}), p = y(import.meta.url), f = [
...e(l.globals.Buffer, "dev") ? [p.resolve("vite-plugin-node-polyfills/shims/buffer")] : [],

@@ -61,3 +61,8 @@ ...e(l.globals.global, "dev") ? [p.resolve("vite-plugin-node-polyfills/shims/global")] : [],

config: (o, r) => {
const i = r.command === "serve";
const i = r.command === "serve", c = {
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite
...e(l.globals.Buffer, "build") ? { Buffer: "vite-plugin-node-polyfills/shims/buffer" } : {},
...e(l.globals.global, "build") ? { global: "vite-plugin-node-polyfills/shims/global" } : {},
...e(l.globals.process, "build") ? { process: "vite-plugin-node-polyfills/shims/process" } : {}
};
return {

@@ -67,3 +72,3 @@ build: {

onwarn: (t, n) => {
B(t, () => {
x(t, () => {
if (o.build?.rollupOptions?.onwarn)

@@ -74,12 +79,3 @@ return o.build.rollupOptions.onwarn(t, n);

},
plugins: [
{
...y({
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite
...e(l.globals.Buffer, "build") ? { Buffer: "vite-plugin-node-polyfills/shims/buffer" } : {},
...e(l.globals.global, "build") ? { global: "vite-plugin-node-polyfills/shims/global" } : {},
...e(l.globals.process, "build") ? { process: "vite-plugin-node-polyfills/shims/process" } : {}
})
}
]
plugins: Object.keys(c).length > 0 ? [_(c)] : []
}

@@ -107,3 +103,3 @@ },

plugins: [
x(g),
T(g),
// Supress the 'injected path "..." cannot be marked as external' error in Vite 4 (emitted by esbuild).

@@ -115,4 +111,4 @@ // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1469

for (const n of f) {
const v = $(n);
t.onResolve({ filter: v }, () => ({
const h = O(n);
t.onResolve({ filter: h }, () => ({
// https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1468

@@ -139,4 +135,4 @@ external: !1,

export {
w as nodePolyfills
E as nodePolyfills
};
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.js","sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { BooleanOrBuildTarget, ModuleName, ModuleNameWithoutNodePrefix } from './index'\n\nexport const compareModuleNames = (moduleA: ModuleName, moduleB: ModuleName) => {\n return withoutNodeProtocol(moduleA) === withoutNodeProtocol(moduleB)\n}\n\nexport const isEnabled = (value: BooleanOrBuildTarget, mode: 'build' | 'dev') => {\n if (!value) return false\n if (value === true) return true\n\n return value === mode\n}\n\nexport const isNodeProtocolImport = (name: string) => {\n return name.startsWith('node:')\n}\n\nexport const toRegExp = (text: string) => {\n // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping\n const escapedText = text.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')\n\n return new RegExp(`^${escapedText}$`)\n}\n\nexport const withoutNodeProtocol = (name: ModuleName): ModuleNameWithoutNodePrefix => {\n return name.replace(/^node:/, '') as ModuleNameWithoutNodePrefix\n}\n","import { createRequire } from 'node:module'\nimport inject from '@rollup/plugin-inject'\nimport stdLibBrowser from 'node-stdlib-browser'\nimport { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin'\nimport esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'\nimport type { Plugin } from 'vite'\nimport { compareModuleNames, isEnabled, isNodeProtocolImport, toRegExp, withoutNodeProtocol } from './utils'\n\nexport type BuildTarget = 'build' | 'dev'\nexport type BooleanOrBuildTarget = boolean | BuildTarget\nexport type ModuleName = keyof typeof stdLibBrowser\nexport type ModuleNameWithoutNodePrefix<T = ModuleName> = T extends `node:${infer P}` ? P : never\n\nexport type PolyfillOptions = {\n /**\n * Includes specific modules. If empty, includes all modules\n * @example\n *\n * ```ts\n * nodePolyfills({\n * include: ['fs', 'path'],\n * })\n * ```\n */\n include?: ModuleNameWithoutNodePrefix[],\n /**\n * @example\n *\n * ```ts\n * nodePolyfills({\n * exclude: ['fs', 'path'],\n * })\n * ```\n */\n exclude?: ModuleNameWithoutNodePrefix[],\n /**\n * Specify whether specific globals should be polyfilled.\n *\n * @example\n *\n * ```ts\n * nodePolyfills({\n * globals: {\n * Buffer: false,\n * global: true,\n * process: 'build',\n * },\n * })\n * ```\n */\n globals?: {\n Buffer?: BooleanOrBuildTarget,\n global?: BooleanOrBuildTarget,\n process?: BooleanOrBuildTarget,\n },\n /**\n * Specify alternative modules to use in place of the default polyfills.\n *\n * @example\n *\n * ```ts\n * nodePolyfills({\n * overrides: {\n * fs: 'memfs',\n * },\n * })\n * ```\n */\n overrides?: { [Key in ModuleNameWithoutNodePrefix]?: string },\n /**\n * Specify whether the Node protocol version of an import (e.g. `node:buffer`) should be polyfilled too.\n *\n * @default true\n */\n protocolImports?: boolean,\n}\n\nexport type PolyfillOptionsResolved = {\n include: ModuleNameWithoutNodePrefix[],\n exclude: ModuleNameWithoutNodePrefix[],\n globals: {\n Buffer: BooleanOrBuildTarget,\n global: BooleanOrBuildTarget,\n process: BooleanOrBuildTarget,\n },\n overrides: { [Key in ModuleNameWithoutNodePrefix]?: string },\n protocolImports: boolean,\n}\n\nconst globalShimBanners = {\n buffer: [\n `import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'`,\n `globalThis.Buffer = globalThis.Buffer || __buffer_polyfill`,\n ],\n global: [\n `import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'`,\n `globalThis.global = globalThis.global || __global_polyfill`,\n ],\n process: [\n `import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'`,\n `globalThis.process = globalThis.process || __process_polyfill`,\n ],\n}\n\n/**\n * Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.\n *\n * @example\n *\n * ```ts\n * // vite.config.ts\n * import { defineConfig } from 'vite'\n * import { nodePolyfills } from 'vite-plugin-node-polyfills'\n *\n * export default defineConfig({\n * plugins: [\n * nodePolyfills({\n * // Specific modules that should not be polyfilled.\n * exclude: [],\n * // Whether to polyfill specific globals.\n * globals: {\n * Buffer: true, // can also be 'build', 'dev', or false\n * global: true,\n * process: true,\n * },\n * // Whether to polyfill `node:` protocol imports.\n * protocolImports: true,\n * }),\n * ],\n * })\n * ```\n */\nexport const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {\n const optionsResolved: PolyfillOptionsResolved = {\n include: [],\n exclude: [],\n overrides: {},\n protocolImports: true,\n ...options,\n globals: {\n Buffer: true,\n global: true,\n process: true,\n ...options.globals,\n },\n }\n\n const isExcluded = (moduleName: ModuleName) => {\n if (optionsResolved.include.length > 0) {\n return !optionsResolved.include.some((includedName) => compareModuleNames(moduleName, includedName))\n }\n\n return optionsResolved.exclude.some((excludedName) => compareModuleNames(moduleName, excludedName))\n }\n\n const toOverride = (name: ModuleNameWithoutNodePrefix): string | void => {\n if (isEnabled(optionsResolved.globals.Buffer, 'dev') && /^buffer$/.test(name)) {\n return 'vite-plugin-node-polyfills/shims/buffer'\n }\n\n if (isEnabled(optionsResolved.globals.global, 'dev') && /^global$/.test(name)) {\n return 'vite-plugin-node-polyfills/shims/global'\n }\n\n if (isEnabled(optionsResolved.globals.process, 'dev') && /^process$/.test(name)) {\n return 'vite-plugin-node-polyfills/shims/process'\n }\n\n if (name in optionsResolved.overrides) {\n return optionsResolved.overrides[name]\n }\n }\n\n const polyfills = (Object.entries(stdLibBrowser) as Array<[ModuleName, string]>).reduce<Record<ModuleName, string>>((included, [name, value]) => {\n if (!optionsResolved.protocolImports) {\n if (isNodeProtocolImport(name)) {\n return included\n }\n }\n\n if (!isExcluded(name)) {\n included[name] = toOverride(withoutNodeProtocol(name)) || value\n }\n\n return included\n }, {} as Record<ModuleName, string>)\n\n const require = createRequire(import.meta.url)\n const globalShimPaths = [\n ...((isEnabled(optionsResolved.globals.Buffer, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/buffer')] : []),\n ...((isEnabled(optionsResolved.globals.global, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/global')] : []),\n ...((isEnabled(optionsResolved.globals.process, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/process')] : []),\n ]\n\n const globalShimsBanner = [\n ...((isEnabled(optionsResolved.globals.Buffer, 'dev')) ? globalShimBanners.buffer : []),\n ...((isEnabled(optionsResolved.globals.global, 'dev')) ? globalShimBanners.global : []),\n ...((isEnabled(optionsResolved.globals.process, 'dev')) ? globalShimBanners.process : []),\n ``,\n ].join('\\n')\n\n return {\n name: 'vite-plugin-node-polyfills',\n config: (config, env) => {\n const isDev = env.command === 'serve'\n\n return {\n build: {\n rollupOptions: {\n onwarn: (warning, rollupWarn) => {\n handleCircularDependancyWarning(warning, () => {\n if (config.build?.rollupOptions?.onwarn) {\n return config.build.rollupOptions.onwarn(warning, rollupWarn)\n }\n\n rollupWarn(warning)\n })\n },\n plugins: [\n {\n ...inject({\n // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite\n ...(isEnabled(optionsResolved.globals.Buffer, 'build') ? { Buffer: 'vite-plugin-node-polyfills/shims/buffer' } : {}),\n ...(isEnabled(optionsResolved.globals.global, 'build') ? { global: 'vite-plugin-node-polyfills/shims/global' } : {}),\n ...(isEnabled(optionsResolved.globals.process, 'build') ? { process: 'vite-plugin-node-polyfills/shims/process' } : {}),\n }),\n },\n ],\n },\n },\n esbuild: {\n // In dev, the global polyfills need to be injected as a banner in order for isolated scripts (such as Vue SFCs) to have access to them.\n banner: isDev ? globalShimsBanner : undefined,\n },\n optimizeDeps: {\n exclude: [\n ...globalShimPaths,\n ],\n esbuildOptions: {\n banner: isDev ? { js: globalShimsBanner } : undefined,\n // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209\n define: {\n ...((isDev && isEnabled(optionsResolved.globals.Buffer, 'dev')) ? { Buffer: 'Buffer' } : {}),\n ...((isDev && isEnabled(optionsResolved.globals.global, 'dev')) ? { global: 'global' } : {}),\n ...((isDev && isEnabled(optionsResolved.globals.process, 'dev')) ? { process: 'process' } : {}),\n },\n inject: [\n ...globalShimPaths,\n ],\n plugins: [\n esbuildPlugin(polyfills),\n // Supress the 'injected path \"...\" cannot be marked as external' error in Vite 4 (emitted by esbuild).\n // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1469\n {\n name: 'vite-plugin-node-polyfills-shims-resolver',\n setup(build) {\n for (const globalShimPath of globalShimPaths) {\n const globalShimsFilter = toRegExp(globalShimPath)\n\n // https://esbuild.github.io/plugins/#on-resolve\n build.onResolve({ filter: globalShimsFilter }, () => {\n return {\n // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1468\n external: false,\n path: globalShimPath,\n }\n })\n }\n },\n },\n ],\n },\n },\n resolve: {\n // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L150\n alias: {\n ...polyfills,\n },\n },\n }\n },\n }\n}\n"],"names":["compareModuleNames","moduleA","moduleB","withoutNodeProtocol","isEnabled","value","mode","isNodeProtocolImport","name","toRegExp","text","escapedText","globalShimBanners","nodePolyfills","options","optionsResolved","isExcluded","moduleName","includedName","excludedName","toOverride","polyfills","stdLibBrowser","included","require","createRequire","globalShimPaths","globalShimsBanner","config","env","isDev","warning","rollupWarn","handleCircularDependancyWarning","inject","esbuildPlugin","build","globalShimPath","globalShimsFilter"],"mappings":";;;;;AAEa,MAAAA,IAAqB,CAACC,GAAqBC,MAC/CC,EAAoBF,CAAO,MAAME,EAAoBD,CAAO,GAGxDE,IAAY,CAACC,GAA6BC,MAChDD,IACDA,MAAU,KAAa,KAEpBA,MAAUC,IAHE,IAMRC,IAAuB,CAACC,MAC5BA,EAAK,WAAW,OAAO,GAGnBC,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AAAA,sBAEL,UAAU;AAAA,sBACV,MAAMD;AAAA,oBAAA,EAET;AAAA,kBACH;AAAA,gBACF;AAAA,cACF;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,QACA,SAAS;AAAA;AAAA,UAEP,OAAO;AAAA,YACL,GAAGhB;AAAA,UACL;AAAA,QACF;AAAA,MAAA;AAAA,IAEJ;AAAA,EAAA;AAEJ;"}
{"version":3,"file":"index.js","sources":["../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { BooleanOrBuildTarget, ModuleName, ModuleNameWithoutNodePrefix } from './index'\n\nexport const compareModuleNames = (moduleA: ModuleName, moduleB: ModuleName) => {\n return withoutNodeProtocol(moduleA) === withoutNodeProtocol(moduleB)\n}\n\nexport const isEnabled = (value: BooleanOrBuildTarget, mode: 'build' | 'dev') => {\n if (!value) return false\n if (value === true) return true\n\n return value === mode\n}\n\nexport const isNodeProtocolImport = (name: string) => {\n return name.startsWith('node:')\n}\n\nexport const toRegExp = (text: string) => {\n // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping\n const escapedText = text.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')\n\n return new RegExp(`^${escapedText}$`)\n}\n\nexport const withoutNodeProtocol = (name: ModuleName): ModuleNameWithoutNodePrefix => {\n return name.replace(/^node:/, '') as ModuleNameWithoutNodePrefix\n}\n","import { createRequire } from 'node:module'\nimport inject from '@rollup/plugin-inject'\nimport stdLibBrowser from 'node-stdlib-browser'\nimport { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin'\nimport esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'\nimport type { Plugin } from 'vite'\nimport { compareModuleNames, isEnabled, isNodeProtocolImport, toRegExp, withoutNodeProtocol } from './utils'\n\nexport type BuildTarget = 'build' | 'dev'\nexport type BooleanOrBuildTarget = boolean | BuildTarget\nexport type ModuleName = keyof typeof stdLibBrowser\nexport type ModuleNameWithoutNodePrefix<T = ModuleName> = T extends `node:${infer P}` ? P : never\n\nexport type PolyfillOptions = {\n /**\n * Includes specific modules. If empty, includes all modules\n * @example\n *\n * ```ts\n * nodePolyfills({\n * include: ['fs', 'path'],\n * })\n * ```\n */\n include?: ModuleNameWithoutNodePrefix[],\n /**\n * @example\n *\n * ```ts\n * nodePolyfills({\n * exclude: ['fs', 'path'],\n * })\n * ```\n */\n exclude?: ModuleNameWithoutNodePrefix[],\n /**\n * Specify whether specific globals should be polyfilled.\n *\n * @example\n *\n * ```ts\n * nodePolyfills({\n * globals: {\n * Buffer: false,\n * global: true,\n * process: 'build',\n * },\n * })\n * ```\n */\n globals?: {\n Buffer?: BooleanOrBuildTarget,\n global?: BooleanOrBuildTarget,\n process?: BooleanOrBuildTarget,\n },\n /**\n * Specify alternative modules to use in place of the default polyfills.\n *\n * @example\n *\n * ```ts\n * nodePolyfills({\n * overrides: {\n * fs: 'memfs',\n * },\n * })\n * ```\n */\n overrides?: { [Key in ModuleNameWithoutNodePrefix]?: string },\n /**\n * Specify whether the Node protocol version of an import (e.g. `node:buffer`) should be polyfilled too.\n *\n * @default true\n */\n protocolImports?: boolean,\n}\n\nexport type PolyfillOptionsResolved = {\n include: ModuleNameWithoutNodePrefix[],\n exclude: ModuleNameWithoutNodePrefix[],\n globals: {\n Buffer: BooleanOrBuildTarget,\n global: BooleanOrBuildTarget,\n process: BooleanOrBuildTarget,\n },\n overrides: { [Key in ModuleNameWithoutNodePrefix]?: string },\n protocolImports: boolean,\n}\n\nconst globalShimBanners = {\n buffer: [\n `import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'`,\n `globalThis.Buffer = globalThis.Buffer || __buffer_polyfill`,\n ],\n global: [\n `import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'`,\n `globalThis.global = globalThis.global || __global_polyfill`,\n ],\n process: [\n `import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'`,\n `globalThis.process = globalThis.process || __process_polyfill`,\n ],\n}\n\n/**\n * Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.\n *\n * @example\n *\n * ```ts\n * // vite.config.ts\n * import { defineConfig } from 'vite'\n * import { nodePolyfills } from 'vite-plugin-node-polyfills'\n *\n * export default defineConfig({\n * plugins: [\n * nodePolyfills({\n * // Specific modules that should not be polyfilled.\n * exclude: [],\n * // Whether to polyfill specific globals.\n * globals: {\n * Buffer: true, // can also be 'build', 'dev', or false\n * global: true,\n * process: true,\n * },\n * // Whether to polyfill `node:` protocol imports.\n * protocolImports: true,\n * }),\n * ],\n * })\n * ```\n */\nexport const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {\n const optionsResolved: PolyfillOptionsResolved = {\n include: [],\n exclude: [],\n overrides: {},\n protocolImports: true,\n ...options,\n globals: {\n Buffer: true,\n global: true,\n process: true,\n ...options.globals,\n },\n }\n\n const isExcluded = (moduleName: ModuleName) => {\n if (optionsResolved.include.length > 0) {\n return !optionsResolved.include.some((includedName) => compareModuleNames(moduleName, includedName))\n }\n\n return optionsResolved.exclude.some((excludedName) => compareModuleNames(moduleName, excludedName))\n }\n\n const toOverride = (name: ModuleNameWithoutNodePrefix): string | void => {\n if (isEnabled(optionsResolved.globals.Buffer, 'dev') && /^buffer$/.test(name)) {\n return 'vite-plugin-node-polyfills/shims/buffer'\n }\n\n if (isEnabled(optionsResolved.globals.global, 'dev') && /^global$/.test(name)) {\n return 'vite-plugin-node-polyfills/shims/global'\n }\n\n if (isEnabled(optionsResolved.globals.process, 'dev') && /^process$/.test(name)) {\n return 'vite-plugin-node-polyfills/shims/process'\n }\n\n if (name in optionsResolved.overrides) {\n return optionsResolved.overrides[name]\n }\n }\n\n const polyfills = (Object.entries(stdLibBrowser) as Array<[ModuleName, string]>).reduce<Record<ModuleName, string>>((included, [name, value]) => {\n if (!optionsResolved.protocolImports) {\n if (isNodeProtocolImport(name)) {\n return included\n }\n }\n\n if (!isExcluded(name)) {\n included[name] = toOverride(withoutNodeProtocol(name)) || value\n }\n\n return included\n }, {} as Record<ModuleName, string>)\n\n const require = createRequire(import.meta.url)\n const globalShimPaths = [\n ...((isEnabled(optionsResolved.globals.Buffer, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/buffer')] : []),\n ...((isEnabled(optionsResolved.globals.global, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/global')] : []),\n ...((isEnabled(optionsResolved.globals.process, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/process')] : []),\n ]\n\n const globalShimsBanner = [\n ...((isEnabled(optionsResolved.globals.Buffer, 'dev')) ? globalShimBanners.buffer : []),\n ...((isEnabled(optionsResolved.globals.global, 'dev')) ? globalShimBanners.global : []),\n ...((isEnabled(optionsResolved.globals.process, 'dev')) ? globalShimBanners.process : []),\n ``,\n ].join('\\n')\n\n return {\n name: 'vite-plugin-node-polyfills',\n config: (config, env) => {\n const isDev = env.command === 'serve'\n\n const shimsToInject = {\n // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite\n ...(isEnabled(optionsResolved.globals.Buffer, 'build') ? { Buffer: 'vite-plugin-node-polyfills/shims/buffer' } : {}),\n ...(isEnabled(optionsResolved.globals.global, 'build') ? { global: 'vite-plugin-node-polyfills/shims/global' } : {}),\n ...(isEnabled(optionsResolved.globals.process, 'build') ? { process: 'vite-plugin-node-polyfills/shims/process' } : {}),\n }\n\n return {\n build: {\n rollupOptions: {\n onwarn: (warning, rollupWarn) => {\n handleCircularDependancyWarning(warning, () => {\n if (config.build?.rollupOptions?.onwarn) {\n return config.build.rollupOptions.onwarn(warning, rollupWarn)\n }\n\n rollupWarn(warning)\n })\n },\n plugins: Object.keys(shimsToInject).length > 0 ? [inject(shimsToInject)] : [],\n },\n },\n esbuild: {\n // In dev, the global polyfills need to be injected as a banner in order for isolated scripts (such as Vue SFCs) to have access to them.\n banner: isDev ? globalShimsBanner : undefined,\n },\n optimizeDeps: {\n exclude: [\n ...globalShimPaths,\n ],\n esbuildOptions: {\n banner: isDev ? { js: globalShimsBanner } : undefined,\n // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209\n define: {\n ...((isDev && isEnabled(optionsResolved.globals.Buffer, 'dev')) ? { Buffer: 'Buffer' } : {}),\n ...((isDev && isEnabled(optionsResolved.globals.global, 'dev')) ? { global: 'global' } : {}),\n ...((isDev && isEnabled(optionsResolved.globals.process, 'dev')) ? { process: 'process' } : {}),\n },\n inject: [\n ...globalShimPaths,\n ],\n plugins: [\n esbuildPlugin(polyfills),\n // Supress the 'injected path \"...\" cannot be marked as external' error in Vite 4 (emitted by esbuild).\n // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1469\n {\n name: 'vite-plugin-node-polyfills-shims-resolver',\n setup(build) {\n for (const globalShimPath of globalShimPaths) {\n const globalShimsFilter = toRegExp(globalShimPath)\n\n // https://esbuild.github.io/plugins/#on-resolve\n build.onResolve({ filter: globalShimsFilter }, () => {\n return {\n // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1468\n external: false,\n path: globalShimPath,\n }\n })\n }\n },\n },\n ],\n },\n },\n resolve: {\n // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L150\n alias: {\n ...polyfills,\n },\n },\n }\n },\n }\n}\n"],"names":["compareModuleNames","moduleA","moduleB","withoutNodeProtocol","isEnabled","value","mode","isNodeProtocolImport","name","toRegExp","text","escapedText","globalShimBanners","nodePolyfills","options","optionsResolved","isExcluded","moduleName","includedName","excludedName","toOverride","polyfills","stdLibBrowser","included","require","createRequire","globalShimPaths","globalShimsBanner","config","env","isDev","shimsToInject","warning","rollupWarn","handleCircularDependancyWarning","inject","esbuildPlugin","build","globalShimPath","globalShimsFilter"],"mappings":";;;;;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AAAA,YACA,SAAS;AAAA,cACPU,EAAcf,CAAS;AAAA;AAAA;AAAA,cAGvB;AAAA,gBACE,MAAM;AAAA,gBACN,MAAMgB,GAAO;AACX,6BAAWC,KAAkBZ,GAAiB;AACtC,0BAAAa,IAAoB9B,EAAS6B,CAAc;AAGjD,oBAAAD,EAAM,UAAU,EAAE,QAAQE,KAAqB,OACtC;AAAA;AAAA,sBAEL,UAAU;AAAA,sBACV,MAAMD;AAAA,oBAAA,EAET;AAAA,kBACH;AAAA,gBACF;AAAA,cACF;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,QACA,SAAS;AAAA;AAAA,UAEP,OAAO;AAAA,YACL,GAAGjB;AAAA,UACL;AAAA,QACF;AAAA,MAAA;AAAA,IAEJ;AAAA,EAAA;AAEJ;"}
{
"name": "vite-plugin-node-polyfills",
"type": "module",
"version": "0.22.0",
"version": "0.23.0",
"description": "A Vite plugin to polyfill Node's Core Modules for browser environments.",

@@ -66,3 +66,3 @@ "author": "David Myers <hello@davidmyers.dev>",

"peerDependencies": {
"vite": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0"
"vite": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0"
},

@@ -89,3 +89,3 @@ "dependencies": {

"vitest": "^1.2.2",
"vite-plugin-node-polyfills": "0.22.0"
"vite-plugin-node-polyfills": "0.23.0"
},

@@ -92,0 +92,0 @@ "publishConfig": {