davidmyersdev/vite-plugin-node-polyfills

`process` is not polyfilled if `viteConfig.define` is used to define, for example, a static environment variable in the form of `process.env.var_name`

rdsedmundo opened this issue · 2 comments

Problem

  • I'm using Sanity Studio V3 and they define a process.env.MODE variable using Vite's define config.
  • This causes the process polyfill module to not be applied, and instead process becomes simply { env: { MODE: value } }, due to this OR condition here:
globalThis.process = globalThis.process || __process_polyfill

Solution
We should probably merge the objects somehow, or have a separate option that would allow this.

jwnx commented

Maybe dupe of #71 ?

If anyone is facing this, In our app we are in the process of migrating from create-react-app to Vite and I created this package patch (it's for v0.21.0 only) as an interim solution.

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