Skip to content
Merged
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
27 changes: 27 additions & 0 deletions .github/actions/override-dependency/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Override dependency
description: Override the pinned version of a specified dependency
inputs:
name:
description: The name of the dependency to override.
required: true
version:
description: The version of the dependency to install.
required: true
runs:
using: composite
steps:
- name: Print package.json file
shell: bash
run: cat package.json
- name: Generate package.overrides.json file
shell: bash
run: jq '.pnpm.overrides["${{ inputs.name }}"] = "${{ inputs.version }}"' package.json > package.overrides.json
- name: Replace package.json with package.overrides.json
shell: bash
run: mv package.overrides.json package.json
- name: Print package.json file
shell: bash
run: cat package.json
- name: Install overrides
shell: bash
run: pnpm install --no-frozen-lockfile
31 changes: 24 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches:
- main
workflow_dispatch:
permissions:
contents: none
jobs:
lint:
runs-on: ubuntu-latest
Expand All @@ -14,7 +16,7 @@ jobs:
- name: Run the linter
run: pnpm lint
test-e2e:
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/install-dependencies
Expand All @@ -23,18 +25,17 @@ jobs:
- run: pnpm playwright install --with-deps
- run: pnpm -r test:e2e
test-e2e-vite-8:
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/install-dependencies
- run: pnpm build:core
- run: pnpm build:shims
- run: pnpm playwright install --with-deps
- name: update overrides to use vite 8 and re-install
run: |
jq '.pnpm.overrides.vite = "8.0.1"' package.json > package.tmp.json
mv package.tmp.json package.json
pnpm i --no-frozen-lockfile
- uses: ./.github/actions/override-dependency
with:
name: vite
version: 8.0.1
- run: pnpm -r test:e2e
test-unit:
runs-on: ubuntu-latest
Expand All @@ -44,6 +45,22 @@ jobs:
- run: pnpm build:core
- run: pnpm build:shims
- run: pnpm -r test
test-unit-vite-8:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/install-dependencies
- run: pnpm build:core
- run: pnpm build:shims
- uses: ./.github/actions/override-dependency
with:
name: vite
version: 8.0.1
- uses: ./.github/actions/override-dependency
with:
name: vitest
version: ^4.1.2
- run: pnpm -r test
typecheck:
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"node-stdlib-browser": "^1.3.1"
},
"devDependencies": {
"@playwright/test": "^1.40.1",
"@playwright/test": "^1.60.0",
"@types/node": "^18.18.8",
"buffer": "6.0.3",
"esbuild": "^0.19.8",
Expand Down
40 changes: 20 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
type BooleanOrBuildTarget,
type BuildTarget,
isEnabled,
} from './utils'

export type GlobalName = typeof globals[number]

export const globals = [
'buffer',
'global',
'process',
] as const

export const getGlobalsToHandle = (options: {
globals: { [Name in GlobalName]: BooleanOrBuildTarget },
target: BuildTarget,
}): GlobalName[] => {
return globals.filter((global) => {
const value = options.globals[global]

return isEnabled(value, options.target)
})
}
55 changes: 30 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ import stdLibBrowser from 'node-stdlib-browser'
import { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin'
import esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'
import type { Plugin } from 'vite'
import { compareModuleNames, isEnabled, isNodeProtocolImport, toRegExp, withoutNodeProtocol } from './utils'

type TransformHook = Extract<Plugin['transform'], Function>

export type BuildTarget = 'build' | 'dev'
export type BooleanOrBuildTarget = boolean | BuildTarget
export type ModuleName = keyof typeof stdLibBrowser
export type ModuleNameWithoutNodePrefix<T = ModuleName> = T extends `node:${infer P}` ? P : never
import { getModulesToPolyfill } from './modules'
import { buildTrailingSlashNormalizer } from './plugins'
import {
type BooleanOrBuildTarget,
type ModuleName,
type ModuleNameWithoutNodePrefix,
type TransformHook,
compareModuleNames,
globalShimBanners,
isEnabled,
isNodeProtocolImport,
toRegExp,
withoutNodeProtocol,
} from './utils'

export type PolyfillOptions = {
/**
Expand Down Expand Up @@ -89,21 +95,6 @@ export type PolyfillOptionsResolved = {
protocolImports: boolean,
}

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 @@ -147,6 +138,16 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
},
}

const modulesToPolyfill = getModulesToPolyfill({
modulesToExclude: optionsResolved.exclude,
modulesToInclude: optionsResolved.include,
})

const trailingSlashNormalizer = buildTrailingSlashNormalizer({
modules: modulesToPolyfill,
protocolImports: optionsResolved.protocolImports,
})

const isExcluded = (moduleName: ModuleName) => {
if (optionsResolved.include.length > 0) {
return !optionsResolved.include.some((includedName) => compareModuleNames(moduleName, includedName))
Expand Down Expand Up @@ -220,9 +221,11 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
const plugin: Plugin = {
name: 'vite-plugin-node-polyfills',
config(config, env) {
const isBuild = env.command === 'build'
const isDev = env.command === 'serve'
// @ts-expect-error - this.meta.rolldownVersion only exists with rolldown-vite 7+
const isRolldownVite = !!this?.meta?.rolldownVersion
const isNativeInjectAvailable = isBuild && isRolldownVite

// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209
const defines = {
Expand All @@ -238,7 +241,6 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
...(isEnabled(optionsResolved.globals.process, 'build') ? { process: 'vite-plugin-node-polyfills/shims/process' } : {}),
}

const isNativeInjectAvailable = env.command === 'build' && isRolldownVite
rawInjectPlugin = (Object.keys(shimsToInject).length > 0 && !isNativeInjectAvailable) ? inject(shimsToInject) as Plugin : false
if (rawInjectPlugin === false) {
delete injectPlugin.transform
Expand Down Expand Up @@ -283,6 +285,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
define: defines,
},
plugins: [
trailingSlashNormalizer,
{
name: 'vite-plugin-node-polyfills:optimizer',
banner: isDev ? globalShimsBanner : undefined,
Expand Down Expand Up @@ -331,8 +334,10 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
}
},
}

return [
trailingSlashNormalizer,
injectPlugin,
plugin,
]
].flat()
}
Loading
Loading