Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 #83

Open
rdsedmundo opened this issue Mar 19, 2024 · 2 comments · May be fixed by #100

Comments

@rdsedmundo
Copy link

rdsedmundo commented Mar 19, 2024

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
Copy link

jwnx commented Mar 20, 2024

Maybe dupe of #71 ?

@sa3dany
Copy link

sa3dany commented May 20, 2024

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

80avin-sw added a commit to 80avin-sw/vite-plugin-node-polyfills that referenced this issue Jul 24, 2024
@80avin-sw 80avin-sw linked a pull request Jul 24, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants