-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathindex.js
217 lines (162 loc) · 5.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import path from 'path';
import { promisify } from 'util';
import validateOptions from 'schema-utils';
import parseDataURL from 'data-urls';
import { SourceMapConsumer } from 'source-map';
import { labelToName, decode } from 'whatwg-encoding';
import { getOptions, urlToRequest } from 'loader-utils';
import schema from './options.json';
import {
flattenSourceMap,
readFile,
fetchFile,
getContentFromSourcesContent,
getSourceMappingUrl,
getRequestedUrl,
} from './utils';
export default async function loader(input, inputMap) {
const options = getOptions(this);
validateOptions(schema, options, {
name: 'Source Map Loader',
baseDataPath: 'options',
});
const { fetchReader } = options;
let { url } = getSourceMappingUrl(input);
const { replacementString } = getSourceMappingUrl(input);
const callback = this.async();
if (!url) {
callback(null, input, inputMap);
return;
}
const { fs, context, resolve, addDependency, emitWarning } = this;
const resolver = promisify(resolve);
const reader = promisify(fs.readFile).bind(fs);
if (url.toLowerCase().startsWith('data:')) {
const dataURL = parseDataURL(url);
if (dataURL) {
let map;
try {
dataURL.encodingName =
labelToName(dataURL.mimeType.parameters.get('charset')) || 'UTF-8';
map = decode(dataURL.body, dataURL.encodingName);
map = JSON.parse(map.replace(/^\)\]\}'/, ''));
} catch (error) {
emitWarning(
`Cannot parse inline SourceMap with Charset ${dataURL.encodingName}: ${error}`
);
callback(null, input, inputMap);
return;
}
processMap(map, context, callback);
return;
}
emitWarning(`Cannot parse inline SourceMap: ${url}`);
callback(null, input, inputMap);
return;
}
try {
url = getRequestedUrl(url);
} catch (error) {
emitWarning(error.message);
callback(null, input, inputMap);
return;
}
let urlResolved;
try {
urlResolved = await resolver(context, urlToRequest(url, true));
} catch (resolveError) {
emitWarning(`Cannot find SourceMap '${url}': ${resolveError}`);
callback(null, input, inputMap);
return;
}
urlResolved = urlResolved.toString();
addDependency(urlResolved);
const content = await reader(urlResolved);
let map;
try {
map = JSON.parse(content.toString());
} catch (parseError) {
emitWarning(`Cannot parse SourceMap '${url}': ${parseError}`);
callback(null, input, inputMap);
return;
}
processMap(map, path.dirname(urlResolved), callback);
// eslint-disable-next-line no-shadow
async function processMap(map, context, callback) {
if (map.sections) {
// eslint-disable-next-line no-param-reassign
map = await flattenSourceMap(map);
}
const mapConsumer = await new SourceMapConsumer(map);
let resolvedSources;
try {
resolvedSources = await Promise.all(
map.sources.map(async (source) => {
const fullPath = map.sourceRoot
? `${map.sourceRoot}${path.sep}${source}`
: source;
const originalData = getContentFromSourcesContent(
mapConsumer,
source
);
if (/^https?:\/\//.test(fullPath)) {
return originalData
? { source: fullPath, content: originalData }
: fetchFile(fullPath, emitWarning, fetchReader);
}
if (path.isAbsolute(fullPath)) {
return originalData
? { source: fullPath, content: originalData }
: readFile(fullPath, emitWarning, reader);
}
let fullPathResolved;
try {
fullPathResolved = await resolver(
context,
urlToRequest(fullPath, true)
);
} catch (resolveError) {
emitWarning(`Cannot find source file '${source}': ${resolveError}`);
return originalData
? {
source: fullPath,
content: originalData,
}
: { source: fullPath, content: null };
}
return originalData
? {
source: fullPathResolved,
content: originalData,
}
: readFile(fullPathResolved, emitWarning, reader);
})
);
} catch (error) {
emitWarning(error);
callback(null, input, inputMap);
}
const resultMap = { ...map };
resultMap.sources = [];
resultMap.sourcesContent = [];
delete resultMap.sourceRoot;
resolvedSources.forEach((res) => {
// eslint-disable-next-line no-param-reassign
resultMap.sources.push(path.normalize(res.source));
resultMap.sourcesContent.push(res.content);
if (res.source) {
addDependency(res.source);
}
});
const sourcesContentIsEmpty =
resultMap.sourcesContent.filter((entry) => !!entry).length === 0;
if (sourcesContentIsEmpty) {
delete resultMap.sourcesContent;
}
callback(null, input.replace(replacementString, ''), resultMap);
}
}