Skip to content

Commit 1ee8ef4

Browse files
authored
chore: add deprecation warning for transformWithEsbuild (#173)
1 parent b642f48 commit 1ee8ef4

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

packages/vite/src/node/plugins/esbuild.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,29 @@ const importEsbuild = () => {
8181
return esbuild
8282
}
8383

84+
let warnedTransformWithEsbuild = false
85+
const warnTransformWithEsbuildUsageOnce = () => {
86+
if (warnedTransformWithEsbuild) return
87+
warnedTransformWithEsbuild = true
88+
89+
// eslint-disable-next-line no-console -- logger cannot be used here
90+
console.warn(
91+
colors.yellow(
92+
'`transformWithEsbuild` is deprecated and will be removed in the future. ' +
93+
'Please migrate to `transformWithOxc`.',
94+
),
95+
)
96+
}
97+
8498
export async function transformWithEsbuild(
8599
code: string,
86100
filename: string,
87101
options?: EsbuildTransformOptions,
88102
inMap?: object,
89103
config?: ResolvedConfig,
90104
watcher?: FSWatcher,
105+
/** @internal */
106+
ignoreEsbuildWarning = false,
91107
): Promise<ESBuildTransformResult> {
92108
let loader = options?.loader
93109

@@ -202,8 +218,23 @@ export async function transformWithEsbuild(
202218
// @ts-expect-error jsxInject exists in ESBuildOptions
203219
delete resolvedOptions.jsxInject
204220

221+
let transform: typeof import('esbuild').transform
222+
try {
223+
transform = (await importEsbuild()).transform
224+
} catch (e) {
225+
throw new Error(
226+
'Failed to load `transformWithEsbuild`. ' +
227+
'It is deprecated and it now requires esbuild to be installed separately. ' +
228+
'If you are a package author, please migrate to `transformWithOxc` instead.',
229+
{ cause: e },
230+
)
231+
}
232+
233+
if (!ignoreEsbuildWarning) {
234+
warnTransformWithEsbuildUsageOnce()
235+
}
236+
205237
try {
206-
const { transform } = await importEsbuild()
207238
const result = await transform(code, resolvedOptions)
208239
let map: SourceMap
209240
if (inMap && resolvedOptions.sourcemap) {
@@ -350,6 +381,8 @@ export const buildEsbuildPlugin = (): Plugin => {
350381
options,
351382
undefined,
352383
config,
384+
undefined,
385+
true,
353386
)
354387

355388
if (config.build.lib) {

0 commit comments

Comments
 (0)