11/** @import * as AST from 'estree' */
22/** @import { CompileError, Platform, PlatformSpecializationOptions } from '../../types/index' */
33
4+ import { decode , encode } from '@jridgewell/sourcemap-codec' ;
45import MagicString from 'magic-string' ;
56import { DIAGNOSTIC_CODES } from '../diagnostics.js' ;
67import { error } from '../errors.js' ;
@@ -326,23 +327,125 @@ declare global {
326327 return { ...result , code : result . code + declarations } ;
327328}
328329
330+ /**
331+ * @param {unknown } map
332+ * @returns {import('source-map').RawSourceMap | undefined }
333+ */
334+ function normalize_source_map ( map ) {
335+ if ( map == null ) return undefined ;
336+ const parsed = typeof map === 'string' ? JSON . parse ( map ) : map ;
337+ if ( parsed && typeof parsed === 'object' && typeof parsed . mappings === 'string' ) {
338+ return /** @type {import('source-map').RawSourceMap } */ ( parsed ) ;
339+ }
340+ return undefined ;
341+ }
342+
343+ /**
344+ * Chain a follow-up MagicString map through an incoming compile map so the
345+ * result still names the original file and content, not the intermediate source.
346+ *
347+ * @param {import('source-map').RawSourceMap } input_map
348+ * @param {import('source-map').RawSourceMap } output_map
349+ * @returns {import('source-map').RawSourceMap }
350+ */
351+ function compose_source_maps ( input_map , output_map ) {
352+ const input_decoded = decode ( input_map . mappings ) ;
353+ const output_decoded = decode ( output_map . mappings ) ;
354+
355+ /**
356+ * @param {number } line
357+ * @param {number } column
358+ * @returns {{ source_index: number, line: number, column: number, name_index?: number } | null }
359+ */
360+ function original_position_for ( line , column ) {
361+ const segments = input_decoded [ line ] ;
362+ if ( ! segments || segments . length === 0 ) return null ;
363+
364+ let low = 0 ;
365+ let high = segments . length - 1 ;
366+ let found = - 1 ;
367+ while ( low <= high ) {
368+ const mid = ( low + high ) >> 1 ;
369+ if ( segments [ mid ] [ 0 ] <= column ) {
370+ found = mid ;
371+ low = mid + 1 ;
372+ } else {
373+ high = mid - 1 ;
374+ }
375+ }
376+ if ( found === - 1 ) return null ;
377+
378+ const segment = segments [ found ] ;
379+ if ( segment . length < 4 ) return null ;
380+
381+ return {
382+ source_index : segment [ 1 ] ,
383+ line : segment [ 2 ] ,
384+ column : segment [ 3 ] ,
385+ name_index : segment . length > 4 ? segment [ 4 ] : undefined ,
386+ } ;
387+ }
388+
389+ /** @typedef {[number] | [number, number, number, number] | [number, number, number, number, number] } SourceMapSegment */
390+ /** @type {SourceMapSegment[][] } */
391+ const composed = [ ] ;
392+ for ( const line of output_decoded ) {
393+ /** @type {SourceMapSegment[] } */
394+ const next_line = [ ] ;
395+ for ( const segment of line ) {
396+ if ( segment . length < 4 ) {
397+ next_line . push ( [ segment [ 0 ] ] ) ;
398+ continue ;
399+ }
400+
401+ const original = original_position_for ( segment [ 2 ] , segment [ 3 ] ) ;
402+ if ( ! original ) continue ;
403+
404+ next_line . push (
405+ original . name_index === undefined
406+ ? [ segment [ 0 ] , original . source_index , original . line , original . column ]
407+ : [
408+ segment [ 0 ] ,
409+ original . source_index ,
410+ original . line ,
411+ original . column ,
412+ original . name_index ,
413+ ] ,
414+ ) ;
415+ }
416+ composed . push ( next_line ) ;
417+ }
418+
419+ return {
420+ ...input_map ,
421+ file : output_map . file ?? input_map . file ,
422+ mappings : encode ( composed ) ,
423+ } ;
424+ }
425+
329426/**
330427 * Replace exact platform flag expressions in a JavaScript/TypeScript module.
331428 * This is used by integrations without a native `define` facility. It does not
332429 * perform dead-code elimination; the downstream bundler sees literal booleans
333430 * and owns that optimization.
334431 *
432+ * When `input_map` is supplied, the returned map is composed through that map
433+ * so loader chains keep pointing at the original file instead of the
434+ * intermediate source that this pass rewrites.
435+ *
335436 * @param {string } source
336437 * @param {string } filename
337438 * @param {unknown } platform_value
439+ * @param {import('source-map').RawSourceMap | string | null } [input_map]
338440 * @returns {{ code: string, map: import('source-map').RawSourceMap } }
339441 */
340- export function replace_platform_flags ( source , filename , platform_value ) {
442+ export function replace_platform_flags ( source , filename , platform_value , input_map ) {
341443 const platform = validate_platform ( platform_value ) ;
342444 if ( platform === undefined ) {
343445 throw new TypeError ( 'Replacing TSRX platform flags requires a configured platform.' ) ;
344446 }
345447
448+ const incoming = normalize_source_map ( input_map ) ;
346449 const ast = parse_module ( source , filename ) ;
347450 const output = new MagicString ( source ) ;
348451
@@ -358,8 +461,13 @@ export function replace_platform_flags(source, filename, platform_value) {
358461 }
359462
360463 visit ( ast ) ;
464+ if ( ! output . hasChanged ( ) && incoming ) {
465+ return { code : source , map : incoming } ;
466+ }
467+
468+ const map = output . generateMap ( { source : filename , includeContent : true , hires : true } ) ;
361469 return {
362470 code : output . toString ( ) ,
363- map : output . generateMap ( { source : filename , includeContent : true , hires : true } ) ,
471+ map : incoming ? compose_source_maps ( incoming , map ) : map ,
364472 } ;
365473}
0 commit comments