Skip to content
This repository was archived by the owner on Oct 9, 2020. It is now read-only.

Commit c4094c3

Browse files
committed
fix(): loader should not fetch sourcemaps if disabled.
* Currently if the trace option `sourceMaps` is set to false, the sourceMaps will be still loaded. This isn't valid. Once the option is set to false, it should just ignore the sourceMaps. Fixes #610.
1 parent f018a60 commit c4094c3

File tree

3 files changed

+63
-33
lines changed

3 files changed

+63
-33
lines changed

lib/builder.js

+1-25
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ var asp = require('bluebird').promisify;
55
var fs = require('fs');
66
var path = require('path');
77

8-
var dataUriToBuffer = require('data-uri-to-buffer');
9-
108
var extend = require('./utils').extend;
119

1210
var attachCompilers = require('./compile').attachCompilers;
@@ -190,29 +188,7 @@ Builder.prototype.reset = function(baseLoader) {
190188
throw err;
191189
});
192190
})
193-
}))
194-
195-
// support picking up source URLs during fetch to set as metadata.sourceMap
196-
.then(function (source) {
197-
if (load.metadata.sourceMap)
198-
return source;
199-
var sourceMap = source.match(/^\s*\/\/\s*[#@] sourceMappingURL=([^\s'"]*)/m);
200-
if (!sourceMap)
201-
return source;
202-
if (sourceMap[1].startsWith('data:')) {
203-
// inline source map
204-
load.metadata.sourceMap = JSON.parse(dataUriToBuffer(sourceMap[1]).toString('utf8'));
205-
return source;
206-
}
207-
else {
208-
// relative path to the .map file
209-
var sourceMapPath = path.join(path.dirname(fromFileURL(load.address)), sourceMap[1]);
210-
return asp(fs.readFile)(sourceMapPath, 'utf8').then(function (sourceMap) {
211-
load.metadata.sourceMap = JSON.parse(sourceMap);
212-
return source;
213-
});
214-
}
215-
});
191+
}));
216192
};
217193

218194
// this allows us to normalize package conditionals into package conditionals

lib/trace.js

+46-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var getPackageConfigPath = require('./utils').getPackageConfigPath;
1212
var isPackageConfig = require('./utils').isPackageConfig;
1313
var parseCondition = require('./utils').parseCondition;
1414
var serializeCondition = require('./utils').serializeCondition;
15+
var dataUriToBuffer = require('data-uri-to-buffer');
1516

1617
module.exports = Trace;
1718

@@ -51,9 +52,9 @@ Trace.prototype.traceCanonical = function(canonical, traceOpts) {
5152
return toCanonicalConditionalEnv.call(self, traceOpts.conditions)
5253
.then(function(canonicalConditionalEnv) {
5354
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);
5556
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);
5758
})
5859
.then(function(loads) {
5960
// 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) {
108109
* Low-level functions
109110
*/
110111
// 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) {
112113

113114
var loader = this.loader;
114115
var loads = this.loads;
@@ -422,7 +423,44 @@ Trace.prototype.getLoadRecord = function(canonical, excludeURLs, parentStack) {
422423
}
423424

424425
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*[#@] sourceMappingURL=([^\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+
426464
.then(function(source) {
427465
if (typeof source != 'string')
428466
throw new TypeError('Loader fetch hook did not return a source string');
@@ -540,7 +578,7 @@ Trace.prototype.getLoadRecord = function(canonical, excludeURLs, parentStack) {
540578
*
541579
*/
542580
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) {
544582
var loader = this.loader;
545583

546584
curLoads = curLoads || {};
@@ -549,7 +587,7 @@ Trace.prototype.getAllLoadRecords = function(canonical, excludeURLs, tracePackag
549587
return curLoads;
550588

551589
var self = this;
552-
return this.getLoadRecord(canonical, excludeURLs, parentStack)
590+
return this.getLoadRecord(canonical, excludeURLs, parentStack, sourceMaps)
553591
.then(function(load) {
554592
// conditionals, build: false and system modules are falsy loads in the trace trees
555593
// (that is, part of depcache, but not built)
@@ -571,14 +609,14 @@ Trace.prototype.getAllLoadRecords = function(canonical, excludeURLs, tracePackag
571609

572610
// helper function -> returns the "condition" build of a tree
573611
// 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) {
575613
var loader = this.loader;
576614

577615
if (canonical in curLoads)
578616
return curLoads;
579617

580618
var self = this;
581-
return this.getLoadRecord(canonical, excludeURLs, parentStack)
619+
return this.getLoadRecord(canonical, excludeURLs, parentStack, sourceMaps)
582620
.then(function(load) {
583621
if (inConditionTree && systemModules.indexOf(canonical) == -1)
584622
curLoads[canonical] = load;

test/sourcemaps.js

+16
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ suite('Source Maps', function() {
108108
.catch(err);
109109
});
110110

111+
test('can be disabled for tracing', function(done) {
112+
var module = 'register.js';
113+
var instance = new Builder();
114+
115+
// Load our test configuration.
116+
instance.loadConfigSync(configFile);
117+
118+
instance
119+
.bundle(module, { sourceMaps: false })
120+
.then(function(output) {
121+
assert.isUndefined(output.sourceMap);
122+
})
123+
.then(done)
124+
.catch(err);
125+
});
126+
111127
suite('sources paths', function() {
112128

113129
test('are relative to outFile', function(done) {

0 commit comments

Comments
 (0)