Skip to content

Commit 94a3ae4

Browse files
committed
Inject imports for globals instead of an iife
1 parent c72c938 commit 94a3ae4

File tree

6 files changed

+12
-40
lines changed

6 files changed

+12
-40
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ jobs:
2828
- uses: ./.github/actions/install-dependencies
2929
- run: pnpm build:core
3030
- run: pnpm build:shims
31-
- run: pnpm build:banner
3231
- run: pnpm playwright install --with-deps
3332
- run: pnpm -r test
3433
test-unit:
@@ -40,7 +39,6 @@ jobs:
4039
- uses: ./.github/actions/install-dependencies
4140
- run: pnpm build:core
4241
- run: pnpm build:shims
43-
- run: pnpm build:banner
4442
- run: pnpm test:build
4543
typecheck:
4644
needs:
@@ -51,5 +49,4 @@ jobs:
5149
- uses: ./.github/actions/install-dependencies
5250
- run: pnpm build:core
5351
- run: pnpm build:shims
54-
- run: pnpm build:banner
5552
- run: pnpm typecheck

package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
"require": "./shims/dist/index.cjs",
3232
"import": "./shims/dist/index.js"
3333
},
34-
"./shims/banner": {
35-
"require": "./shims/banner/dist/index.cjs",
36-
"import": "./shims/banner/dist/index.js"
37-
},
3834
"./shims/buffer": {
3935
"require": "./shims/buffer/dist/index.cjs",
4036
"import": "./shims/buffer/dist/index.js"
@@ -56,8 +52,7 @@
5652
"shims"
5753
],
5854
"scripts": {
59-
"build": "run-s build:core build:shims build:banner build:types",
60-
"build:banner": "vite-node ./shims/banner/build.ts",
55+
"build": "run-s build:core build:shims build:types",
6156
"build:core": "vite build",
6257
"build:shims": "run-p build:shims:*",
6358
"build:shims:buffer": "vite build -c ./shims/vite.config.ts ./shims/buffer",

shims/banner/build.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

shims/banner/index.cjs

Lines changed: 0 additions & 4 deletions
This file was deleted.

shims/banner/package.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { readFileSync } from 'node:fs'
21
import { createRequire } from 'node:module'
32
import inject from '@rollup/plugin-inject'
43
import stdLibBrowser from 'node-stdlib-browser'
@@ -88,6 +87,17 @@ export type PolyfillOptionsResolved = {
8887
protocolImports: boolean,
8988
}
9089

90+
const globalShimsBanner = [
91+
`import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'`,
92+
`import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'`,
93+
`import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'`,
94+
``,
95+
`globalThis.Buffer = globalThis.Buffer || __buffer_polyfill`,
96+
`globalThis.global = globalThis.global || __global_polyfill`,
97+
`globalThis.process = globalThis.process || __process_polyfill`,
98+
``,
99+
].join('\n')
100+
91101
/**
92102
* Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.
93103
*
@@ -123,8 +133,6 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
123133
require.resolve('vite-plugin-node-polyfills/shims/global'),
124134
require.resolve('vite-plugin-node-polyfills/shims/process'),
125135
]
126-
const globalShimsBannerPath = require.resolve('vite-plugin-node-polyfills/shims/banner')
127-
const globalShimsBanner = readFileSync(globalShimsBannerPath, 'utf-8')
128136
const optionsResolved: PolyfillOptionsResolved = {
129137
include: [],
130138
exclude: [],
@@ -232,7 +240,6 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
232240
// Supress the 'injected path "..." cannot be marked as external' error in Vite 4 (emitted by esbuild).
233241
// https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1469
234242
{
235-
236243
name: 'vite-plugin-node-polyfills-shims-resolver',
237244
setup(build) {
238245
for (const globalShimPath of globalShimPaths) {

0 commit comments

Comments
 (0)