{"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 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 globalShimsBanner = [\n `import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'`,\n `import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'`,\n `import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'`,\n ``,\n `globalThis.Buffer = globalThis.Buffer || __buffer_polyfill`,\n `globalThis.global = globalThis.global || __global_polyfill`,\n `globalThis.process = globalThis.process || __process_polyfill`,\n ``,\n].join('\\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 require = createRequire(import.meta.url)\n const globalShimPaths = [\n require.resolve('vite-plugin-node-polyfills/shims/buffer'),\n require.resolve('vite-plugin-node-polyfills/shims/global'),\n require.resolve('vite-plugin-node-polyfills/shims/process'),\n ]\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>((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)\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","globalShimsBanner","nodePolyfills","options","require","createRequire","globalShimPaths","optionsResolved","isExcluded","moduleName","includedName","excludedName","toOverride","polyfills","stdLibBrowser","included","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,IAAW,CAACC,MAAiB;AAExC,QAAMC,IAAcD,EAAK,QAAQ,uBAAuB,MAAM;AAE9D,SAAO,IAAI,OAAO,IAAIC,CAAW,GAAG;AACtC,GAEaR,IAAsB,CAACK,MAC3BA,EAAK,QAAQ,UAAU,EAAE,GCgE5BI,IAAoB;AAAA,EACxB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,EAAE,KAAK;AAAA,CAAI,GA8BEC,IAAgB,CAACC,IAA2B,OAAe;AAChE,QAAAC,IAAUC,EAAc,YAAY,GAAG,GACvCC,IAAkB;AAAA,IACtBF,EAAQ,QAAQ,yCAAyC;AAAA,IACzDA,EAAQ,QAAQ,yCAAyC;AAAA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