Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion shims/buffer/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Blob,
BlobOptions,
Buffer,
Buffer as PolyfillBuffer,
File,
FileOptions,
INSPECT_MAX_BYTES,
Expand All @@ -20,6 +20,9 @@ import {
// eslint-disable-next-line unicorn/prefer-node-protocol
} from 'buffer'

// Prefer the host's real `globalThis.Buffer` when present
const Buffer = (typeof globalThis !== 'undefined' && (globalThis as { Buffer?: typeof PolyfillBuffer }).Buffer) || PolyfillBuffer

export {
Blob,
BlobOptions,
Expand Down
5 changes: 4 additions & 1 deletion shims/global/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// eslint-disable-next-line @typescript-eslint/no-invalid-this
const global = globalThis || this || self
const polyfillGlobal = globalThis || this || self

// Prefer the host's real `globalThis.global` when present
const global = (typeof globalThis !== 'undefined' && (globalThis as { global?: typeof polyfillGlobal }).global) || polyfillGlobal

export { global }
export default global
5 changes: 4 additions & 1 deletion shims/process/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// conflict with `node-stdlib-browser` which fails to import `process/browser.js`.
// https://github.com/yarnpkg/yarn/issues/6907
// eslint-disable-next-line unicorn/prefer-node-protocol
import process from 'process'
import polyfillProcess from 'process'

// Prefer the host's real `globalThis.process` when present
const process = (typeof globalThis !== 'undefined' && (globalThis as { process?: typeof polyfillProcess }).process) || polyfillProcess

export { process }
export default process
54 changes: 54 additions & 0 deletions test/integration/global-references/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ describe('import globals', () => {
Buffer.from("test");
`))
})

it('resolves to host globalThis.Buffer at runtime', async () => {
const { default: shimBuffer } = await import('vite-plugin-node-polyfills/shims/buffer')

expect(shimBuffer).toBe((globalThis as { Buffer?: typeof Buffer }).Buffer)
})

it('mutations to globalThis.Buffer are observed by the shim', async () => {
const { default: shimBuffer } = await import('vite-plugin-node-polyfills/shims/buffer') as { default: Record<string, unknown> }
const key = `__soft_fallback_test_${Date.now()}__`

try {
;(globalThis as Record<string, Record<string, unknown>>).Buffer[key] = 'set-on-host'
expect(shimBuffer[key]).toEqual('set-on-host')
} finally {
delete (globalThis as Record<string, Record<string, unknown>>).Buffer[key]
}
})
})

describe('global', () => {
Expand Down Expand Up @@ -56,6 +74,24 @@ describe('import globals', () => {
console.log(global);
`))
})

it('resolves to host globalThis.global at runtime', async () => {
const { default: shimGlobal } = await import('vite-plugin-node-polyfills/shims/global')

expect(shimGlobal).toBe((globalThis as { global?: typeof globalThis }).global)
})

it('mutations to globalThis.global are observed by the shim', async () => {
const { default: shimGlobal } = await import('vite-plugin-node-polyfills/shims/global') as { default: Record<string, unknown> }
const key = `__soft_fallback_test_${Date.now()}__`

try {
;(globalThis as Record<string, Record<string, unknown>>).global[key] = 'set-on-host'
expect(shimGlobal[key]).toEqual('set-on-host')
} finally {
delete (globalThis as Record<string, Record<string, unknown>>).global[key]
}
})
})

describe('process', () => {
Expand Down Expand Up @@ -84,5 +120,23 @@ describe('import globals', () => {
console.log(process);
`))
})

it('resolves to host globalThis.process at runtime', async () => {
const { default: shimProcess } = await import('vite-plugin-node-polyfills/shims/process')

expect(shimProcess).toBe(globalThis.process)
})

it('mutations to globalThis.process are observed by the shim', async () => {
const { default: shimProcess } = await import('vite-plugin-node-polyfills/shims/process') as { default: Record<string, unknown> }
const key = `__soft_fallback_test_${Date.now()}__`

try {
;(globalThis.process as Record<string, unknown>)[key] = 'set-on-host'
expect(shimProcess[key]).toEqual('set-on-host')
} finally {
delete (globalThis.process as Record<string, unknown>)[key]
}
})
})
})