Skip to content
Closed
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ jobs:
- 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
jq '.pnpm.overrides.vite = "8.0.1" | .pnpm.overrides.vitest = "^4.1.2"' package.json > package.tmp.json
mv package.tmp.json package.json
pnpm i --no-frozen-lockfile
- run: pnpm -r test:e2e
- run: pnpm -r test
test-unit:
runs-on: ubuntu-latest
steps:
Expand Down
39 changes: 27 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ const globalShimBanners = {
],
}

const isModuleName = (name: string): name is ModuleName => {
return name in stdLibBrowser
}

/**
* Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.
*
Expand Down Expand Up @@ -194,6 +198,27 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
...((isEnabled(optionsResolved.globals.process, 'dev')) ? [require.resolve('vite-plugin-node-polyfills/shims/process')] : []),
]

const resolvePlugin: Plugin = {
name: 'vite-plugin-node-polyfills:resolve',
enforce: 'pre',
resolveId: {
// @ts-expect-error -- hook filters are only supported in Vite 6.3.0+
filter: { id: new RegExp(Object.keys(stdLibBrowser).map((name) => `^${name}\/?$`).join('|')) },
async handler(source, importer, opts) {
if (!optionsResolved.protocolImports && isNodeProtocolImport(source)) {
return
}
source = source.endsWith('/') ? source.slice(0, -1) : source // strip trailing slash
if (!isModuleName(source) || isExcluded(source)) {
return
}
const aliased = toOverride(withoutNodeProtocol(source)) || stdLibBrowser[source]
const resolved = await this.resolve(aliased, importer, { skipSelf: true, ...opts })
return resolved
},
},
}

const globalShimsBanner = [
...((isEnabled(optionsResolved.globals.Buffer, 'dev')) ? globalShimBanners.buffer : []),
...((isEnabled(optionsResolved.globals.global, 'dev')) ? globalShimBanners.global : []),
Expand Down Expand Up @@ -273,16 +298,11 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
...isRolldownVite
? {
rolldownOptions: {
resolve: {
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L150
alias: {
...polyfills,
},
},
transform: {
define: defines,
},
plugins: [
resolvePlugin,
{
name: 'vite-plugin-node-polyfills:optimizer',
banner: isDev ? globalShimsBanner : undefined,
Expand Down Expand Up @@ -322,16 +342,11 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
},
},
},
resolve: {
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L150
alias: {
...polyfills,
},
},
}
},
}
return [
resolvePlugin,
injectPlugin,
plugin,
]
Expand Down
Loading