1- import { transform } from 'esbuild'
2- import { TraceMap , decodedMap , encodedMap } from '@jridgewell/trace-mapping'
1+ import { transform } from 'rolldown/experimental'
32import type { ResolvedConfig } from '../config'
43import type { Plugin } from '../plugin'
54import { 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 {
0 commit comments