11import { readFileSync } from 'node:fs' ;
2- import { join } from 'node:path' ;
3- import { build , dev } from '@e2e/helper' ;
2+ import path , { join } from 'node:path' ;
3+ import { build , dev , mapSourceMapPositions } from '@e2e/helper' ;
44import { expect , test } from '@playwright/test' ;
55import type { Rspack } from '@rsbuild/core' ;
6- import sourceMap from 'source-map' ;
76
87const fixtures = __dirname ;
98
10- async function validateSourceMap (
11- rawSourceMap : string ,
12- generatedPositions : {
13- line : number ;
14- column : number ;
15- } [ ] ,
16- ) {
17- const consumer = await new sourceMap . SourceMapConsumer ( rawSourceMap ) ;
18-
19- const originalPositions = generatedPositions . map ( ( generatedPosition ) =>
20- consumer . originalPositionFor ( {
21- line : generatedPosition . line ,
22- column : generatedPosition . column ,
23- } ) ,
24- ) ;
25-
26- consumer . destroy ( ) ;
27- return originalPositions ;
28- }
29-
309async function testSourceMapType ( devtool : Rspack . Configuration [ 'devtool' ] ) {
3110 const rsbuild = await build ( {
3211 cwd : fixtures ,
@@ -36,24 +15,31 @@ async function testSourceMapType(devtool: Rspack.Configuration['devtool']) {
3615 js : devtool ,
3716 } ,
3817 legalComments : 'none' ,
18+ filenameHash : false ,
3919 } ,
4020 } ,
4121 } ) ;
4222
43- const files = await rsbuild . getDistFiles ( false ) ;
44- const [ , jsMapContent ] = Object . entries ( files ) . find (
45- ( [ name ] ) => name . includes ( 'static/js/' ) && name . endsWith ( '.js.map' ) ,
46- ) ! ;
23+ const files = await rsbuild . getDistFiles ( { sourceMaps : true } ) ;
4724
48- const [ , jsContent ] = Object . entries ( files ) . find (
49- ( [ name ] ) => name . includes ( 'static/js/' ) && name . endsWith ( '.js' ) ,
50- ) ! ;
25+ const indexSourceCode = readFileSync (
26+ path . join ( __dirname , 'src/index.js' ) ,
27+ 'utf-8' ,
28+ ) ;
29+ const appSourceCode = readFileSync (
30+ path . join ( __dirname , 'src/App.jsx' ) ,
31+ 'utf-8' ,
32+ ) ;
33+ const outputCode =
34+ files [ Object . keys ( files ) . find ( ( file ) => file . endsWith ( 'index.js' ) ) ! ] ;
35+ const sourceMap =
36+ files [ Object . keys ( files ) . find ( ( file ) => file . endsWith ( 'index.js.map' ) ) ! ] ;
5137
52- const AppContentIndex = jsContent . indexOf ( 'Hello Rsbuild!' ) ;
53- const indexContentIndex = jsContent . indexOf ( 'window.aa ' ) ;
38+ const AppContentIndex = outputCode . indexOf ( 'Hello Rsbuild!' ) ;
39+ const indexContentIndex = outputCode . indexOf ( 'window.test ' ) ;
5440
55- const originalPositions = (
56- await validateSourceMap ( jsMapContent , [
41+ const positions = (
42+ await mapSourceMapPositions ( sourceMap , [
5743 {
5844 line : 1 ,
5945 column : AppContentIndex ,
@@ -65,22 +51,23 @@ async function testSourceMapType(devtool: Rspack.Configuration['devtool']) {
6551 ] )
6652 ) . map ( ( o ) => ( {
6753 ...o ,
68- source : o . source ! . split ( 'source-map/' ) [ 1 ] || o . source ,
54+ source : o . source ? .split ( 'source-map/' ) [ 1 ] || o . source ,
6955 } ) ) ;
7056
71- expect ( originalPositions [ 0 ] ) . toEqual ( {
72- source : 'src/App.jsx' ,
73- line : 2 ,
74- column : 24 ,
75- name : null ,
76- } ) ;
77-
78- expect ( originalPositions [ 1 ] ) . toEqual ( {
79- source : 'src/index.js' ,
80- line : 7 ,
81- column : 0 ,
82- name : 'window' ,
83- } ) ;
57+ expect ( positions ) . toEqual ( [
58+ {
59+ source : 'src/App.jsx' ,
60+ line : 2 ,
61+ column : appSourceCode . split ( '\n' ) [ 1 ] . indexOf ( 'Hello Rsbuild!' ) ,
62+ name : null ,
63+ } ,
64+ {
65+ source : 'src/index.js' ,
66+ line : 7 ,
67+ column : indexSourceCode . split ( '\n' ) [ 6 ] . indexOf ( 'window' ) ,
68+ name : 'window' ,
69+ } ,
70+ ] ) ;
8471}
8572
8673const productionDevtools : Rspack . Configuration [ 'devtool' ] [ ] = [
@@ -101,7 +88,7 @@ test('should not generate source map by default in production build', async () =
10188 cwd : fixtures ,
10289 } ) ;
10390
104- const files = await rsbuild . getDistFiles ( false ) ;
91+ const files = await rsbuild . getDistFiles ( { sourceMaps : true } ) ;
10592
10693 const jsMapFiles = Object . keys ( files ) . filter ( ( files ) =>
10794 files . endsWith ( '.js.map' ) ,
@@ -123,7 +110,7 @@ test('should generate source map if `output.sourceMap` is true', async () => {
123110 } ,
124111 } ) ;
125112
126- const files = await rsbuild . getDistFiles ( false ) ;
113+ const files = await rsbuild . getDistFiles ( { sourceMaps : true } ) ;
127114
128115 const jsMapFiles = Object . keys ( files ) . filter ( ( files ) =>
129116 files . endsWith ( '.js.map' ) ,
@@ -145,7 +132,7 @@ test('should not generate source map if `output.sourceMap` is false', async () =
145132 } ,
146133 } ) ;
147134
148- const files = await rsbuild . getDistFiles ( false ) ;
135+ const files = await rsbuild . getDistFiles ( { sourceMaps : true } ) ;
149136
150137 const jsMapFiles = Object . keys ( files ) . filter ( ( files ) =>
151138 files . endsWith ( '.js.map' ) ,
@@ -165,7 +152,7 @@ test('should generate source map correctly in development build', async ({
165152 page,
166153 } ) ;
167154
168- const files = await rsbuild . getDistFiles ( false ) ;
155+ const files = await rsbuild . getDistFiles ( { sourceMaps : true } ) ;
169156
170157 const jsMapFile = Object . keys ( files ) . find ( ( files ) =>
171158 files . endsWith ( '.js.map' ) ,
@@ -200,7 +187,7 @@ test('should allow to only generate source map for CSS files', async () => {
200187 } ,
201188 } ) ;
202189
203- const files = await rsbuild . getDistFiles ( false ) ;
190+ const files = await rsbuild . getDistFiles ( { sourceMaps : true } ) ;
204191
205192 const jsMapFiles = Object . keys ( files ) . filter ( ( files ) =>
206193 files . endsWith ( '.js.map' ) ,
0 commit comments