@@ -12,6 +12,7 @@ var getPackageConfigPath = require('./utils').getPackageConfigPath;
12
12
var isPackageConfig = require ( './utils' ) . isPackageConfig ;
13
13
var parseCondition = require ( './utils' ) . parseCondition ;
14
14
var serializeCondition = require ( './utils' ) . serializeCondition ;
15
+ var dataUriToBuffer = require ( 'data-uri-to-buffer' ) ;
15
16
16
17
module . exports = Trace ;
17
18
@@ -51,9 +52,9 @@ Trace.prototype.traceCanonical = function(canonical, traceOpts) {
51
52
return toCanonicalConditionalEnv . call ( self , traceOpts . conditions )
52
53
. then ( function ( canonicalConditionalEnv ) {
53
54
if ( ! traceOpts . traceConditionsOnly )
54
- return self . getAllLoadRecords ( canonical , traceOpts . excludeURLs , traceOpts . tracePackageConfig , traceOpts . traceAllConditionals , canonicalConditionalEnv , { } , [ ] ) ;
55
+ return self . getAllLoadRecords ( canonical , traceOpts . excludeURLs , traceOpts . tracePackageConfig , traceOpts . traceAllConditionals , canonicalConditionalEnv , { } , [ ] , traceOpts . sourceMaps ) ;
55
56
else
56
- return self . getConditionLoadRecords ( canonical , traceOpts . excludeURLs , traceOpts . tracePackageConfig , canonicalConditionalEnv , false , { } , [ ] ) ;
57
+ return self . getConditionLoadRecords ( canonical , traceOpts . excludeURLs , traceOpts . tracePackageConfig , canonicalConditionalEnv , false , { } , [ ] , traceOpts . sourceMaps ) ;
57
58
} )
58
59
. then ( function ( loads ) {
59
60
// if it is a bundle, we just use a regex to extract the list of loads
@@ -108,7 +109,7 @@ function isLoadFresh(load, loader, loads) {
108
109
* Low-level functions
109
110
*/
110
111
// runs the pipeline hooks, returning the load record for a module
111
- Trace . prototype . getLoadRecord = function ( canonical , excludeURLs , parentStack ) {
112
+ Trace . prototype . getLoadRecord = function ( canonical , excludeURLs , parentStack , sourceMaps ) {
112
113
113
114
var loader = this . loader ;
114
115
var loads = this . loads ;
@@ -422,7 +423,44 @@ Trace.prototype.getLoadRecord = function(canonical, excludeURLs, parentStack) {
422
423
}
423
424
424
425
curHook = 'fetch' ;
425
- return loader . fetch ( { name : normalized , metadata : load . metadata , address : address } )
426
+
427
+
428
+ return loader
429
+ . fetch ( { name : normalized , metadata : load . metadata , address : address } )
430
+
431
+ // Parse source map definitions inside of the source and apply it to the metadata if present.
432
+ . then ( function ( source ) {
433
+ // Once the sourceMaps option is set to false, we will not parse any source map definitions.
434
+ // Just returning the plain source is required.
435
+ if ( sourceMaps === false ) {
436
+ return source ;
437
+ }
438
+
439
+ if ( load . metadata . sourceMap ) return source ;
440
+
441
+ // Search for the specified sourceMap definition in the files source.
442
+ var sourceMap = source . match ( / ^ \s * \/ \/ \s * [ # @ ] s o u r c e M a p p i n g U R L = ( [ ^ \s ' " ] * ) / m) ;
443
+
444
+ if ( ! sourceMap ) {
445
+ return source ;
446
+ }
447
+
448
+ // Once the sourceMappingURL starts with `data:`, we have to parse it as an inline source map.
449
+ if ( sourceMap [ 1 ] . startsWith ( 'data:' ) ) {
450
+ load . metadata . sourceMap = JSON . parse ( dataUriToBuffer ( sourceMap [ 1 ] ) . toString ( 'utf8' ) ) ;
451
+ return source ;
452
+ } else {
453
+ // Retrieve the path of the sourceMappingURL in relative to the
454
+ // relative path to the .map file
455
+ var sourceMapPath = path . join ( path . dirname ( fromFileURL ( address ) ) , sourceMap [ 1 ] ) ;
456
+
457
+ return asp ( fs . readFile ) ( sourceMapPath , 'utf8' ) . then ( function ( sourceMap ) {
458
+ load . metadata . sourceMap = JSON . parse ( sourceMap ) ;
459
+ return source ;
460
+ } ) ;
461
+ }
462
+ } )
463
+
426
464
. then ( function ( source ) {
427
465
if ( typeof source != 'string' )
428
466
throw new TypeError ( 'Loader fetch hook did not return a source string' ) ;
@@ -540,7 +578,7 @@ Trace.prototype.getLoadRecord = function(canonical, excludeURLs, parentStack) {
540
578
*
541
579
*/
542
580
var systemModules = [ '@empty' , '@system-env' , '@@amd-helpers' , '@@global-helpers' ] ;
543
- Trace . prototype . getAllLoadRecords = function ( canonical , excludeURLs , tracePackageConfig , traceAllConditionals , canonicalConditionalEnv , curLoads , parentStack ) {
581
+ Trace . prototype . getAllLoadRecords = function ( canonical , excludeURLs , tracePackageConfig , traceAllConditionals , canonicalConditionalEnv , curLoads , parentStack , sourceMaps ) {
544
582
var loader = this . loader ;
545
583
546
584
curLoads = curLoads || { } ;
@@ -549,7 +587,7 @@ Trace.prototype.getAllLoadRecords = function(canonical, excludeURLs, tracePackag
549
587
return curLoads ;
550
588
551
589
var self = this ;
552
- return this . getLoadRecord ( canonical , excludeURLs , parentStack )
590
+ return this . getLoadRecord ( canonical , excludeURLs , parentStack , sourceMaps )
553
591
. then ( function ( load ) {
554
592
// conditionals, build: false and system modules are falsy loads in the trace trees
555
593
// (that is, part of depcache, but not built)
@@ -571,14 +609,14 @@ Trace.prototype.getAllLoadRecords = function(canonical, excludeURLs, tracePackag
571
609
572
610
// helper function -> returns the "condition" build of a tree
573
611
// that is the modules needed to determine the exact conditional solution of the tree
574
- Trace . prototype . getConditionLoadRecords = function ( canonical , excludeURLs , tracePackageConfig , canonicalConditionalEnv , inConditionTree , curLoads , parentStack ) {
612
+ Trace . prototype . getConditionLoadRecords = function ( canonical , excludeURLs , tracePackageConfig , canonicalConditionalEnv , inConditionTree , curLoads , parentStack , sourceMaps ) {
575
613
var loader = this . loader ;
576
614
577
615
if ( canonical in curLoads )
578
616
return curLoads ;
579
617
580
618
var self = this ;
581
- return this . getLoadRecord ( canonical , excludeURLs , parentStack )
619
+ return this . getLoadRecord ( canonical , excludeURLs , parentStack , sourceMaps )
582
620
. then ( function ( load ) {
583
621
if ( inConditionTree && systemModules . indexOf ( canonical ) == - 1 )
584
622
curLoads [ canonical ] = load ;
0 commit comments