UNPKG

4 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = loader;
7
8var _path = _interopRequireDefault(require("path"));
9
10var _options = _interopRequireDefault(require("./options.json"));
11
12var _utils = require("./utils");
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16/*
17 MIT License http://www.opensource.org/licenses/mit-license.php
18 Author Tobias Koppers @sokra
19*/
20async function loader(input, inputMap) {
21 const options = this.getOptions(_options.default);
22 const {
23 sourceMappingURL,
24 replacementString
25 } = (0, _utils.getSourceMappingURL)(input);
26 const callback = this.async();
27
28 if (!sourceMappingURL) {
29 callback(null, input, inputMap);
30 return;
31 }
32
33 let behaviourSourceMappingUrl;
34
35 try {
36 behaviourSourceMappingUrl = typeof options.filterSourceMappingUrl !== "undefined" ? options.filterSourceMappingUrl(sourceMappingURL, this.resourcePath) : "consume";
37 } catch (error) {
38 callback(error);
39 return;
40 } // eslint-disable-next-line default-case
41
42
43 switch (behaviourSourceMappingUrl) {
44 case "skip":
45 callback(null, input, inputMap);
46 return;
47
48 case false:
49 case "remove":
50 callback(null, input.replace(replacementString, ""), inputMap);
51 return;
52 }
53
54 let sourceURL;
55 let sourceContent;
56
57 try {
58 ({
59 sourceURL,
60 sourceContent
61 } = await (0, _utils.fetchFromURL)(this, this.context, sourceMappingURL));
62 } catch (error) {
63 this.emitWarning(error);
64 callback(null, input, inputMap);
65 return;
66 }
67
68 if (sourceURL) {
69 this.addDependency(sourceURL);
70 }
71
72 let map;
73
74 try {
75 map = JSON.parse(sourceContent.replace(/^\)\]\}'/, ""));
76 } catch (parseError) {
77 this.emitWarning(new Error(`Failed to parse source map from '${sourceMappingURL}': ${parseError}`));
78 callback(null, input, inputMap);
79 return;
80 }
81
82 const context = sourceURL ? _path.default.dirname(sourceURL) : this.context;
83
84 if (map.sections) {
85 // eslint-disable-next-line no-param-reassign
86 map = await (0, _utils.flattenSourceMap)(map);
87 }
88
89 const resolvedSources = await Promise.all(map.sources.map(async (source, i) => {
90 // eslint-disable-next-line no-shadow
91 let sourceURL; // eslint-disable-next-line no-shadow
92
93 let sourceContent;
94 const originalSourceContent = map.sourcesContent && typeof map.sourcesContent[i] !== "undefined" ? map.sourcesContent[i] : // eslint-disable-next-line no-undefined
95 undefined;
96 const skipReading = typeof originalSourceContent !== "undefined";
97 let errored = false; // We do not skipReading here, because we need absolute paths in sources.
98 // This is necessary so that for sourceMaps with the same file structure in sources, name collisions do not occur.
99 // https://github.com/webpack-contrib/source-map-loader/issues/51
100
101 try {
102 ({
103 sourceURL,
104 sourceContent
105 } = await (0, _utils.fetchFromURL)(this, context, source, map.sourceRoot, skipReading));
106 } catch (error) {
107 errored = true;
108 this.emitWarning(error);
109 }
110
111 if (skipReading) {
112 sourceContent = originalSourceContent;
113 } else if (!errored && sourceURL) {
114 this.addDependency(sourceURL);
115 } // Return original value of `source` when error happens
116
117
118 return {
119 sourceURL: errored ? source : sourceURL,
120 sourceContent
121 };
122 }));
123 const newMap = { ...map
124 };
125 newMap.sources = [];
126 newMap.sourcesContent = [];
127 delete newMap.sourceRoot;
128 resolvedSources.forEach(source => {
129 // eslint-disable-next-line no-shadow
130 const {
131 sourceURL,
132 sourceContent
133 } = source;
134 newMap.sources.push(sourceURL || "");
135 newMap.sourcesContent.push(sourceContent || "");
136 });
137 const sourcesContentIsEmpty = newMap.sourcesContent.filter(entry => Boolean(entry)).length === 0;
138
139 if (sourcesContentIsEmpty) {
140 delete newMap.sourcesContent;
141 }
142
143 callback(null, input.replace(replacementString, ""), newMap);
144}
\No newline at end of file