Skip to content

Commit b158876

Browse files
committed
Use local shims for globals (and update buffer polyfill)
1 parent 91c0052 commit b158876

File tree

7 files changed

+77
-7
lines changed

7 files changed

+77
-7
lines changed

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"keywords": [
1414
"node",
1515
"node-core-modules",
16+
"node-polyfills",
1617
"node-stdlib",
1718
"polyfills",
1819
"vite",
@@ -27,10 +28,14 @@
2728
"require": "./dist/index.cjs",
2829
"import": "./dist/index.js",
2930
"types": "./dist/index.d.ts"
30-
}
31+
},
32+
"./shims": "./shims/dist/index.js"
3133
},
3234
"scripts": {
33-
"build": "vite build && tsc",
35+
"build": "run-s build:core build:shims build:types",
36+
"build:core": "vite build",
37+
"build:shims": "vite build ./shims",
38+
"build:types": "tsc",
3439
"test": "run-s test:build test:dev",
3540
"test:build": "vite -c test/vite.config.ts build",
3641
"test:dev": "vite -c test/vite.config.ts"
@@ -55,7 +60,9 @@
5560
},
5661
"dependencies": {
5762
"@rollup/plugin-inject": "^5.0.3",
58-
"node-stdlib-browser": "^1.2.0"
63+
"buffer-polyfill": "npm:buffer@^6.0.3",
64+
"node-stdlib-browser": "^1.2.0",
65+
"process-polyfill": "npm:process@^0.11.10"
5966
},
6067
"peerDependencies": {
6168
"vite": "^2.0.0 || ^3.0.0 || ^4.0.0"

pnpm-lock.yaml

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

shims/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const global = globalThis || this || self
2+
3+
export { Buffer } from 'buffer-polyfill'
4+
export { default as process } from 'process-polyfill'
5+
export { global }

shims/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "module",
3+
"sideEffects": false,
4+
"exports": {
5+
".": "./dist/index.js"
6+
},
7+
"main": "./dist/index.js",
8+
"module": "./dist/index.js"
9+
}

shims/vite.config.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { defineConfig } from 'vite'
2+
import { externalizeDeps } from 'vite-plugin-externalize-deps'
3+
4+
// https://vitejs.dev/config/
5+
export default defineConfig({
6+
build: {
7+
lib: {
8+
entry: './index.ts',
9+
fileName: 'index',
10+
},
11+
rollupOptions: {
12+
external: [/^node:.*$/],
13+
output: [
14+
{
15+
esModule: true,
16+
exports: 'named',
17+
format: 'es',
18+
},
19+
],
20+
},
21+
sourcemap: true,
22+
target: 'esnext',
23+
},
24+
plugins: [
25+
externalizeDeps(),
26+
],
27+
})

src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export type PolyfillOptionsResolved = {
5757
protocolImports: boolean,
5858
}
5959

60+
const globals = ['buffer', 'global', 'process'].flatMap(name => [name, `node:${name}`])
61+
6062
const isBuildEnabled = (value: BooleanOrBuildTarget) => {
6163
if (!value) return false
6264
if (value === true) return true
@@ -105,7 +107,7 @@ const isProtocolImport = (name: string) => {
105107
*/
106108
export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
107109
const require = createRequire(import.meta.url)
108-
const globalShimsPath = require.resolve('node-stdlib-browser/helpers/esbuild/shim')
110+
const globalShimsPath = require.resolve('vite-plugin-node-polyfills/shims')
109111
const optionsResolved: PolyfillOptionsResolved = {
110112
exclude: [],
111113
protocolImports: true,
@@ -135,7 +137,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
135137
}
136138

137139
if (!isExcluded(name)) {
138-
included[name] = value
140+
included[name] = globals.includes(name) ? globalShimsPath : value
139141
}
140142

141143
return included
@@ -179,10 +181,10 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
179181
setup(build) {
180182
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
181183
const escapedGlobalShimsPath = globalShimsPath.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
182-
const filter = new RegExp(`^${escapedGlobalShimsPath}$`)
184+
const globalShimsFilter = new RegExp(`^${escapedGlobalShimsPath}$`)
183185

184186
// https://esbuild.github.io/plugins/#on-resolve
185-
build.onResolve({ filter }, () => {
187+
build.onResolve({ filter: globalShimsFilter }, () => {
186188
return {
187189
// https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1468
188190
external: false,

test/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ console.log(resolve('.'))
1212
console.log(process)
1313
console.log(process.env)
1414
console.log(globalThis.Array)
15+
console.log(Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]).readBigUInt64BE(0))
1516
console.log(Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]))
1617
console.log(Array)
1718
console.log(fs)

0 commit comments

Comments
 (0)