Skip to content

Commit b1609a7

Browse files
Normalize trailing slashes (#156)
2 parents 780986c + 3c24819 commit b1609a7

10 files changed

Lines changed: 304 additions & 56 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Override dependency
2+
description: Override the pinned version of a specified dependency
3+
inputs:
4+
name:
5+
description: The name of the dependency to override.
6+
required: true
7+
version:
8+
description: The version of the dependency to install.
9+
required: true
10+
runs:
11+
using: composite
12+
steps:
13+
- name: Print package.json file
14+
shell: bash
15+
run: cat package.json
16+
- name: Generate package.overrides.json file
17+
shell: bash
18+
run: jq '.pnpm.overrides["${{ inputs.name }}"] = "${{ inputs.version }}"' package.json > package.overrides.json
19+
- name: Replace package.json with package.overrides.json
20+
shell: bash
21+
run: mv package.overrides.json package.json
22+
- name: Print package.json file
23+
shell: bash
24+
run: cat package.json
25+
- name: Install overrides
26+
shell: bash
27+
run: pnpm install --no-frozen-lockfile

.github/workflows/ci.yml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ on:
55
branches:
66
- main
77
workflow_dispatch:
8+
permissions:
9+
contents: none
810
jobs:
911
lint:
1012
runs-on: ubuntu-latest
@@ -14,7 +16,7 @@ jobs:
1416
- name: Run the linter
1517
run: pnpm lint
1618
test-e2e:
17-
runs-on: ubuntu-22.04
19+
runs-on: ubuntu-latest
1820
steps:
1921
- uses: actions/checkout@v4
2022
- uses: ./.github/actions/install-dependencies
@@ -23,18 +25,17 @@ jobs:
2325
- run: pnpm playwright install --with-deps
2426
- run: pnpm -r test:e2e
2527
test-e2e-vite-8:
26-
runs-on: ubuntu-22.04
28+
runs-on: ubuntu-latest
2729
steps:
2830
- uses: actions/checkout@v4
2931
- uses: ./.github/actions/install-dependencies
3032
- run: pnpm build:core
3133
- run: pnpm build:shims
3234
- run: pnpm playwright install --with-deps
33-
- name: update overrides to use vite 8 and re-install
34-
run: |
35-
jq '.pnpm.overrides.vite = "8.0.1"' package.json > package.tmp.json
36-
mv package.tmp.json package.json
37-
pnpm i --no-frozen-lockfile
35+
- uses: ./.github/actions/override-dependency
36+
with:
37+
name: vite
38+
version: 8.0.1
3839
- run: pnpm -r test:e2e
3940
test-unit:
4041
runs-on: ubuntu-latest
@@ -44,6 +45,22 @@ jobs:
4445
- run: pnpm build:core
4546
- run: pnpm build:shims
4647
- run: pnpm -r test
48+
test-unit-vite-8:
49+
runs-on: ubuntu-latest
50+
steps:
51+
- uses: actions/checkout@v4
52+
- uses: ./.github/actions/install-dependencies
53+
- run: pnpm build:core
54+
- run: pnpm build:shims
55+
- uses: ./.github/actions/override-dependency
56+
with:
57+
name: vite
58+
version: 8.0.1
59+
- uses: ./.github/actions/override-dependency
60+
with:
61+
name: vitest
62+
version: ^4.1.2
63+
- run: pnpm -r test
4764
typecheck:
4865
runs-on: ubuntu-latest
4966
steps:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"node-stdlib-browser": "^1.3.1"
9999
},
100100
"devDependencies": {
101-
"@playwright/test": "^1.40.1",
101+
"@playwright/test": "^1.60.0",
102102
"@types/node": "^18.18.8",
103103
"buffer": "6.0.3",
104104
"esbuild": "^0.19.8",

pnpm-lock.yaml

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

src/globals.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {
2+
type BooleanOrBuildTarget,
3+
type BuildTarget,
4+
isEnabled,
5+
} from './utils'
6+
7+
export type GlobalName = typeof globals[number]
8+
9+
export const globals = [
10+
'buffer',
11+
'global',
12+
'process',
13+
] as const
14+
15+
export const getGlobalsToHandle = (options: {
16+
globals: { [Name in GlobalName]: BooleanOrBuildTarget },
17+
target: BuildTarget,
18+
}): GlobalName[] => {
19+
return globals.filter((global) => {
20+
const value = options.globals[global]
21+
22+
return isEnabled(value, options.target)
23+
})
24+
}

src/index.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ import stdLibBrowser from 'node-stdlib-browser'
44
import { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin'
55
import esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'
66
import type { Plugin } from 'vite'
7-
import { compareModuleNames, isEnabled, isNodeProtocolImport, toRegExp, withoutNodeProtocol } from './utils'
8-
9-
type TransformHook = Extract<Plugin['transform'], Function>
10-
11-
export type BuildTarget = 'build' | 'dev'
12-
export type BooleanOrBuildTarget = boolean | BuildTarget
13-
export type ModuleName = keyof typeof stdLibBrowser
14-
export type ModuleNameWithoutNodePrefix<T = ModuleName> = T extends `node:${infer P}` ? P : never
7+
import { getModulesToPolyfill } from './modules'
8+
import { buildTrailingSlashNormalizer } from './plugins'
9+
import {
10+
type BooleanOrBuildTarget,
11+
type ModuleName,
12+
type ModuleNameWithoutNodePrefix,
13+
type TransformHook,
14+
compareModuleNames,
15+
globalShimBanners,
16+
isEnabled,
17+
isNodeProtocolImport,
18+
toRegExp,
19+
withoutNodeProtocol,
20+
} from './utils'
1521

1622
export type PolyfillOptions = {
1723
/**
@@ -89,21 +95,6 @@ export type PolyfillOptionsResolved = {
8995
protocolImports: boolean,
9096
}
9197

92-
const globalShimBanners = {
93-
buffer: [
94-
`import __buffer_polyfill from 'vite-plugin-node-polyfills/shims/buffer'`,
95-
`globalThis.Buffer = globalThis.Buffer || __buffer_polyfill`,
96-
],
97-
global: [
98-
`import __global_polyfill from 'vite-plugin-node-polyfills/shims/global'`,
99-
`globalThis.global = globalThis.global || __global_polyfill`,
100-
],
101-
process: [
102-
`import __process_polyfill from 'vite-plugin-node-polyfills/shims/process'`,
103-
`globalThis.process = globalThis.process || __process_polyfill`,
104-
],
105-
}
106-
10798
/**
10899
* Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.
109100
*
@@ -147,6 +138,16 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
147138
},
148139
}
149140

141+
const modulesToPolyfill = getModulesToPolyfill({
142+
modulesToExclude: optionsResolved.exclude,
143+
modulesToInclude: optionsResolved.include,
144+
})
145+
146+
const trailingSlashNormalizer = buildTrailingSlashNormalizer({
147+
modules: modulesToPolyfill,
148+
protocolImports: optionsResolved.protocolImports,
149+
})
150+
150151
const isExcluded = (moduleName: ModuleName) => {
151152
if (optionsResolved.include.length > 0) {
152153
return !optionsResolved.include.some((includedName) => compareModuleNames(moduleName, includedName))
@@ -220,9 +221,11 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
220221
const plugin: Plugin = {
221222
name: 'vite-plugin-node-polyfills',
222223
config(config, env) {
224+
const isBuild = env.command === 'build'
223225
const isDev = env.command === 'serve'
224226
// @ts-expect-error - this.meta.rolldownVersion only exists with rolldown-vite 7+
225227
const isRolldownVite = !!this?.meta?.rolldownVersion
228+
const isNativeInjectAvailable = isBuild && isRolldownVite
226229

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

241-
const isNativeInjectAvailable = env.command === 'build' && isRolldownVite
242244
rawInjectPlugin = (Object.keys(shimsToInject).length > 0 && !isNativeInjectAvailable) ? inject(shimsToInject) as Plugin : false
243245
if (rawInjectPlugin === false) {
244246
delete injectPlugin.transform
@@ -283,6 +285,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
283285
define: defines,
284286
},
285287
plugins: [
288+
trailingSlashNormalizer,
286289
{
287290
name: 'vite-plugin-node-polyfills:optimizer',
288291
banner: isDev ? globalShimsBanner : undefined,
@@ -331,8 +334,10 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin[] => {
331334
}
332335
},
333336
}
337+
334338
return [
339+
trailingSlashNormalizer,
335340
injectPlugin,
336341
plugin,
337-
]
342+
].flat()
338343
}

0 commit comments

Comments
 (0)