Skip to content

Commit 6253fd3

Browse files
committed
Merge pull request #12 from jvmccarthy/sourcemaps
Further source map merge refactoring
2 parents 6b7f228 + 5ff016e commit 6253fd3

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

loader.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ function getOptions(sourceMapEnabled, filename) {
2222
options.map = {
2323
inline: false,
2424
inFile: filename,
25-
sourceRoot: filename
2625
};
2726
}
2827

@@ -33,28 +32,22 @@ function getOptions(sourceMapEnabled, filename) {
3332
return options;
3433
}
3534

36-
37-
module.exports = function(source, inputSourceMap) {
35+
function mergeSourceMaps(inputSourceMap, annotateMap) {
3836
var outputSourceMap;
3937
var sourceMapEnabled = this.sourceMap;
40-
var filename = utils.getCurrentRequest(this);
38+
var filename = this.resourcePath;
4139
this.cacheable && this.cacheable();
4240

43-
var annotateResult = ngAnnotate(source, getOptions.call(this, sourceMapEnabled, filename));
44-
45-
// Merge source maps. Using BabelJS as an example,
41+
// Using BabelJS as an example,
4642
// https://github.com./babel/babel/blob/d3a73b87e9007104cb4fec343f0cfb9e1c67a4ec/packages/babel/src/transformation/file/index.js#L465
4743
// See also vinyl-sourcemaps-apply (used by gulp-ng-annotate) - https://github.com./floridoo/vinyl-sourcemaps-apply/blob/master/index.js
4844
if (sourceMapEnabled && inputSourceMap) {
49-
if (annotateResult.map) {
50-
var annotateMap = JSON.parse(annotateResult.map);
51-
//Sources array should be filled somehow to work with source-map package
52-
annotateMap.sources[0] = inputSourceMap.file;
45+
if (annotateMap) {
5346
var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(annotateMap));
54-
generator.applySourceMap(new SourceMapConsumer(inputSourceMap));
47+
generator.applySourceMap(new SourceMapConsumer(inputSourceMap), filename);
5548

5649
outputSourceMap = generator.toJSON();
57-
outputSourceMap.sources = inputSourceMap.sources;
50+
5851
//Should be set to avoid '../../file is not in SourceMap error https://github.com./huston007/ng-annotate-loader/pull/11'
5952
outputSourceMap.sourceRoot = '';
6053
//Copy file name from incoming file because it is empty by some unknown reaon
@@ -64,5 +57,16 @@ module.exports = function(source, inputSourceMap) {
6457
}
6558
}
6659

60+
return outputSourceMap;
61+
}
62+
63+
module.exports = function(source, inputSourceMap) {
64+
var sourceMapEnabled = this.sourceMap;
65+
var filename = this.resourcePath;
66+
this.cacheable && this.cacheable();
67+
68+
var annotateResult = ngAnnotate(source, getOptions.call(this, sourceMapEnabled, filename));
69+
var outputSourceMap = mergeSourceMaps.call(this, inputSourceMap, annotateResult.map);
70+
6771
this.callback(null, annotateResult.src || source, outputSourceMap);
6872
};

0 commit comments

Comments
 (0)