Skip to content

Commit 1ceaadd

Browse files
committed
feat(bun): sanatize bun plugin namespace
1 parent 11bea2a commit 1ceaadd

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/bun/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import { normalizeObjectHook } from '../utils/filter'
66
import { toArray } from '../utils/general'
77
import { createBuildContext, createPluginContext, guessLoader } from './utils'
88

9+
// Coerce plugin.name to satisfy Bun's namespace validator:
10+
// https://github.com/oven-sh/bun/blob/12d77d1ac561771e9fa1d0822e954273248e7f9a/src/js/builtins/BundlerPlugin.ts#L215-L217
11+
function toBunNamespace(name: string): string {
12+
return name.replace(/[^$a-zA-Z0-9_-]/g, '-')
13+
}
14+
915
export function getBunPlugin<UserOptions = Record<string, never>>(
1016
factory: UnpluginFactory<UserOptions>,
1117
): UnpluginInstance<UserOptions>['bun'] {
@@ -102,7 +108,7 @@ export function getBunPlugin<UserOptions = Record<string, never>>(
102108
if (!isAbsolute(result)) {
103109
return {
104110
path: result,
105-
namespace: plugin.name,
111+
namespace: toBunNamespace(plugin.name),
106112
}
107113
}
108114
return { path: result }
@@ -112,7 +118,7 @@ export function getBunPlugin<UserOptions = Record<string, never>>(
112118
return {
113119
path: result.id,
114120
external: result.external,
115-
namespace: plugin.name,
121+
namespace: toBunNamespace(plugin.name),
116122
}
117123
}
118124
return {
@@ -217,7 +223,7 @@ export function getBunPlugin<UserOptions = Record<string, never>>(
217223
}
218224

219225
for (const pluginName of virtualModulePlugins) {
220-
build.onLoad({ filter: /.*/, namespace: pluginName }, async (args) => {
226+
build.onLoad({ filter: /.*/, namespace: toBunNamespace(pluginName) }, async (args) => {
221227
return processLoadTransform(args.path, pluginName, args.loader)
222228
})
223229
}

0 commit comments

Comments
 (0)