-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathindex.js
155 lines (122 loc) · 3.43 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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import path from 'path';
import validateOptions from 'schema-utils';
import { getOptions } from 'loader-utils';
import schema from './options.json';
import {
getSourceMappingURL,
fetchFromURL,
flattenSourceMap,
getErrorReporter,
} from './utils';
export default async function loader(input, inputMap) {
const options = getOptions(this);
validateOptions(schema, options, {
name: 'Source Map Loader',
baseDataPath: 'options',
});
const { sourceMappingURL, replacementString } = getSourceMappingURL(input);
const callback = this.async();
if (!sourceMappingURL) {
callback(null, input, inputMap);
return;
}
let sourceURL;
let sourceContent;
try {
({ sourceURL, sourceContent } = await fetchFromURL(
this,
this.context,
sourceMappingURL,
'',
false,
options.unresolveSourceFetcher
));
} catch (error) {
const brokenMapUrlReporter = getErrorReporter(
this,
options.brokenMapUrlReportType
);
brokenMapUrlReporter(error);
callback(null, input, inputMap);
return;
}
if (sourceURL) {
this.addDependency(sourceURL);
}
let map;
try {
map = JSON.parse(sourceContent.replace(/^\)\]\}'/, ''));
} catch (parseError) {
const brokenMapParseReporter = getErrorReporter(
this,
options.brokenMapParseReportType
);
brokenMapParseReporter(
new Error(`Cannot parse source map from '${sourceURL}': ${parseError}`)
);
callback(null, input, inputMap);
return;
}
const context = sourceURL ? path.dirname(sourceURL) : this.context;
if (map.sections) {
// eslint-disable-next-line no-param-reassign
map = await flattenSourceMap(map);
}
const resolvedSources = await Promise.all(
map.sources.map(async (source, i) => {
// eslint-disable-next-line no-shadow
let sourceURL;
// eslint-disable-next-line no-shadow
let sourceContent;
const originalSourceContent =
map.sourcesContent && map.sourcesContent[i]
? map.sourcesContent[i]
: null;
const skipReading = originalSourceContent !== null;
try {
({ sourceURL, sourceContent } = await fetchFromURL(
this,
context,
source,
map.sourceRoot,
skipReading,
options.unresolveSourceFetcher
));
} catch (error) {
const brokenSourceUrlReporter = getErrorReporter(
this,
options.brokenSourceUrlReportType
);
brokenSourceUrlReporter(error);
sourceURL = source;
}
if (originalSourceContent) {
sourceContent = originalSourceContent;
}
if (sourceURL) {
this.addDependency(sourceURL);
}
return { sourceURL, sourceContent };
})
);
const newMap = { ...map };
newMap.sources = [];
newMap.sourcesContent = [];
delete newMap.sourceRoot;
resolvedSources.forEach((source) => {
// eslint-disable-next-line no-shadow
const { sourceURL, sourceContent } = source;
newMap.sources.push(sourceURL || '');
newMap.sourcesContent.push(sourceContent || '');
});
const sourcesContentIsEmpty =
newMap.sourcesContent.filter((entry) => Boolean(entry)).length === 0;
if (sourcesContentIsEmpty) {
delete newMap.sourcesContent;
}
callback(null, input.replace(replacementString, ''), newMap);
}