1
- import { transform } from 'esbuild'
2
- import { TraceMap , decodedMap , encodedMap } from '@jridgewell/trace-mapping'
1
+ import { transform } from 'rolldown/experimental'
3
2
import type { ResolvedConfig } from '../config'
4
3
import type { Plugin } from '../plugin'
5
4
import { escapeRegex } from '../utils'
@@ -171,9 +170,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
171
170
`const ${ marker } = ${ importMetaEnvVal } ;\n` + result . code
172
171
173
172
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
177
174
}
178
175
}
179
176
}
@@ -189,39 +186,19 @@ export async function replaceDefine(
189
186
code : string ,
190
187
id : string ,
191
188
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' ,
199
193
define,
200
- sourcefile : id ,
201
194
sourcemap :
202
195
environment . config . command === 'build'
203
196
? ! ! environment . config . build . sourcemap
204
197
: true ,
205
198
} )
206
199
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' )
225
202
}
226
203
227
204
return {
0 commit comments