@atlaspack/utils 2.16.2-noselfbuild-63bde801d.0 → 2.16.2-typescript-e769947a5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/lib/index.js +37343 -470
  2. package/lib/index.js.map +1 -0
  3. package/package.json +16 -15
  4. package/src/index.js +1 -4
  5. package/src/progress-message.js +0 -15
  6. package/lib/DefaultMap.js +0 -46
  7. package/lib/Deferred.js +0 -30
  8. package/lib/PromiseQueue.js +0 -112
  9. package/lib/TapStream.js +0 -34
  10. package/lib/alternatives.js +0 -116
  11. package/lib/ansi-html.js +0 -18
  12. package/lib/blob.js +0 -40
  13. package/lib/bundle-url.js +0 -34
  14. package/lib/collection.js +0 -111
  15. package/lib/config.js +0 -172
  16. package/lib/countLines.js +0 -15
  17. package/lib/debounce.js +0 -18
  18. package/lib/debug-tools.js +0 -36
  19. package/lib/dependency-location.js +0 -21
  20. package/lib/escape-html.js +0 -22
  21. package/lib/generateBuildMetrics.js +0 -121
  22. package/lib/generateCertificate.js +0 -129
  23. package/lib/getCertificate.js +0 -18
  24. package/lib/getExisting.js +0 -25
  25. package/lib/getModuleParts.js +0 -30
  26. package/lib/getRootDir.js +0 -52
  27. package/lib/glob.js +0 -110
  28. package/lib/hash.js +0 -50
  29. package/lib/http-server.js +0 -85
  30. package/lib/is-url.js +0 -22
  31. package/lib/isDirectoryInside.js +0 -18
  32. package/lib/objectHash.js +0 -27
  33. package/lib/openInBrowser.js +0 -74
  34. package/lib/parseCSSImport.js +0 -15
  35. package/lib/path.js +0 -39
  36. package/lib/prettifyTime.js +0 -9
  37. package/lib/prettyDiagnostic.js +0 -136
  38. package/lib/progress-message.js +0 -36
  39. package/lib/relativeBundlePath.js +0 -22
  40. package/lib/relativeUrl.js +0 -24
  41. package/lib/replaceBundleReferences.js +0 -199
  42. package/lib/schema.js +0 -336
  43. package/lib/shared-buffer.js +0 -27
  44. package/lib/sourcemap.js +0 -127
  45. package/lib/stream.js +0 -76
  46. package/lib/throttle.js +0 -15
  47. package/lib/urlJoin.js +0 -35
@@ -1,199 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getURLReplacement = getURLReplacement;
7
- exports.replaceInlineReferences = replaceInlineReferences;
8
- exports.replaceURLReferences = replaceURLReferences;
9
- function _rust() {
10
- const data = require("@atlaspack/rust");
11
- _rust = function () {
12
- return data;
13
- };
14
- return data;
15
- }
16
- function _featureFlags() {
17
- const data = require("@atlaspack/feature-flags");
18
- _featureFlags = function () {
19
- return data;
20
- };
21
- return data;
22
- }
23
- function _stream() {
24
- const data = require("stream");
25
- _stream = function () {
26
- return data;
27
- };
28
- return data;
29
- }
30
- function _nullthrows() {
31
- const data = _interopRequireDefault(require("nullthrows"));
32
- _nullthrows = function () {
33
- return data;
34
- };
35
- return data;
36
- }
37
- function _assert() {
38
- const data = _interopRequireDefault(require("assert"));
39
- _assert = function () {
40
- return data;
41
- };
42
- return data;
43
- }
44
- function _url() {
45
- const data = _interopRequireDefault(require("url"));
46
- _url = function () {
47
- return data;
48
- };
49
- return data;
50
- }
51
- var _ = require("./");
52
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
53
- /*
54
- * Replaces references to dependency ids for URL dependencies with:
55
- * - in the case of an unresolvable url dependency, the original specifier.
56
- * These are external requests that Parcel did not bundle.
57
- * - in the case of a reference to another bundle, the relative url to that
58
- * bundle from the current bundle.
59
- */
60
- function replaceURLReferences({
61
- bundle,
62
- bundleGraph,
63
- contents,
64
- map,
65
- getReplacement = s => s,
66
- relative = true
67
- }) {
68
- let replacements = new Map();
69
- let urlDependencies = [];
70
- bundle.traverse(node => {
71
- if (node.type === 'dependency' && node.value.specifierType === 'url') {
72
- urlDependencies.push(node.value);
73
- }
74
- });
75
- for (let dependency of urlDependencies) {
76
- var _dependency$meta;
77
- if (dependency.specifierType !== 'url') {
78
- continue;
79
- }
80
- let placeholder = ((_dependency$meta = dependency.meta) === null || _dependency$meta === void 0 ? void 0 : _dependency$meta.placeholder) ?? dependency.id;
81
- (0, _assert().default)(typeof placeholder === 'string');
82
- let resolved = bundleGraph.getReferencedBundle(dependency, bundle);
83
- if (resolved == null) {
84
- replacements.set(placeholder, {
85
- from: placeholder,
86
- to: getReplacement(dependency.specifier)
87
- });
88
- continue;
89
- }
90
- if (resolved.bundleBehavior === 'inline') {
91
- // If a bundle is inline, it should be replaced with inline contents,
92
- // not a URL.
93
- continue;
94
- }
95
- replacements.set(placeholder, getURLReplacement({
96
- dependency,
97
- fromBundle: bundle,
98
- toBundle: resolved,
99
- relative,
100
- getReplacement
101
- }));
102
- }
103
- return performReplacement(replacements, contents, map);
104
- }
105
-
106
- /*
107
- * Replaces references to dependency ids for inline bundles with the packaged
108
- * contents of that bundle.
109
- */
110
- async function replaceInlineReferences({
111
- bundle,
112
- bundleGraph,
113
- contents,
114
- map,
115
- getInlineReplacement,
116
- getInlineBundleContents
117
- }) {
118
- let replacements = new Map();
119
- let dependencies = [];
120
- bundle.traverse(node => {
121
- if (node.type === 'dependency') {
122
- dependencies.push(node.value);
123
- }
124
- });
125
- for (let dependency of dependencies) {
126
- let entryBundle = bundleGraph.getReferencedBundle(dependency, bundle);
127
- if ((entryBundle === null || entryBundle === void 0 ? void 0 : entryBundle.bundleBehavior) !== 'inline') {
128
- continue;
129
- }
130
- let packagedBundle = await getInlineBundleContents(entryBundle, bundleGraph);
131
- let packagedContents = (packagedBundle.contents instanceof _stream().Readable ? await (0, _.bufferStream)(packagedBundle.contents) : packagedBundle.contents).toString();
132
- let inlineType = (0, _nullthrows().default)(entryBundle.getMainEntry()).meta.inlineType;
133
- if (inlineType == null || inlineType === 'string') {
134
- var _dependency$meta2;
135
- let placeholder = ((_dependency$meta2 = dependency.meta) === null || _dependency$meta2 === void 0 ? void 0 : _dependency$meta2.placeholder) ?? dependency.id;
136
- (0, _assert().default)(typeof placeholder === 'string');
137
- replacements.set(placeholder, getInlineReplacement(dependency, inlineType, packagedContents));
138
- }
139
- }
140
- return performReplacement(replacements, contents, map);
141
- }
142
- function getURLReplacement({
143
- dependency,
144
- fromBundle,
145
- toBundle,
146
- relative,
147
- getReplacement
148
- }) {
149
- var _dependency$meta3;
150
- let to;
151
- let orig = _url().default.parse(dependency.specifier);
152
- if (relative) {
153
- to = _url().default.format({
154
- pathname: (0, _.relativeBundlePath)(fromBundle, toBundle, {
155
- leadingDotSlash: false
156
- }),
157
- hash: orig.hash
158
- });
159
-
160
- // If the resulting path includes a colon character and doesn't start with a ./ or ../
161
- // we need to add one so that the first part before the colon isn't parsed as a URL protocol.
162
- if (to.includes(':') && !to.startsWith('./') && !to.startsWith('../')) {
163
- to = './' + to;
164
- }
165
- } else {
166
- to = (0, _.urlJoin)(toBundle.target.publicUrl, _url().default.format({
167
- pathname: (0, _nullthrows().default)(toBundle.name),
168
- hash: orig.hash
169
- }));
170
- }
171
- let placeholder = ((_dependency$meta3 = dependency.meta) === null || _dependency$meta3 === void 0 ? void 0 : _dependency$meta3.placeholder) ?? dependency.id;
172
- (0, _assert().default)(typeof placeholder === 'string');
173
- return {
174
- from: placeholder,
175
- to: getReplacement ? getReplacement(to) : to
176
- };
177
- }
178
- function performReplacement(replacements, contents, map) {
179
- let finalContents = contents;
180
- if ((0, _featureFlags().getFeatureFlag)('inlineStringReplacementPerf')) {
181
- let replacementList = Array.from(replacements.values());
182
- if (replacementList.length > 0) {
183
- finalContents = (0, _rust().performStringReplacements)(contents, replacementList);
184
- }
185
- } else {
186
- for (let {
187
- from,
188
- to
189
- } of replacements.values()) {
190
- // Perform replacement
191
- finalContents = finalContents.split(from).join(to);
192
- }
193
- }
194
- return {
195
- contents: finalContents,
196
- // TODO: Update sourcemap with adjusted contents
197
- map
198
- };
199
- }
package/lib/schema.js DELETED
@@ -1,336 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- exports.fuzzySearch = fuzzySearch;
8
- function _diagnostic() {
9
- const data = _interopRequireWildcard(require("@atlaspack/diagnostic"));
10
- _diagnostic = function () {
11
- return data;
12
- };
13
- return data;
14
- }
15
- function _nullthrows() {
16
- const data = _interopRequireDefault(require("nullthrows"));
17
- _nullthrows = function () {
18
- return data;
19
- };
20
- return data;
21
- }
22
- function levenshtein() {
23
- const data = _interopRequireWildcard(require("fastest-levenshtein"));
24
- levenshtein = function () {
25
- return data;
26
- };
27
- return data;
28
- }
29
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
31
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
32
- function validateSchema(schema, data) {
33
- function walk(schemaAncestors, dataNode, dataPath) {
34
- let [schemaNode] = schemaAncestors;
35
- if (schemaNode.type) {
36
- let type = Array.isArray(dataNode) ? 'array' : typeof dataNode;
37
- if (schemaNode.type !== type) {
38
- return {
39
- type: 'type',
40
- dataType: 'value',
41
- dataPath,
42
- expectedTypes: [schemaNode.type],
43
- ancestors: schemaAncestors,
44
- prettyType: schemaNode.__type
45
- };
46
- } else {
47
- switch (schemaNode.type) {
48
- case 'array':
49
- {
50
- if (schemaNode.items) {
51
- let results = [];
52
- // $FlowFixMe type was already checked
53
- for (let i = 0; i < dataNode.length; i++) {
54
- let result = walk([schemaNode.items].concat(schemaAncestors),
55
- // $FlowFixMe type was already checked
56
- dataNode[i], dataPath + '/' + i);
57
- if (result) results.push(result);
58
- }
59
- if (results.length) return results.reduce((acc, v) => acc.concat(v), []);
60
- }
61
- break;
62
- }
63
- case 'string':
64
- {
65
- // $FlowFixMe type was already checked
66
- let value = dataNode;
67
- if (schemaNode.enum) {
68
- if (!schemaNode.enum.includes(value)) {
69
- return {
70
- type: 'enum',
71
- dataType: 'value',
72
- dataPath,
73
- expectedValues: schemaNode.enum,
74
- actualValue: value,
75
- ancestors: schemaAncestors
76
- };
77
- }
78
- } else if (schemaNode.__validate) {
79
- let validationError = schemaNode.__validate(value);
80
- if (typeof validationError == 'string') {
81
- return {
82
- type: 'other',
83
- dataType: 'value',
84
- dataPath,
85
- message: validationError,
86
- actualValue: value,
87
- ancestors: schemaAncestors
88
- };
89
- }
90
- }
91
- break;
92
- }
93
- case 'number':
94
- {
95
- // $FlowFixMe type was already checked
96
- let value = dataNode;
97
- if (schemaNode.enum) {
98
- if (!schemaNode.enum.includes(value)) {
99
- return {
100
- type: 'enum',
101
- dataType: 'value',
102
- dataPath,
103
- expectedValues: schemaNode.enum,
104
- actualValue: value,
105
- ancestors: schemaAncestors
106
- };
107
- }
108
- }
109
- break;
110
- }
111
- case 'object':
112
- {
113
- let results = [];
114
- let invalidProps;
115
- if (schemaNode.__forbiddenProperties) {
116
- // $FlowFixMe type was already checked
117
- let keys = Object.keys(dataNode);
118
- invalidProps = schemaNode.__forbiddenProperties.filter(val => keys.includes(val));
119
- results.push(...invalidProps.map(k => ({
120
- type: 'forbidden-prop',
121
- dataPath: dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k),
122
- dataType: 'key',
123
- prop: k,
124
- expectedProps: Object.keys(schemaNode.properties),
125
- actualProps: keys,
126
- ancestors: schemaAncestors
127
- })));
128
- }
129
- if (schemaNode.required) {
130
- // $FlowFixMe type was already checked
131
- let keys = Object.keys(dataNode);
132
- let missingKeys = schemaNode.required.filter(val => !keys.includes(val));
133
- results.push(...missingKeys.map(k => ({
134
- type: 'missing-prop',
135
- dataPath,
136
- dataType: 'value',
137
- prop: k,
138
- expectedProps: schemaNode.required,
139
- actualProps: keys,
140
- ancestors: schemaAncestors
141
- })));
142
- }
143
- if (schemaNode.properties) {
144
- let {
145
- additionalProperties = true
146
- } = schemaNode;
147
- // $FlowFixMe type was already checked
148
- for (let k in dataNode) {
149
- if (invalidProps && invalidProps.includes(k)) {
150
- // Don't check type on forbidden props
151
- continue;
152
- } else if (k in schemaNode.properties) {
153
- let result = walk([schemaNode.properties[k]].concat(schemaAncestors),
154
- // $FlowFixMe type was already checked
155
- dataNode[k], dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k));
156
- if (result) results.push(result);
157
- } else {
158
- if (typeof additionalProperties === 'boolean') {
159
- if (!additionalProperties) {
160
- results.push({
161
- type: 'enum',
162
- dataType: 'key',
163
- dataPath: dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k),
164
- expectedValues: Object.keys(schemaNode.properties).filter(
165
- // $FlowFixMe type was already checked
166
- p => !(p in dataNode)),
167
- actualValue: k,
168
- ancestors: schemaAncestors,
169
- prettyType: schemaNode.__type
170
- });
171
- }
172
- } else {
173
- let result = walk([additionalProperties].concat(schemaAncestors),
174
- // $FlowFixMe type was already checked
175
- dataNode[k], dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k));
176
- if (result) results.push(result);
177
- }
178
- }
179
- }
180
- }
181
- if (results.length) return results.reduce((acc, v) => acc.concat(v), []);
182
- break;
183
- }
184
- case 'boolean':
185
- // NOOP, type was checked already
186
- break;
187
- default:
188
- throw new Error(`Unimplemented schema type ${type}?`);
189
- }
190
- }
191
- } else {
192
- if (schemaNode.enum && !schemaNode.enum.includes(dataNode)) {
193
- return {
194
- type: 'enum',
195
- dataType: 'value',
196
- dataPath: dataPath,
197
- expectedValues: schemaNode.enum,
198
- actualValue: schemaNode,
199
- ancestors: schemaAncestors
200
- };
201
- }
202
- if (schemaNode.oneOf || schemaNode.allOf) {
203
- let list = schemaNode.oneOf || schemaNode.allOf;
204
- let results = [];
205
- for (let f of list) {
206
- let result = walk([f].concat(schemaAncestors), dataNode, dataPath);
207
- if (result) results.push(result);
208
- }
209
- if (schemaNode.oneOf ? results.length == schemaNode.oneOf.length : results.length > 0) {
210
- // return the result with more values / longer key
211
- results.sort((a, b) => Array.isArray(a) || Array.isArray(b) ? Array.isArray(a) && !Array.isArray(b) ? -1 : !Array.isArray(a) && Array.isArray(b) ? 1 : Array.isArray(a) && Array.isArray(b) ? b.length - a.length : 0 : b.dataPath.length - a.dataPath.length);
212
- return results[0];
213
- }
214
- } else if (schemaNode.not) {
215
- let result = walk([schemaNode.not].concat(schemaAncestors), dataNode, dataPath);
216
- if (!result || result.length == 0) {
217
- return {
218
- type: 'other',
219
- dataPath,
220
- dataType: null,
221
- message: schemaNode.__message,
222
- actualValue: dataNode,
223
- ancestors: schemaAncestors
224
- };
225
- }
226
- }
227
- }
228
- return undefined;
229
- }
230
- let result = walk([schema], data, '');
231
- return Array.isArray(result) ? result : result ? [result] : [];
232
- }
233
- var _default = exports.default = validateSchema;
234
- function fuzzySearch(expectedValues, actualValue) {
235
- let result = expectedValues.map(exp => [exp, levenshtein().distance(exp, actualValue)]).filter(
236
- // Remove if more than half of the string would need to be changed
237
- ([, d]) => d * 2 < actualValue.length);
238
- result.sort(([, a], [, b]) => a - b);
239
- return result.map(([v]) => v);
240
- }
241
- validateSchema.diagnostic = function (schema, data, origin, message) {
242
- if ('source' in data && 'data' in data && typeof data.source !== 'string' && !data) {
243
- throw new Error('At least one of data.source and data.data must be defined!');
244
- }
245
- let object = data.map ? data.map.data :
246
- // $FlowFixMe we can assume it's a JSON object
247
- data.data ?? JSON.parse(data.source);
248
- let errors = validateSchema(schema, object);
249
- if (errors.length) {
250
- let keys = errors.map(e => {
251
- let message;
252
- if (e.type === 'enum') {
253
- let {
254
- actualValue
255
- } = e;
256
- let expectedValues = e.expectedValues.map(String);
257
- let likely = actualValue != null ? fuzzySearch(expectedValues, String(actualValue)) : [];
258
- if (likely.length > 0) {
259
- message = `Did you mean ${likely.map(v => JSON.stringify(v)).join(', ')}?`;
260
- } else if (expectedValues.length > 0) {
261
- message = `Possible values: ${expectedValues.map(v => JSON.stringify(v)).join(', ')}`;
262
- } else {
263
- message = 'Unexpected value';
264
- }
265
- } else if (e.type === 'forbidden-prop') {
266
- let {
267
- prop,
268
- expectedProps,
269
- actualProps
270
- } = e;
271
- let likely = fuzzySearch(expectedProps, prop).filter(v => !actualProps.includes(v));
272
- if (likely.length > 0) {
273
- message = `Did you mean ${likely.map(v => JSON.stringify(v)).join(', ')}?`;
274
- } else {
275
- message = 'Unexpected property';
276
- }
277
- } else if (e.type === 'missing-prop') {
278
- let {
279
- prop,
280
- actualProps
281
- } = e;
282
- let likely = fuzzySearch(actualProps, prop);
283
- if (likely.length > 0) {
284
- message = `Did you mean ${JSON.stringify(prop)}?`;
285
- e.dataPath += '/' + likely[0];
286
- e.dataType = 'key';
287
- } else {
288
- message = `Missing property ${prop}`;
289
- }
290
- } else if (e.type === 'type') {
291
- if (e.prettyType != null) {
292
- message = `Expected ${e.prettyType}`;
293
- } else {
294
- message = `Expected type ${e.expectedTypes.join(', ')}`;
295
- }
296
- } else {
297
- message = e.message;
298
- }
299
- return {
300
- key: e.dataPath,
301
- type: e.dataType,
302
- message
303
- };
304
- });
305
- let map, code;
306
- if (data.map) {
307
- map = data.map;
308
- code = data.source;
309
- } else {
310
- // $FlowFixMe we can assume that data is valid JSON
311
- map = data.source ?? JSON.stringify((0, _nullthrows().default)(data.data), 0, '\t');
312
- code = map;
313
- }
314
- let codeFrames = [{
315
- filePath: data.filePath ?? undefined,
316
- language: 'json',
317
- code,
318
- codeHighlights: (0, _diagnostic().generateJSONCodeHighlights)(map, keys.map(({
319
- key,
320
- type,
321
- message
322
- }) => ({
323
- key: (data.prependKey ?? '') + key,
324
- type: type,
325
- message: message != null ? (0, _diagnostic().escapeMarkdown)(message) : message
326
- })))
327
- }];
328
- throw new (_diagnostic().default)({
329
- diagnostic: {
330
- message: message,
331
- origin,
332
- codeFrames
333
- }
334
- });
335
- }
336
- };
@@ -1,27 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.SharedBuffer = void 0;
7
- let SharedBuffer = exports.SharedBuffer = void 0;
8
-
9
- // $FlowFixMe[prop-missing]
10
- if (process.browser) {
11
- exports.SharedBuffer = SharedBuffer = ArrayBuffer;
12
- // Safari has removed the constructor
13
- if (typeof SharedArrayBuffer !== 'undefined') {
14
- let channel = new MessageChannel();
15
- try {
16
- // Firefox might throw when sending the Buffer over a MessagePort
17
- channel.port1.postMessage(new SharedArrayBuffer(0));
18
- exports.SharedBuffer = SharedBuffer = SharedArrayBuffer;
19
- } catch (_) {
20
- // NOOP
21
- }
22
- channel.port1.close();
23
- channel.port2.close();
24
- }
25
- } else {
26
- exports.SharedBuffer = SharedBuffer = SharedArrayBuffer;
27
- }
package/lib/sourcemap.js DELETED
@@ -1,127 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.SOURCEMAP_RE = exports.SOURCEMAP_EXTENSIONS = void 0;
7
- exports.loadSourceMap = loadSourceMap;
8
- exports.loadSourceMapUrl = loadSourceMapUrl;
9
- exports.matchSourceMappingURL = matchSourceMappingURL;
10
- exports.remapSourceLocation = remapSourceLocation;
11
- function _sourceMap() {
12
- const data = _interopRequireDefault(require("@parcel/source-map"));
13
- _sourceMap = function () {
14
- return data;
15
- };
16
- return data;
17
- }
18
- function _path() {
19
- const data = _interopRequireDefault(require("path"));
20
- _path = function () {
21
- return data;
22
- };
23
- return data;
24
- }
25
- var _path2 = require("./path");
26
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
- const SOURCEMAP_RE = exports.SOURCEMAP_RE = /(?:\/\*|\/\/)\s*[@#]\s*sourceMappingURL\s*=\s*([^\s*]+)(?:\s*\*\/)?\s*$/;
28
- const DATA_URL_RE = /^data:[^;]+(?:;charset=[^;]+)?;base64,(.*)/;
29
- const SOURCEMAP_EXTENSIONS = exports.SOURCEMAP_EXTENSIONS = new Set(['css', 'es', 'es6', 'js', 'jsx', 'mjs', 'ts', 'tsx']);
30
- function matchSourceMappingURL(contents) {
31
- return contents.match(SOURCEMAP_RE);
32
- }
33
- async function loadSourceMapUrl(fs, filename, contents) {
34
- let match = matchSourceMappingURL(contents);
35
- if (match) {
36
- let url = match[1].trim();
37
- let dataURLMatch = url.match(DATA_URL_RE);
38
- let mapFilePath;
39
- if (dataURLMatch) {
40
- mapFilePath = filename;
41
- } else {
42
- mapFilePath = url.replace(/^file:\/\//, '');
43
- mapFilePath = (0, _path2.isAbsolute)(mapFilePath) ? mapFilePath : _path().default.join(_path().default.dirname(filename), mapFilePath);
44
- }
45
- return {
46
- url,
47
- filename: mapFilePath,
48
- map: JSON.parse(dataURLMatch ? Buffer.from(dataURLMatch[1], 'base64').toString() : await fs.readFile(mapFilePath, 'utf8'))
49
- };
50
- }
51
- }
52
- async function loadSourceMap(filename, contents, options) {
53
- let foundMap = await loadSourceMapUrl(options.fs, filename, contents);
54
- if (foundMap) {
55
- let mapSourceRoot = _path().default.dirname(filename);
56
- if (foundMap.map.sourceRoot && !(0, _path2.normalizeSeparators)(foundMap.map.sourceRoot).startsWith('/')) {
57
- mapSourceRoot = _path().default.join(mapSourceRoot, foundMap.map.sourceRoot);
58
- }
59
- let sourcemapInstance = new (_sourceMap().default)(options.projectRoot);
60
- sourcemapInstance.addVLQMap({
61
- ...foundMap.map,
62
- sources: foundMap.map.sources.map(s => {
63
- return _path().default.join(mapSourceRoot, s);
64
- })
65
- });
66
- return sourcemapInstance;
67
- }
68
- }
69
- function remapSourceLocation(loc, originalMap) {
70
- let {
71
- filePath,
72
- start: {
73
- line: startLine,
74
- column: startCol
75
- },
76
- end: {
77
- line: endLine,
78
- column: endCol
79
- }
80
- } = loc;
81
- let lineDiff = endLine - startLine;
82
- let colDiff = endCol - startCol;
83
- let start = originalMap.findClosestMapping(startLine, startCol - 1);
84
- let end = originalMap.findClosestMapping(endLine, endCol - 1);
85
- if (start !== null && start !== void 0 && start.original) {
86
- if (start.source) {
87
- filePath = start.source;
88
- }
89
- ({
90
- line: startLine,
91
- column: startCol
92
- } = start.original);
93
- startCol++; // source map columns are 0-based
94
- }
95
-
96
- if (end !== null && end !== void 0 && end.original) {
97
- ({
98
- line: endLine,
99
- column: endCol
100
- } = end.original);
101
- endCol++; // source map columns are 0-based
102
-
103
- if (endLine < startLine) {
104
- endLine = startLine;
105
- endCol = startCol;
106
- } else if (endLine === startLine && endCol < startCol && lineDiff === 0) {
107
- endCol = startCol + colDiff;
108
- } else if (endLine === startLine && startCol === endCol && lineDiff === 0) {
109
- // Prevent 0-length ranges
110
- endCol = startCol + 1;
111
- }
112
- } else {
113
- endLine = startLine;
114
- endCol = startCol;
115
- }
116
- return {
117
- filePath,
118
- start: {
119
- line: startLine,
120
- column: startCol
121
- },
122
- end: {
123
- line: endLine,
124
- column: endCol
125
- }
126
- };
127
- }