Skip to content

Commit 7b2a2dc

Browse files
committed
feat: use OXC for define plugin
1 parent d678e52 commit 7b2a2dc

File tree

3 files changed

+10
-33
lines changed

3 files changed

+10
-33
lines changed

packages/vite/src/node/__tests__/plugins/define.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('definePlugin', () => {
6060
// assert that the default behavior is to replace import.meta.hot with undefined
6161
const transform = await createDefinePluginTransform()
6262
expect(await transform('const hot = import.meta.hot;')).toBe(
63-
'const hot = void 0;\n',
63+
'const hot = undefined;\n',
6464
)
6565
// assert that we can specify a user define to preserve import.meta.hot
6666
const overrideTransform = await createDefinePluginTransform({

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

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { transform } from 'esbuild'
2-
import { TraceMap, decodedMap, encodedMap } from '@jridgewell/trace-mapping'
1+
import { transform } from 'rolldown/experimental'
32
import type { ResolvedConfig } from '../config'
43
import type { Plugin } from '../plugin'
54
import { escapeRegex } from '../utils'
@@ -171,9 +170,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
171170
`const ${marker} = ${importMetaEnvVal};\n` + result.code
172171

173172
if (result.map) {
174-
const map = JSON.parse(result.map)
175-
map.mappings = ';' + map.mappings
176-
result.map = map
173+
result.map.mappings = ';' + result.map.mappings
177174
}
178175
}
179176
}
@@ -189,39 +186,19 @@ export async function replaceDefine(
189186
code: string,
190187
id: string,
191188
define: Record<string, string>,
192-
): Promise<{ code: string; map: string | null }> {
193-
const esbuildOptions = environment.config.esbuild || {}
194-
195-
const result = await transform(code, {
196-
loader: 'js',
197-
charset: esbuildOptions.charset ?? 'utf8',
198-
platform: 'neutral',
189+
): Promise<{ code: string; map: ReturnType<typeof transform>['map'] | null }> {
190+
const result = transform(id, code, {
191+
lang: 'js',
192+
sourceType: 'module',
199193
define,
200-
sourcefile: id,
201194
sourcemap:
202195
environment.config.command === 'build'
203196
? !!environment.config.build.sourcemap
204197
: true,
205198
})
206199

207-
// remove esbuild's <define:...> source entries
208-
// since they would confuse source map remapping/collapsing which expects a single source
209-
if (result.map.includes('<define:')) {
210-
const originalMap = new TraceMap(result.map)
211-
if (originalMap.sources.length >= 2) {
212-
const sourceIndex = originalMap.sources.indexOf(id)
213-
const decoded = decodedMap(originalMap)
214-
decoded.sources = [id]
215-
decoded.mappings = decoded.mappings.map((segments) =>
216-
segments.filter((segment) => {
217-
// modify and filter
218-
const index = segment[1]
219-
segment[1] = 0
220-
return index === sourceIndex
221-
}),
222-
)
223-
result.map = JSON.stringify(encodedMap(new TraceMap(decoded as any)))
224-
}
200+
if (result.errors.length > 0) {
201+
throw new AggregateError(result.errors, 'oxc transform error')
225202
}
226203

227204
return {

playground/js-sourcemap/__tests__/js-sourcemap.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ describe.runIf(isBuild)('build tests', () => {
181181
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
182182
{
183183
"debugId": "00000000-0000-0000-0000-000000000000",
184-
"mappings": "qBAEA,SAAS,GAAO,CACd,GAAA,AACD,CAED,SAAS,GAAY,CAEnB,QAAQ,MAAM,qBAAsB,EAAA,AACrC,CAED,GAAA",
184+
"mappings": "AAEA,SAAS,GAAO,CACd,GAAW,AACZ,CAED,SAAS,GAAY,CAEnB,QAAQ,MAAM,qBAAA,CAAA,MAAA,MAAA,EAAyC,AACxD,CAED,GAAM",
185185
"sources": [
186186
"../../with-define-object.ts",
187187
],

0 commit comments

Comments
 (0)