@@ -11,7 +11,7 @@ function execLoader(filename, callback) {
11
11
context : path . dirname ( filename ) ,
12
12
resolve : function ( context , request , callback ) {
13
13
process . nextTick ( function ( ) {
14
- var p = path . join ( context , request ) ;
14
+ var p = path . isAbsolute ( request ) ? request : path . resolve ( context , request ) ;
15
15
if ( fs . existsSync ( p ) )
16
16
callback ( null , p ) ;
17
17
else
@@ -40,8 +40,11 @@ function execLoader(filename, callback) {
40
40
}
41
41
42
42
describe ( "source-map-loader" , function ( ) {
43
+ const fixturesPath = path . join ( __dirname , "fixtures" ) ;
44
+ const dataPath = path . join ( fixturesPath , "data" ) ;
45
+
43
46
it ( "should leave normal files untouched" , function ( done ) {
44
- execLoader ( path . join ( __dirname , "fixtures" , "normal-file.js" ) , function ( err , res , map , deps , warns ) {
47
+ execLoader ( path . join ( fixturesPath , "normal-file.js" ) , function ( err , res , map , deps , warns ) {
45
48
should . equal ( err , null ) ;
46
49
warns . should . be . eql ( [ ] ) ;
47
50
should . equal ( res , "without SourceMap" ) ,
@@ -50,8 +53,9 @@ describe("source-map-loader", function() {
50
53
done ( ) ;
51
54
} ) ;
52
55
} ) ;
56
+
53
57
it ( "should process inlined SourceMaps" , function ( done ) {
54
- execLoader ( path . join ( __dirname , "fixtures" , "inline-source-map.js" ) , function ( err , res , map , deps , warns ) {
58
+ execLoader ( path . join ( fixturesPath , "inline-source-map.js" ) , function ( err , res , map , deps , warns ) {
55
59
should . equal ( err , null ) ;
56
60
warns . should . be . eql ( [ ] ) ;
57
61
should . equal ( res , "with SourceMap\n// comment" ) ,
@@ -68,8 +72,9 @@ describe("source-map-loader", function() {
68
72
done ( ) ;
69
73
} ) ;
70
74
} ) ;
75
+
71
76
it ( "should process external SourceMaps" , function ( done ) {
72
- execLoader ( path . join ( __dirname , "fixtures" , "external-source-map.js" ) , function ( err , res , map , deps , warns ) {
77
+ execLoader ( path . join ( fixturesPath , "external-source-map.js" ) , function ( err , res , map , deps , warns ) {
73
78
should . equal ( err , null ) ;
74
79
warns . should . be . eql ( [ ] ) ;
75
80
should . equal ( res , "with SourceMap\n// comment" ) ,
@@ -83,34 +88,36 @@ describe("source-map-loader", function() {
83
88
"mappings" :"AAAA"
84
89
} ) ;
85
90
deps . should . be . eql ( [
86
- path . join ( __dirname , "fixtures" , "external-source-map.map" )
91
+ path . join ( fixturesPath , "external-source-map.map" )
87
92
] ) ;
88
93
done ( ) ;
89
94
} ) ;
90
95
} ) ;
96
+
91
97
it ( "should process external SourceMaps (external sources)" , function ( done ) {
92
- execLoader ( path . join ( __dirname , "fixtures" , "external-source-map2.js" ) , function ( err , res , map , deps , warns ) {
98
+ execLoader ( path . join ( fixturesPath , "external-source-map2.js" ) , function ( err , res , map , deps , warns ) {
93
99
should . equal ( err , null ) ;
94
100
warns . should . be . eql ( [ ] ) ;
95
101
should . equal ( res , "with SourceMap\n// comment" ) ,
96
102
map . should . be . eql ( {
97
103
"version" :3 ,
98
104
"file" :"external-source-map2.js" ,
99
105
"sources" :[
100
- path . join ( __dirname , "fixtures" , "external-source-map2.txt" )
106
+ path . join ( fixturesPath , "external-source-map2.txt" )
101
107
] ,
102
108
"sourcesContent" :[ "with SourceMap" ] ,
103
109
"mappings" :"AAAA"
104
110
} ) ;
105
111
deps . should . be . eql ( [
106
- path . join ( __dirname , "fixtures" , "data" , "external-source-map2.map" ) ,
107
- path . join ( __dirname , "fixtures" , "external-source-map2.txt" )
112
+ path . join ( dataPath , "external-source-map2.map" ) ,
113
+ path . join ( fixturesPath , "external-source-map2.txt" )
108
114
] ) ;
109
115
done ( ) ;
110
116
} ) ;
111
117
} ) ;
118
+
112
119
it ( "should use last SourceMap directive" , function ( done ) {
113
- execLoader ( path . join ( __dirname , "fixtures" , "multi-source-map.js" ) , function ( err , res , map , deps , warns ) {
120
+ execLoader ( path . join ( fixturesPath , "multi-source-map.js" ) , function ( err , res , map , deps , warns ) {
114
121
should . equal ( err , null ) ;
115
122
warns . should . be . eql ( [ ] ) ;
116
123
should . equal ( res , "with SourceMap\nanInvalidDirective = \"\\n/*# sourceMappingURL=data:application/json;base64,\"+btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))))+\" */\";\n// comment" ) ,
@@ -127,8 +134,9 @@ describe("source-map-loader", function() {
127
134
done ( ) ;
128
135
} ) ;
129
136
} ) ;
137
+
130
138
it ( "should skip invalid base64 SourceMap" , function ( done ) {
131
- execLoader ( path . join ( __dirname , "fixtures" , "invalid-inline-source-map.js" ) , function ( err , res , map , deps , warns ) {
139
+ execLoader ( path . join ( fixturesPath , "invalid-inline-source-map.js" ) , function ( err , res , map , deps , warns ) {
132
140
should . equal ( err , null ) ;
133
141
warns . should . be . eql ( [ ] ) ;
134
142
should . equal ( res , "without SourceMap\n// @sourceMappingURL=data:application/source-map;base64,\"something invalid\"\n// comment" ) ;
@@ -138,7 +146,7 @@ describe("source-map-loader", function() {
138
146
} ) ;
139
147
} ) ;
140
148
it ( "should warn on invalid base64 SourceMap" , function ( done ) {
141
- execLoader ( path . join ( __dirname , "fixtures" , "invalid-inline-source-map2.js" ) , function ( err , res , map , deps , warns ) {
149
+ execLoader ( path . join ( fixturesPath , "invalid-inline-source-map2.js" ) , function ( err , res , map , deps , warns ) {
142
150
should . equal ( err , null ) ;
143
151
warns . should . matchEach (
144
152
new RegExp ( "Cannot parse inline SourceMap 'invalid\/base64=': SyntaxError: Unexpected token" )
@@ -149,22 +157,24 @@ describe("source-map-loader", function() {
149
157
done ( ) ;
150
158
} ) ;
151
159
} ) ;
160
+
152
161
it ( "should warn on invalid SourceMap" , function ( done ) {
153
- execLoader ( path . join ( __dirname , "fixtures" , "invalid-source-map.js" ) , function ( err , res , map , deps , warns ) {
162
+ execLoader ( path . join ( fixturesPath , "invalid-source-map.js" ) , function ( err , res , map , deps , warns ) {
154
163
should . equal ( err , null ) ;
155
164
warns . should . matchEach (
156
165
new RegExp ( "Cannot parse SourceMap 'invalid-source-map.map': SyntaxError: Unexpected string in JSON at position 102" )
157
166
) ;
158
167
should . equal ( res , "with SourceMap\n//#sourceMappingURL=invalid-source-map.map\n// comment" ) ;
159
168
should . equal ( map , null ) ;
160
169
deps . should . be . eql ( [
161
- path . join ( __dirname , "fixtures" , "invalid-source-map.map" )
170
+ path . join ( fixturesPath , "invalid-source-map.map" )
162
171
] ) ;
163
172
done ( ) ;
164
173
} ) ;
165
174
} ) ;
175
+
166
176
it ( "should warn on missing SourceMap" , function ( done ) {
167
- execLoader ( path . join ( __dirname , "fixtures" , "missing-source-map.js" ) , function ( err , res , map , deps , warns ) {
177
+ execLoader ( path . join ( fixturesPath , "missing-source-map.js" ) , function ( err , res , map , deps , warns ) {
168
178
should . equal ( err , null ) ;
169
179
warns . should . be . eql ( [
170
180
"Cannot find SourceMap 'missing-source-map.map': Error: File not found"
@@ -175,8 +185,9 @@ describe("source-map-loader", function() {
175
185
done ( ) ;
176
186
} ) ;
177
187
} ) ;
188
+
178
189
it ( "should warn on missing source file" , function ( done ) {
179
- execLoader ( path . join ( __dirname , "fixtures" , "missing-source-map2.js" ) , function ( err , res , map , deps , warns ) {
190
+ execLoader ( path . join ( fixturesPath , "missing-source-map2.js" ) , function ( err , res , map , deps , warns ) {
180
191
should . equal ( err , null ) ;
181
192
warns . should . be . eql ( [
182
193
"Cannot find source file 'missing-source-map2.txt': Error: File not found"
@@ -192,14 +203,14 @@ describe("source-map-loader", function() {
192
203
"mappings" :"AAAA"
193
204
} ) ;
194
205
deps . should . be . eql ( [
195
- path . join ( __dirname , "fixtures" , "missing-source-map2.map" )
206
+ path . join ( fixturesPath , "missing-source-map2.map" )
196
207
] ) ;
197
208
done ( ) ;
198
209
} ) ;
199
210
} ) ;
200
211
201
212
it ( "should process inlined SourceMaps with charset" , function ( done ) {
202
- execLoader ( path . join ( __dirname , "fixtures" , "charset-inline-source-map.js" ) , function ( err , res , map , deps , warns ) {
213
+ execLoader ( path . join ( fixturesPath , "charset-inline-source-map.js" ) , function ( err , res , map , deps , warns ) {
203
214
should . equal ( err , null ) ;
204
215
warns . should . be . eql ( [ ] ) ;
205
216
should . equal ( res , "with SourceMap\n// comment" ) ,
@@ -216,4 +227,81 @@ describe("source-map-loader", function() {
216
227
done ( ) ;
217
228
} ) ;
218
229
} ) ;
230
+
231
+ it ( "should support absolute sourceRoot paths in sourcemaps" , ( done ) => {
232
+ const sourceRoot = path . join ( fixturesPath ) ;
233
+ const javaScriptFilename = "absolute-sourceRoot-source-map.js" ;
234
+ const sourceFilename = "absolute-sourceRoot-source-map.txt" ;
235
+ const rootRelativeSourcePath = path . join ( sourceRoot , sourceFilename ) ;
236
+ const sourceMapPath = path . join ( sourceRoot , "absolute-sourceRoot-source-map.map" ) ;
237
+
238
+ // Create the sourcemap file
239
+ const rawSourceMap = {
240
+ "version" : 3 ,
241
+ "file" : javaScriptFilename ,
242
+ "sourceRoot" : sourceRoot ,
243
+ "sources" : [
244
+ sourceFilename
245
+ ] ,
246
+ "mappings" : "AAAA"
247
+ } ;
248
+ fs . writeFileSync ( sourceMapPath , JSON . stringify ( rawSourceMap ) ) ;
249
+
250
+ execLoader (
251
+ path . join ( fixturesPath , javaScriptFilename ) ,
252
+ ( err , res , map , deps , warns ) => {
253
+ should . equal ( err , null ) ;
254
+ warns . should . be . eql ( [ ] ) ;
255
+ should . equal ( res , "with SourceMap\n// comment" ) ,
256
+ map . should . be . eql ( {
257
+ "version" : 3 ,
258
+ "file" : javaScriptFilename ,
259
+ "sources" : [
260
+ rootRelativeSourcePath
261
+ ] ,
262
+ "sourcesContent" : [
263
+ "with SourceMap\n// comment"
264
+ ] ,
265
+ "mappings" : "AAAA"
266
+ } ) ;
267
+ deps . should . be . eql ( [
268
+ sourceMapPath ,
269
+ rootRelativeSourcePath
270
+ ] ) ;
271
+ done ( ) ;
272
+ }
273
+ ) ;
274
+ } ) ;
275
+
276
+ it ( "should support relative sourceRoot paths in sourcemaps" , ( done ) => {
277
+ const javaScriptFilename = "relative-sourceRoot-source-map.js" ;
278
+ const sourceFilename = "relative-sourceRoot-source-map.txt" ;
279
+ const rootRelativeSourcePath = path . join ( dataPath , sourceFilename ) ;
280
+ const sourceMapPath = path . join ( fixturesPath , "relative-sourceRoot-source-map.map" ) ;
281
+
282
+ execLoader (
283
+ path . join ( fixturesPath , javaScriptFilename ) ,
284
+ ( err , res , map , deps , warns ) => {
285
+ should . equal ( err , null ) ;
286
+ warns . should . be . eql ( [ ] ) ;
287
+ should . equal ( res , "with SourceMap\n// comment" ) ,
288
+ map . should . be . eql ( {
289
+ "version" : 3 ,
290
+ "file" : javaScriptFilename ,
291
+ "sources" : [
292
+ rootRelativeSourcePath
293
+ ] ,
294
+ "sourcesContent" : [
295
+ "with SourceMap\n// comment"
296
+ ] ,
297
+ "mappings" : "AAAA"
298
+ } ) ;
299
+ deps . should . be . eql ( [
300
+ sourceMapPath ,
301
+ rootRelativeSourcePath
302
+ ] ) ;
303
+ done ( ) ;
304
+ }
305
+ ) ;
306
+ } ) ;
219
307
} ) ;
0 commit comments