UNPKG

@troggy/vite-plugin-node-polyfills

Version:

A Vite plugin to polyfill Node's Core Modules for browser environments.

134 lines (133 loc) 5.52 kB
import { createRequire as h } from "node:module"; import _ from "@rollup/plugin-inject"; import B from "node-stdlib-browser"; import { handleCircularDependancyWarning as T } from "node-stdlib-browser/helpers/rollup/plugin"; import x from "node-stdlib-browser/helpers/esbuild/plugin"; const d = (s, l) => u(s) === u(l), e = (s, l) => s ? s === !0 ? !0 : s === l : !1, j = (s) => s.startsWith("node:"), O = (s) => { const l = s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); return new RegExp(`^${l}$`); }, u = (s) => s.replace(/^node:/, ""), g = { globalThis: [ "if (typeof globalThis === 'undefined') globalThis = window" ], buffer: [ "import __buffer_polyfill from '@troggy/vite-plugin-node-polyfills/shims/buffer'", "if (!globalThis.Buffer) globalThis.Buffer = __buffer_polyfill" ], global: [ "import __global_polyfill from '@troggy/vite-plugin-node-polyfills/shims/global'", "if (!globalThis.global) globalThis.global = __global_polyfill" ], process: [ "import __process_polyfill from '@troggy/vite-plugin-node-polyfills/shims/process'", "if (!globalThis.process) globalThis.process = __process_polyfill" ] }, E = (s = {}) => { const l = { include: [], exclude: [], overrides: {}, protocolImports: !0, ...s, globals: { Buffer: !0, global: !0, process: !0, ...s.globals } }, 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)) return "@troggy/vite-plugin-node-polyfills/shims/buffer"; if (e(l.globals.global, "dev") && /^global$/.test(o)) return "@troggy/vite-plugin-node-polyfills/shims/global"; if (e(l.globals.process, "dev") && /^process$/.test(o)) return "@troggy/vite-plugin-node-polyfills/shims/process"; if (o in l.overrides) return l.overrides[o]; }, b = Object.entries(B).reduce((o, [r, i]) => (!l.protocolImports && j(r) || m(r) || (o[r] = v(u(r)) || i), o), {}), f = h(import.meta.url), p = [ ...e(l.globals.Buffer, "dev") ? [f.resolve("@troggy/vite-plugin-node-polyfills/shims/buffer")] : [], ...e(l.globals.global, "dev") ? [f.resolve("@troggy/vite-plugin-node-polyfills/shims/global")] : [], ...e(l.globals.process, "dev") ? [f.resolve("@troggy/vite-plugin-node-polyfills/shims/process")] : [] ], a = [ ...g.globalThis, ...e(l.globals.Buffer, "dev") ? g.buffer : [], ...e(l.globals.global, "dev") ? g.global : [], ...e(l.globals.process, "dev") ? g.process : [], "" ].join(` `); return { name: "@troggy/vite-plugin-node-polyfills", config: (o, r) => { const i = r.command === "serve", c = { // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite ...e(l.globals.Buffer, "build") ? { Buffer: "@troggy/vite-plugin-node-polyfills/shims/buffer" } : {}, ...e(l.globals.global, "build") ? { global: "@troggy/vite-plugin-node-polyfills/shims/global" } : {}, ...e(l.globals.process, "build") ? { process: "@troggy/vite-plugin-node-polyfills/shims/process" } : {} }; return { build: { rollupOptions: { onwarn: (t, n) => { T(t, () => { if (o.build?.rollupOptions?.onwarn) return o.build.rollupOptions.onwarn(t, n); n(t); }); }, plugins: Object.keys(c).length > 0 ? [_(c)] : [] } }, esbuild: { // 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. banner: i ? a : void 0 }, optimizeDeps: { exclude: [ ...p ], esbuildOptions: { banner: i ? { js: a } : void 0, // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209 define: { ...i && e(l.globals.Buffer, "dev") ? { Buffer: "Buffer" } : {}, ...i && e(l.globals.global, "dev") ? { global: "global" } : {}, ...i && e(l.globals.process, "dev") ? { process: "process" } : {} }, inject: [ ...p ], plugins: [ x(b), // Supress the 'injected path "..." cannot be marked as external' error in Vite 4 (emitted by esbuild). // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1469 { name: "@troggy/vite-plugin-node-polyfills-shims-resolver", setup(t) { for (const n of p) { const y = O(n); t.onResolve({ filter: y }, () => ({ // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1468 external: !1, path: n })); } } } ] } }, resolve: { // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L150 alias: { ...b } } }; } }; }; export { E as nodePolyfills }; //# sourceMappingURL=index.js.map