Skip to content

Commit abf35b8

Browse files
committed
Make sure globals are properly shimmed for esbuild transforms
1 parent a2bfc25 commit abf35b8

File tree

9 files changed

+173
-6
lines changed

9 files changed

+173
-6
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@tanstack/react-query-devtools": "^4.29.12",
4747
"@types/node": "^18.7.23",
4848
"@vitejs/plugin-react": "^4.0.0",
49+
"@vitejs/plugin-vue": "^4.2.3",
4950
"esbuild": "^0.16.16",
5051
"npm-run-all": "^4.1.5",
5152
"ohmyfetch": "^0.4.20",
@@ -56,7 +57,8 @@
5657
"vite": "^4.0.4",
5758
"vite-plugin-externalize-deps": "^0.1.5",
5859
"vite-plugin-inspect": "^0.6.0",
59-
"vite-plugin-node-polyfills": "workspace:*"
60+
"vite-plugin-node-polyfills": "workspace:*",
61+
"vue": "^3.3.4"
6062
},
6163
"dependencies": {
6264
"@rollup/plugin-inject": "^5.0.3",

pnpm-lock.yaml

Lines changed: 125 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
161161
],
162162
},
163163
},
164+
esbuild: {
165+
// 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.
166+
banner: [
167+
isDevEnabled(optionsResolved.globals.Buffer) ? `import { Buffer as BufferPolyfill } from '${globalShimsPath}'\nwindow.Buffer = BufferPolyfill` : '',
168+
isDevEnabled(optionsResolved.globals.global) ? `import { global as globalPolyfill } from '${globalShimsPath}'\nwindow.global = globalPolyfill` : '',
169+
isDevEnabled(optionsResolved.globals.process) ? `import { process as processPolyfill } from '${globalShimsPath}'\nwindow.process = processPolyfill` : '',
170+
].join('\n'),
171+
},
164172
optimizeDeps: {
165173
esbuildOptions: {
166174
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209
@@ -177,6 +185,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
177185
// Supress the 'injected path "..." cannot be marked as external' error in Vite 4 (emitted by esbuild).
178186
// https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1469
179187
{
188+
180189
name: 'vite-plugin-node-polyfills-shims-resolver',
181190
setup(build) {
182191
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping

test/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
<title>Example</title>
77
</head>
88
<body>
9-
<div id="app"></div>
9+
<div id="react-app"></div>
10+
<div id="vue-app"></div>
1011
<script type="module" src="/src/main.ts"></script>
1112
</body>
1213
</html>

test/src/app.vue

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script lang="ts">
2+
import { defineComponent, ref } from 'vue'
3+
4+
export default defineComponent({
5+
setup() {
6+
const msg = ref(Buffer.from(Buffer.from('Hello, friend!').toString('base64'), 'base64').toString('ascii'))
7+
// const msg = ref('')
8+
9+
return {
10+
msg,
11+
}
12+
},
13+
})
14+
</script>
15+
16+
<template>
17+
<div>{{ msg }}</div>
18+
</template>

test/src/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { resolve } from 'node:path'
33
import * as process from 'node:process'
44
import fs from 'node:fs'
55
import { fetch } from 'ohmyfetch'
6-
import { react } from './react'
6+
// import { react } from './react'
7+
import { vue } from './vue'
78

8-
react()
9+
// react()
10+
vue()
911

1012
console.log(fetch)
1113
console.log(resolve('.'))

test/src/react.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function App() {
3333
}
3434

3535
export const react = () => {
36-
ReactDOM.createRoot(document.getElementById('app') as HTMLElement).render(
36+
ReactDOM.createRoot(document.getElementById('react-app') as HTMLElement).render(
3737
<React.StrictMode>
3838
<QueryClientProvider client={queryClient}>
3939
<App />

test/src/vue.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createApp } from 'vue'
2+
import App from './app.vue'
3+
4+
export const vue = () => {
5+
const app = createApp(App)
6+
7+
app.mount('#vue-app')
8+
}

test/vite.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { resolve } from 'node:path'
22
import react from '@vitejs/plugin-react'
3+
import vue from '@vitejs/plugin-vue'
34
import { defineConfig } from 'vite'
45
import { nodePolyfills } from '../src/index'
56

67
// https://vitejs.dev/config/
78
export default defineConfig({
89
plugins: [
9-
react(),
10+
// react(),
11+
vue(),
1012
nodePolyfills({
1113
exclude: ['fs'],
1214
globals: {

0 commit comments

Comments
 (0)