@@ -2,6 +2,7 @@ import { URL, fileURLToPath } from 'node:url'
22import { promisify } from 'node:util'
33import { execFile } from 'node:child_process'
44import { existsSync } from 'node:fs'
5+ import { TraceMap , originalPositionFor } from '@jridgewell/trace-mapping'
56import { describe , expect , test } from 'vitest'
67import { mapFileCommentRegex } from 'convert-source-map'
78import { commentSourceMap } from '../foo-with-sourcemap-plugin'
@@ -16,12 +17,55 @@ import {
1617 serverLogs ,
1718} from '~utils'
1819
19- function createMapFileReader ( moduleUrl : string ) {
20- return async ( filename : string ) : Promise < string > => {
21- const base = new URL ( moduleUrl , page . url ( ) )
22- const res = await page . request . get ( new URL ( filename , base ) . href )
23- return res . text ( )
24- }
20+ const escapeRegexRE = / [ - / \\ ^ $ * + ? . ( ) | [ \] { } ] / g
21+ function escapeRegex ( str : string ) : string {
22+ return str . replace ( escapeRegexRE , '\\$&' )
23+ }
24+
25+ async function getDepJs ( entry : string , depIdFragment : string ) {
26+ const res = await page . request . get ( new URL ( entry , page . url ( ) ) . href )
27+ const js = await res . text ( )
28+ const depUrlMatch = js . match (
29+ new RegExp ( `from\\s+"([^"]*${ depIdFragment } [^"]*)"` ) ,
30+ )
31+ expect ( depUrlMatch ) . toBeTruthy ( )
32+
33+ const depUrl = depUrlMatch ! [ 1 ]
34+ expect ( depUrl ) . toContain ( '/deps/' )
35+
36+ const depRes = await page . request . get ( new URL ( depUrl , page . url ( ) ) . href )
37+ return depRes . text ( )
38+ }
39+
40+ function expectConsoleLogArgumentMapsToOriginalX (
41+ depJs : string ,
42+ generatedName : string ,
43+ ) {
44+ const map = extractSourcemap ( depJs )
45+ const depLines = depJs . split ( '\n' )
46+ const consoleLogCallRE = new RegExp (
47+ `console[\\w$]*\\.\\s*log[\\w$]*\\(${ escapeRegex ( generatedName ) } \\)` ,
48+ )
49+ const generatedLine =
50+ depLines . findIndex ( ( line ) => consoleLogCallRE . test ( line ) ) + 1
51+ expect ( generatedLine ) . toBeGreaterThan ( 0 )
52+
53+ const generatedColumn = depLines [ generatedLine - 1 ] . indexOf ( generatedName )
54+ expect ( generatedColumn ) . toBeGreaterThanOrEqual ( 0 )
55+
56+ const position = originalPositionFor ( new TraceMap ( map ) , {
57+ line : generatedLine ,
58+ column : generatedColumn ,
59+ } )
60+
61+ expect ( depJs ) . toMatch (
62+ / ^ \/ \/ # s o u r c e M a p p i n g U R L = d a t a : a p p l i c a t i o n \/ j s o n ; b a s e 6 4 , / m,
63+ )
64+ expect ( position ) . toMatchObject ( {
65+ line : 6 ,
66+ column : 16 ,
67+ name : 'x' ,
68+ } )
2569}
2670
2771if ( ! isBuild ) {
@@ -184,12 +228,39 @@ if (!isBuild) {
184228 expect ( depUrl ) . toContain ( '.vite/deps' )
185229 const depRes = await page . request . get ( new URL ( depUrl , page . url ( ) ) . href )
186230 const depJs = await depRes . text ( )
187- const map = await extractSourcemap ( depJs , createMapFileReader ( depUrl ) )
231+ expect ( depJs ) . toMatch (
232+ / ^ \/ \/ # s o u r c e M a p p i n g U R L = d a t a : a p p l i c a t i o n \/ j s o n ; b a s e 6 4 , / m,
233+ )
234+ const map = extractSourcemap ( depJs )
188235 expect ( map . sourcesContent ) . toBeDefined ( )
189236 expect ( map . sourcesContent ) . not . toContainEqual (
190237 expect . stringContaining ( 'defineConfig' ) ,
191238 )
192239 } )
240+
241+ test ( 'babel-transformed downleveled optimized dep maps to the correct original name' , async ( ) => {
242+ const depJs = await getDepJs (
243+ './optimized-class-field-import-babel.js' ,
244+ 'test-dep-class-field-sourcemap-babel' ,
245+ )
246+
247+ expect ( depJs ) . toContain ( 'x = () => 1' )
248+ expect ( depJs ) . toContain ( 'constructor(_x)' )
249+ expect ( depJs ) . toContain ( 'console.log(_x)' )
250+ expectConsoleLogArgumentMapsToOriginalX ( depJs , '_x' )
251+ } )
252+
253+ test ( 'oxc-transformed downleveled optimized dep maps to the correct original name' , async ( ) => {
254+ const depJs = await getDepJs (
255+ './optimized-class-field-import-oxc.js' ,
256+ 'test-dep-class-field-sourcemap-oxc' ,
257+ )
258+
259+ expect ( depJs ) . toContain ( 'x$$$ = () => 1' )
260+ expect ( depJs ) . toContain ( 'constructor$$$(_x$$$)' )
261+ expect ( depJs ) . toContain ( 'console$$$.log$$$(_x$$$)' )
262+ expectConsoleLogArgumentMapsToOriginalX ( depJs , '_x$$$' )
263+ } )
193264}
194265
195266describe . runIf ( isBuild ) ( 'build tests' , ( ) => {
0 commit comments