1 | var ngAnnotate = require('ng-annotate');
|
2 | var utils = require('loader-utils');
|
3 | var SourceMapConsumer = require('source-map').SourceMapConsumer;
|
4 | var SourceMapGenerator = require('source-map').SourceMapGenerator;
|
5 |
|
6 | function loadPlugins(pluginNames) {
|
7 | var pluginNames = pluginNames || [];
|
8 | return pluginNames.map(function(name) {
|
9 | return require(name);
|
10 | });
|
11 | }
|
12 |
|
13 | function getOptions(sourceMapEnabled, filename) {
|
14 | var options = utils.parseQuery(this.query);
|
15 |
|
16 |
|
17 | if (options.add === undefined) {
|
18 | options.add = true;
|
19 | }
|
20 |
|
21 | if (sourceMapEnabled && options.map === undefined) {
|
22 | options.map = {
|
23 | inline: false,
|
24 | inFile: filename,
|
25 | };
|
26 | }
|
27 |
|
28 | if (options.plugin) {
|
29 | options.plugin = loadPlugins(options.plugin);
|
30 | }
|
31 |
|
32 | return options;
|
33 | }
|
34 |
|
35 | function mergeSourceMaps(inputSourceMap, annotateMap) {
|
36 | var outputSourceMap;
|
37 | var sourceMapEnabled = this.sourceMap;
|
38 | var filename = this.resourcePath;
|
39 | this.cacheable && this.cacheable();
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | if (sourceMapEnabled && inputSourceMap) {
|
45 | if (annotateMap) {
|
46 | var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(annotateMap));
|
47 | generator.applySourceMap(new SourceMapConsumer(inputSourceMap), filename);
|
48 |
|
49 | outputSourceMap = generator.toJSON();
|
50 |
|
51 |
|
52 | outputSourceMap.sourceRoot = '';
|
53 |
|
54 | outputSourceMap.file = inputSourceMap.file;
|
55 | } else {
|
56 | outputSourceMap = inputSourceMap;
|
57 | }
|
58 | }
|
59 |
|
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 |
|
71 | this.callback(null, annotateResult.src || source, outputSourceMap);
|
72 | }; |
\ | No newline at end of file |