Skip to content

Commit 4dee126

Browse files
committed
Extract utils into a separate file
1 parent c6f299e commit 4dee126

File tree

2 files changed

+57
-55
lines changed

2 files changed

+57
-55
lines changed

src/index.ts

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import stdLibBrowser from 'node-stdlib-browser'
55
import { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin'
66
import esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'
77
import type { Plugin } from 'vite'
8+
import { compareModuleNames, isEnabled, isNodeProtocolImport, toRegExp, withoutNodeProtocol } from './utils'
89

910
export type BuildTarget = 'build' | 'dev'
1011
export type BooleanOrBuildTarget = boolean | BuildTarget
@@ -87,28 +88,6 @@ export type PolyfillOptionsResolved = {
8788
protocolImports: boolean,
8889
}
8990

90-
const isBuildEnabled = (value: BooleanOrBuildTarget) => {
91-
if (!value) return false
92-
if (value === true) return true
93-
94-
return value === 'build'
95-
}
96-
97-
const isDevEnabled = (value: BooleanOrBuildTarget) => {
98-
if (!value) return false
99-
if (value === true) return true
100-
101-
return value === 'dev'
102-
}
103-
104-
const isProtocolImport = (name: string) => {
105-
return name.startsWith('node:')
106-
}
107-
108-
const stripNodePrefix = (name: ModuleName): ModuleNameWithoutNodePrefix => {
109-
return name.replace(/^node:/, '') as ModuleNameWithoutNodePrefix
110-
}
111-
11291
/**
11392
* Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.
11493
*
@@ -160,27 +139,24 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
160139
},
161140
}
162141

163-
const compareExcludedModuleNames = (moduleName: string, excludedName: string) => {
164-
return moduleName === excludedName || moduleName === `node:${excludedName}`
165-
}
166-
167-
const isExcluded = (name: string) => {
168-
if (optionsResolved.include.length) {
169-
return !optionsResolved.include.some((excludedName) => compareExcludedModuleNames(name, excludedName))
142+
const isExcluded = (moduleName: ModuleName) => {
143+
if (optionsResolved.include.length > 0) {
144+
return !optionsResolved.include.some((includedName) => compareModuleNames(moduleName, includedName))
170145
}
171-
return optionsResolved.exclude.some((excludedName) => compareExcludedModuleNames(name, excludedName))
146+
147+
return optionsResolved.exclude.some((excludedName) => compareModuleNames(moduleName, excludedName))
172148
}
173149

174150
const toOverride = (name: ModuleNameWithoutNodePrefix): string | void => {
175-
if (isDevEnabled(optionsResolved.globals.Buffer) && /^buffer$/.test(name)) {
151+
if (isEnabled(optionsResolved.globals.Buffer, 'dev') && /^buffer$/.test(name)) {
176152
return 'vite-plugin-node-polyfills/shims/buffer'
177153
}
178154

179-
if (isDevEnabled(optionsResolved.globals.global) && /^global$/.test(name)) {
155+
if (isEnabled(optionsResolved.globals.global, 'dev') && /^global$/.test(name)) {
180156
return 'vite-plugin-node-polyfills/shims/global'
181157
}
182158

183-
if (isDevEnabled(optionsResolved.globals.process) && /^process$/.test(name)) {
159+
if (isEnabled(optionsResolved.globals.process, 'dev') && /^process$/.test(name)) {
184160
return 'vite-plugin-node-polyfills/shims/process'
185161
}
186162

@@ -189,23 +165,24 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
189165
}
190166
}
191167

168+
const polyfills = (Object.entries(stdLibBrowser) as Array<[ModuleName, string]>).reduce<Record<ModuleName, string>>((included, [name, value]) => {
169+
if (!optionsResolved.protocolImports) {
170+
if (isNodeProtocolImport(name)) {
171+
return included
172+
}
173+
}
174+
175+
if (!isExcluded(name)) {
176+
included[name] = toOverride(withoutNodeProtocol(name)) || value
177+
}
178+
179+
return included
180+
}, {} as Record<ModuleName, string>)
181+
192182
return {
193183
name: 'vite-plugin-node-polyfills',
194184
config: (config, env) => {
195185
const isDev = env.command === 'serve'
196-
const polyfills = (Object.entries(stdLibBrowser) as Array<[ModuleName, string]>).reduce<Record<ModuleName, string>>((included, [name, value]) => {
197-
if (!optionsResolved.protocolImports) {
198-
if (isProtocolImport(name)) {
199-
return included
200-
}
201-
}
202-
203-
if (!isExcluded(name)) {
204-
included[name] = toOverride(stripNodePrefix(name)) || value
205-
}
206-
207-
return included
208-
}, {} as Record<ModuleName, string>)
209186

210187
return {
211188
build: {
@@ -223,9 +200,9 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
223200
{
224201
...inject({
225202
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite
226-
...(isBuildEnabled(optionsResolved.globals.Buffer) ? { Buffer: 'vite-plugin-node-polyfills/shims/buffer' } : {}),
227-
...(isBuildEnabled(optionsResolved.globals.global) ? { global: 'vite-plugin-node-polyfills/shims/global' } : {}),
228-
...(isBuildEnabled(optionsResolved.globals.process) ? { process: 'vite-plugin-node-polyfills/shims/process' } : {}),
203+
...(isEnabled(optionsResolved.globals.Buffer, 'build') ? { Buffer: 'vite-plugin-node-polyfills/shims/buffer' } : {}),
204+
...(isEnabled(optionsResolved.globals.global, 'build') ? { global: 'vite-plugin-node-polyfills/shims/global' } : {}),
205+
...(isEnabled(optionsResolved.globals.process, 'build') ? { process: 'vite-plugin-node-polyfills/shims/process' } : {}),
229206
}),
230207
},
231208
],
@@ -243,9 +220,9 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
243220
banner: isDev ? { js: globalShimsBanner } : undefined,
244221
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209
245222
define: {
246-
...((isDev && isDevEnabled(optionsResolved.globals.Buffer)) ? { Buffer: 'Buffer' } : {}),
247-
...((isDev && isDevEnabled(optionsResolved.globals.global)) ? { global: 'global' } : {}),
248-
...((isDev && isDevEnabled(optionsResolved.globals.process)) ? { process: 'process' } : {}),
223+
...((isDev && isEnabled(optionsResolved.globals.Buffer, 'dev')) ? { Buffer: 'Buffer' } : {}),
224+
...((isDev && isEnabled(optionsResolved.globals.global, 'dev')) ? { global: 'global' } : {}),
225+
...((isDev && isEnabled(optionsResolved.globals.process, 'dev')) ? { process: 'process' } : {}),
249226
},
250227
inject: [
251228
...globalShimPaths,
@@ -259,9 +236,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
259236
name: 'vite-plugin-node-polyfills-shims-resolver',
260237
setup(build) {
261238
for (const globalShimPath of globalShimPaths) {
262-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
263-
const escapedGlobalShimPath = globalShimPath.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
264-
const globalShimsFilter = new RegExp(`^${escapedGlobalShimPath}$`)
239+
const globalShimsFilter = toRegExp(globalShimPath)
265240

266241
// https://esbuild.github.io/plugins/#on-resolve
267242
build.onResolve({ filter: globalShimsFilter }, () => {

src/utils.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { BooleanOrBuildTarget, ModuleName, ModuleNameWithoutNodePrefix } from './index'
2+
3+
export const compareModuleNames = (moduleA: ModuleName, moduleB: ModuleName) => {
4+
return withoutNodeProtocol(moduleA) === withoutNodeProtocol(moduleB)
5+
}
6+
7+
export const isEnabled = (value: BooleanOrBuildTarget, mode: 'build' | 'dev') => {
8+
if (!value) return false
9+
if (value === true) return true
10+
11+
return value === mode
12+
}
13+
14+
export const isNodeProtocolImport = (name: string) => {
15+
return name.startsWith('node:')
16+
}
17+
18+
export const toRegExp = (text: string) => {
19+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
20+
const escapedText = text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
21+
22+
return new RegExp(`^${escapedText}$`)
23+
}
24+
25+
export const withoutNodeProtocol = (name: ModuleName): ModuleNameWithoutNodePrefix => {
26+
return name.replace(/^node:/, '') as ModuleNameWithoutNodePrefix
27+
}

0 commit comments

Comments
 (0)