Fix __publicField with vite define + globalThis polyfill

The previous index.html polyfill set window.__publicField but Vite's
dep optimizer pre-bundles in Node.js where that global is never set,
so the error persisted.

Two-part fix:
- vite.config.ts define: rewrite bare __publicField identifiers to
  globalThis.__publicField (a valid entity name esbuild accepts)
- index.html: set globalThis.__publicField using the exact
  Object.defineProperty signature esbuild expects, before modules run

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Daniel Pupius
2026-03-15 23:57:28 +00:00
parent a7eb73c558
commit 38798de9ca
2 changed files with 13 additions and 1 deletions

View File

@@ -15,7 +15,13 @@
<body>
<div id="root"></div>
<script>window.__publicField = function(a, b, c) { a[b] = c; return c; };</script>
<script>
globalThis.__publicField = function(obj, key, value) {
Object.defineProperty(obj, typeof key !== 'symbol' ? key + '' : key, {
configurable: true, enumerable: true, writable: true, value: value
});
};
</script>
<script type="module" src="/src/main.tsx"></script>
</body>

View File

@@ -5,6 +5,12 @@ import { resolve } from 'path';
// https://vitejs.dev/config/
export default defineConfig({
define: {
// Some pre-compiled npm packages use __publicField (a TypeScript/esbuild helper)
// without bundling it. Rewrite bare references to a globalThis property that
// we polyfill in index.html before any module code runs.
'__publicField': 'globalThis.__publicField',
},
plugins: [
react(),
TanStackRouterVite(),