Skip to content

Commit

Permalink
Define globals conditionally
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilyat1337 authored and davidmyersdev committed May 19, 2024
1 parent b61a5bb commit a1bc855
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 49 deletions.
44 changes: 28 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,20 @@ export type PolyfillOptionsResolved = {
protocolImports: boolean,
}

const globalShimsBanner = [
`import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'`,
`import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'`,
`import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'`,
``,
`globalThis.Buffer = globalThis.Buffer || __buffer_polyfill`,
`globalThis.global = globalThis.global || __global_polyfill`,
`globalThis.process = globalThis.process || __process_polyfill`,
``,
].join('\n')
const globalShimBanners = {
buffer: [
`import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'`,
`globalThis.Buffer = globalThis.Buffer || __buffer_polyfill`,
],
global: [
`import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'`,
`globalThis.global = globalThis.global || __global_polyfill`,
],
process: [
`import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'`,
`globalThis.process = globalThis.process || __process_polyfill`,
],
}

/**
* Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.
Expand Down Expand Up @@ -127,12 +131,6 @@ const globalShimsBanner = [
* ```
*/
export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
const require = createRequire(import.meta.url)
const globalShimPaths = [
require.resolve('vite-plugin-node-polyfills/shims/buffer'),
require.resolve('vite-plugin-node-polyfills/shims/global'),
require.resolve('vite-plugin-node-polyfills/shims/process'),
]
const optionsResolved: PolyfillOptionsResolved = {
include: [],
exclude: [],
Expand Down Expand Up @@ -187,6 +185,20 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
return included
}, {} as Record<ModuleName, string>)

const require = createRequire(import.meta.url)
const globalShimPaths = [
...((isEnabled(optionsResolved.globals.Buffer, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/buffer')] : []),
...((isEnabled(optionsResolved.globals.global, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/global')] : []),
...((isEnabled(optionsResolved.globals.process, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/process')] : []),
]

const globalShimsBanner = [
...((isEnabled(optionsResolved.globals.Buffer, 'dev')) ? globalShimBanners.buffer : []),
...((isEnabled(optionsResolved.globals.global, 'dev')) ? globalShimBanners.global : []),
...((isEnabled(optionsResolved.globals.process, 'dev')) ? globalShimBanners.process : []),
``,
].join('\n')

return {
name: 'vite-plugin-node-polyfills',
config: (config, env) => {
Expand Down
60 changes: 54 additions & 6 deletions test/integration/global-references/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,28 @@ describe('import globals', () => {

expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
Buffer.from("test");
`))
})

it('injects Buffer only', async () => {
const result = await transformDev(`Buffer.from('test')`, {
globals: {
Buffer: true,
global: false,
process: false,
},
})

expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
globalThis.global = globalThis.global || __global_polyfill
globalThis.process = globalThis.process || __process_polyfill
Buffer.from("test");
`))
Expand All @@ -26,12 +42,28 @@ describe('import globals', () => {

expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
console.log(global);
`))
})

it('injects global only', async () => {
const result = await transformDev(`console.log(global)`, {
globals: {
Buffer: false,
global: true,
process: false,
},
})

expect(result?.code).toEqual(formatWhitespace(`
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
globalThis.process = globalThis.process || __process_polyfill
console.log(global);
`))
Expand All @@ -44,11 +76,27 @@ describe('import globals', () => {

expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
globalThis.global = globalThis.global || __global_polyfill
console.log(process);
`))
})

it('injects process only', async () => {
const result = await transformDev(`console.log(process)`, {
globals: {
Buffer: false,
global: false,
process: true,
},
})

expect(result?.code).toEqual(formatWhitespace(`
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
console.log(process);
Expand Down
40 changes: 16 additions & 24 deletions test/integration/import-globals/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ describe('import globals', () => {

expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
import __global_polyfill from "/shims/global/dist/index.js"
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
import Buffer from "/shims/buffer/dist/index.js";
Expand All @@ -31,11 +30,10 @@ describe('import globals', () => {

expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
import __global_polyfill from "/shims/global/dist/index.js"
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
import Buffer from "/shims/buffer/dist/index.js";
Expand All @@ -51,11 +49,10 @@ describe('import globals', () => {

expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
import __global_polyfill from "/shims/global/dist/index.js"
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
import Buffer from "/shims/buffer/dist/index.js";
Expand All @@ -71,11 +68,10 @@ describe('import globals', () => {

expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
import __global_polyfill from "/shims/global/dist/index.js"
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
import Buffer from "/shims/buffer/dist/index.js";
Expand All @@ -93,11 +89,10 @@ describe('import globals', () => {

expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
import __global_polyfill from "/shims/global/dist/index.js"
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
import process from "/shims/process/dist/index.js";
Expand All @@ -113,11 +108,10 @@ describe('import globals', () => {

expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
import __global_polyfill from "/shims/global/dist/index.js"
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
import process from "/shims/process/dist/index.js";
Expand All @@ -133,11 +127,10 @@ describe('import globals', () => {

expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
import __global_polyfill from "/shims/global/dist/index.js"
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
import process from "/shims/process/dist/index.js";
Expand All @@ -153,11 +146,10 @@ describe('import globals', () => {

expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
import __global_polyfill from "/shims/global/dist/index.js"
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
import process from "/shims/process/dist/index.js";
Expand Down
6 changes: 3 additions & 3 deletions test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createServer } from 'vite'
import { nodePolyfills } from 'vite-plugin-node-polyfills'
import { nodePolyfills, PolyfillOptions } from 'vite-plugin-node-polyfills'

/**
* Format code by removing the smallest indentation from each line.
Expand All @@ -25,14 +25,14 @@ export const formatWhitespace = (code: string) => {
return `${formatted}\n`
}

export const transformDev = async (code: string) => {
export const transformDev = async (code: string, options?: PolyfillOptions) => {
const server = await createServer({
configFile: false,
esbuild: {
format: 'esm',
},
plugins: [
nodePolyfills(),
nodePolyfills(options),
{
name: 'test-code-loader',
load(id) {
Expand Down

0 comments on commit a1bc855

Please sign in to comment.