1
1
// @flow
2
- import type { ParsedMap , VLQMap , SourceMapStringifyOptions , IndexedMapping } from './types' ;
2
+ import type { ParsedMap , VLQMap , SourceMapStringifyOptions , IndexedMapping , GenerateEmptyMapOptions } from './types' ;
3
3
4
4
import path from 'path' ;
5
- import { generateInlineMap , partialVlqMapToSourceMap } from './utils' ;
5
+ import { generateInlineMap , partialVlqMapToSourceMap , relatifyPath , normalizePath } from './utils' ;
6
6
7
7
export default class SourceMap {
8
8
/**
9
9
* @private
10
10
*/
11
11
sourceMapInstance : any ;
12
12
13
+ /**
14
+ * @private
15
+ */
16
+ projectRoot : string ;
17
+
18
+ /**
19
+ * Construct a SourceMap instance
20
+ *
21
+ * @param projectRoot root directory of the project, this is to ensure all source paths are relative to this path
22
+ */
23
+ constructor ( projectRoot : string = '/' ) {
24
+ this . projectRoot = normalizePath ( projectRoot ) ;
25
+ }
26
+
13
27
/**
14
28
* Generates an empty map from the provided fileName and sourceContent
15
29
*
16
30
* @param sourceName path of the source file
17
31
* @param sourceContent content of the source file
18
32
* @param lineOffset an offset that gets added to the sourceLine index of each mapping
19
33
*/
20
- static generateEmptyMap ( sourceName : string , sourceContent : string , lineOffset : number = 0 ) : SourceMap {
34
+ static generateEmptyMap ( {
35
+ projectRoot,
36
+ sourceName,
37
+ sourceContent,
38
+ lineOffset = 0 ,
39
+ } : GenerateEmptyMapOptions ) : SourceMap {
21
40
throw new Error ( 'SourceMap.generateEmptyMap() must be implemented when extending SourceMap' ) ;
22
41
}
23
42
@@ -37,21 +56,7 @@ export default class SourceMap {
37
56
* Appends raw VLQ mappings to the sourcemaps
38
57
*/
39
58
addRawMappings ( map : VLQMap , lineOffset : number = 0 , columnOffset : number = 0 ) : SourceMap {
40
- let { sourcesContent, sources = [ ] , mappings, names = [ ] } = map ;
41
- if ( ! sourcesContent ) {
42
- sourcesContent = sources . map ( ( ) => '' ) ;
43
- } else {
44
- sourcesContent = sourcesContent . map ( ( content ) => ( content ? content : '' ) ) ;
45
- }
46
- this . sourceMapInstance . addRawMappings (
47
- mappings ,
48
- sources ,
49
- sourcesContent . map ( ( content ) => ( content ? content : '' ) ) ,
50
- names ,
51
- lineOffset ,
52
- columnOffset
53
- ) ;
54
- return this ;
59
+ throw new Error ( 'SourceMap.addRawMappings() must be implemented when extending SourceMap' ) ;
55
60
}
56
61
57
62
/**
@@ -90,7 +95,7 @@ export default class SourceMap {
90
95
hasValidOriginal ? mapping . original . line - 1 : - 1 ,
91
96
// $FlowFixMe
92
97
hasValidOriginal ? mapping . original . column : - 1 ,
93
- mapping . source || '' ,
98
+ mapping . source ? relatifyPath ( mapping . source , this . projectRoot ) : '' ,
94
99
mapping . name || ''
95
100
) ;
96
101
}
@@ -144,7 +149,7 @@ export default class SourceMap {
144
149
* @returns the index of the added source filepath in the sources array
145
150
*/
146
151
addSource ( source : string ) : number {
147
- return this . sourceMapInstance . addSource ( source ) ;
152
+ return this . sourceMapInstance . addSource ( relatifyPath ( source , this . projectRoot ) ) ;
148
153
}
149
154
150
155
/**
@@ -163,7 +168,7 @@ export default class SourceMap {
163
168
* @param source the filepath of the source file
164
169
*/
165
170
getSourceIndex ( source : string ) : number {
166
- return this . sourceMapInstance . getSourceIndex ( source ) ;
171
+ return this . sourceMapInstance . getSourceIndex ( relatifyPath ( source , this . projectRoot ) ) ;
167
172
}
168
173
169
174
/**
@@ -183,7 +188,7 @@ export default class SourceMap {
183
188
* @param sourceContent the content of the sourceFile
184
189
*/
185
190
setSourceContent ( sourceName : string , sourceContent : string ) : void {
186
- return this . sourceMapInstance . setSourceContent ( sourceName , sourceContent ) ;
191
+ return this . sourceMapInstance . setSourceContent ( relatifyPath ( sourceName , this . projectRoot ) , sourceContent ) ;
187
192
}
188
193
189
194
/**
@@ -192,7 +197,7 @@ export default class SourceMap {
192
197
* @param sourceName filename
193
198
*/
194
199
getSourceContent ( sourceName : string ) : string {
195
- return this . sourceMapInstance . getSourceContent ( sourceName ) ;
200
+ return this . sourceMapInstance . getSourceContent ( relatifyPath ( sourceName , this . projectRoot ) ) ;
196
201
}
197
202
198
203
/**
@@ -299,6 +304,9 @@ export default class SourceMap {
299
304
* @param options options used for formatting the serialised map
300
305
*/
301
306
async stringify ( options : SourceMapStringifyOptions ) : Promise < string | VLQMap > {
302
- return partialVlqMapToSourceMap ( this . toVLQ ( ) , options ) ;
307
+ return partialVlqMapToSourceMap ( this . toVLQ ( ) , {
308
+ ...options ,
309
+ rootDir : this . projectRoot || options . rootDir ,
310
+ } ) ;
303
311
}
304
312
}
0 commit comments