@atlaspack/source-map 3.0.1-canary.4070

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.
package/dist/utils.js ADDED
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.generateInlineMap = generateInlineMap;
7
+ exports.partialVlqMapToSourceMap = partialVlqMapToSourceMap;
8
+ const path_1 = __importDefault(require("path"));
9
+ function generateInlineMap(map) {
10
+ return `data:application/json;charset=utf-8;base64,${Buffer.from(map).toString('base64')}`;
11
+ }
12
+ async function partialVlqMapToSourceMap(map, opts) {
13
+ let { fs, file, sourceRoot, inlineSources, rootDir, format = 'string' } = opts;
14
+ let resultMap = {
15
+ ...map,
16
+ sourcesContent: map.sourcesContent
17
+ ? map.sourcesContent.map((content) => {
18
+ if (content) {
19
+ return content;
20
+ }
21
+ else {
22
+ return null;
23
+ }
24
+ })
25
+ : [],
26
+ version: 3,
27
+ file,
28
+ sourceRoot,
29
+ };
30
+ if (resultMap.sourcesContent.length < resultMap.sources.length) {
31
+ for (let i = 0; i <= resultMap.sources.length - resultMap.sourcesContent.length; i++) {
32
+ resultMap.sourcesContent.push(null);
33
+ }
34
+ }
35
+ if (fs) {
36
+ resultMap.sourcesContent = await Promise.all(resultMap.sourcesContent.map(async (content, index) => {
37
+ let sourceName = map.sources[index];
38
+ // If sourceName starts with `..` it is outside rootDir, in this case we likely cannot access this file from the browser or packaged node_module
39
+ // Because of this we have to include the sourceContent to ensure you can always see the sourcecontent for each mapping.
40
+ if (!content && (inlineSources || sourceName.startsWith('..'))) {
41
+ try {
42
+ return await fs.readFile(path_1.default.resolve(rootDir || '/', sourceName), 'utf-8');
43
+ }
44
+ catch (e) {
45
+ /* empty */
46
+ }
47
+ }
48
+ return content;
49
+ }));
50
+ }
51
+ if (format === 'inline' || format === 'string') {
52
+ let stringifiedMap = JSON.stringify(resultMap);
53
+ if (format === 'inline') {
54
+ return generateInlineMap(stringifiedMap);
55
+ }
56
+ return stringifiedMap;
57
+ }
58
+ return resultMap;
59
+ }
@@ -0,0 +1,421 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.SOURCE_MAP_VERSION = void 0;
7
+ var _utils = require("./utils");
8
+ var _package = require("../package.json");
9
+ const SOURCE_MAP_VERSION = exports.SOURCE_MAP_VERSION = `atlaspack:${_package.version}`;
10
+ class SourceMap {
11
+ /**
12
+ * @private
13
+ */
14
+
15
+ /**
16
+ * @private
17
+ */
18
+
19
+ /**
20
+ * Construct a SourceMap instance
21
+ *
22
+ * @param projectRoot root directory of the project, this is to ensure all source paths are relative to this path
23
+ */
24
+ constructor(projectRoot = '/', buffer) {
25
+ this.projectRoot = projectRoot;
26
+ }
27
+
28
+ // Use this to invalidate saved buffers, we don't check versioning at all in Rust
29
+ get libraryVersion() {
30
+ return SOURCE_MAP_VERSION;
31
+ }
32
+
33
+ /**
34
+ * Generates an empty map from the provided fileName and sourceContent
35
+ *
36
+ * @param sourceName path of the source file
37
+ * @param sourceContent content of the source file
38
+ * @param lineOffset an offset that gets added to the sourceLine index of each mapping
39
+ */
40
+ static generateEmptyMap({
41
+ projectRoot,
42
+ sourceName,
43
+ sourceContent,
44
+ lineOffset = 0
45
+ }) {
46
+ throw new Error('SourceMap.generateEmptyMap() must be implemented when extending SourceMap');
47
+ }
48
+
49
+ /**
50
+ * Generates an empty map from the provided fileName and sourceContent
51
+ *
52
+ * @param sourceName path of the source file
53
+ * @param sourceContent content of the source file
54
+ * @param lineOffset an offset that gets added to the sourceLine index of each mapping
55
+ */
56
+ addEmptyMap(sourceName, sourceContent, lineOffset = 0) {
57
+ this.sourceMapInstance.addEmptyMap(sourceName, sourceContent, lineOffset);
58
+ return this;
59
+ }
60
+
61
+ /**
62
+ * Appends raw VLQ mappings to the sourcemaps
63
+ */
64
+ addVLQMap(map, lineOffset = 0, columnOffset = 0) {
65
+ throw new Error('SourceMap.addVLQMap() must be implemented when extending SourceMap');
66
+ }
67
+
68
+ /**
69
+ * Appends another sourcemap instance to this sourcemap
70
+ *
71
+ * @param buffer the sourcemap buffer that should get appended to this sourcemap
72
+ * @param lineOffset an offset that gets added to the sourceLine index of each mapping
73
+ */
74
+ addSourceMap(sourcemap, lineOffset = 0) {
75
+ throw new Error('Not implemented by child class');
76
+ }
77
+
78
+ /**
79
+ * Appends a buffer to this sourcemap
80
+ * Note: The buffer should be generated by this library
81
+ * @param buffer the sourcemap buffer that should get appended to this sourcemap
82
+ * @param lineOffset an offset that gets added to the sourceLine index of each mapping
83
+ */
84
+ addBuffer(buffer, lineOffset = 0) {
85
+ throw new Error('Not implemented by child class');
86
+ }
87
+
88
+ /**
89
+ * Appends a Mapping object to this sourcemap
90
+ * Note: line numbers start at 1 due to mozilla's source-map library
91
+ *
92
+ * @param mapping the mapping that should be appended to this sourcemap
93
+ * @param lineOffset an offset that gets added to the sourceLine index of each mapping
94
+ * @param columnOffset an offset that gets added to the sourceColumn index of each mapping
95
+ */
96
+ addIndexedMapping(mapping, lineOffset = 0, columnOffset = 0) {
97
+ // Not sure if it'll be worth it to add this back to C++, wrapping it in an array probably doesn't do that much harm in JS?
98
+ // Also we barely use this function anyway...
99
+ this.addIndexedMappings([mapping], lineOffset, columnOffset);
100
+ }
101
+ _indexedMappingsToInt32Array(mappings, lineOffset = 0, columnOffset = 0) {
102
+ // Encode all mappings into a single typed array and make one call
103
+ // to C++ instead of one for each mapping to improve performance.
104
+ let mappingBuffer = new Int32Array(mappings.length * 6);
105
+ let sources = new Map();
106
+ let names = new Map();
107
+ let i = 0;
108
+ for (let mapping of mappings) {
109
+ let hasValidOriginal = mapping.original !== undefined && typeof mapping.original.line === 'number' && !Number.isNaN(mapping.original.line) && typeof mapping.original.column === 'number' && !Number.isNaN(mapping.original.column);
110
+ mappingBuffer[i++] = mapping.generated.line + lineOffset - 1;
111
+ mappingBuffer[i++] = mapping.generated.column + columnOffset;
112
+ // @ts-expect-error TS18048
113
+ mappingBuffer[i++] = hasValidOriginal ? mapping.original.line - 1 : -1;
114
+ // @ts-expect-error TS18048
115
+ mappingBuffer[i++] = hasValidOriginal ? mapping.original.column : -1;
116
+ let sourceIndex = mapping.source ? sources.get(mapping.source) : -1;
117
+ if (sourceIndex == null && mapping.source) {
118
+ sourceIndex = this.addSource(mapping.source);
119
+ sources.set(mapping.source, sourceIndex);
120
+ }
121
+ // @ts-expect-error TS18048
122
+ mappingBuffer[i++] = sourceIndex;
123
+ let nameIndex = mapping.name ? names.get(mapping.name) : -1;
124
+ if (nameIndex == null && mapping.name) {
125
+ nameIndex = this.addName(mapping.name);
126
+ names.set(mapping.name, nameIndex);
127
+ }
128
+ // @ts-expect-error TS18048
129
+ mappingBuffer[i++] = nameIndex;
130
+ }
131
+ return mappingBuffer;
132
+ }
133
+
134
+ /**
135
+ * Appends an array of Mapping objects to this sourcemap
136
+ * This is useful when improving performance if a library provides the non-serialised mappings
137
+ *
138
+ * Note: This is only faster if they generate the serialised map lazily
139
+ * Note: line numbers start at 1 due to mozilla's source-map library
140
+ *
141
+ * @param mappings an array of mapping objects
142
+ * @param lineOffset an offset that gets added to the sourceLine index of each mapping
143
+ * @param columnOffset an offset that gets added to the sourceColumn index of each mapping
144
+ */
145
+ addIndexedMappings(mappings, lineOffset = 0, columnOffset = 0) {
146
+ let mappingBuffer = this._indexedMappingsToInt32Array(mappings, lineOffset, columnOffset);
147
+ this.sourceMapInstance.addIndexedMappings(mappingBuffer);
148
+ return this;
149
+ }
150
+
151
+ /**
152
+ * Appends a name to the sourcemap
153
+ *
154
+ * @param name the name that should be appended to the names array
155
+ * @returns the index of the added name in the names array
156
+ */
157
+ addName(name) {
158
+ return this.sourceMapInstance.addName(name);
159
+ }
160
+
161
+ /**
162
+ * Appends an array of names to the sourcemap's names array
163
+ *
164
+ * @param names an array of names to add to the sourcemap
165
+ * @returns an array of indexes of the names in the sourcemap's names array, has the same order as the provided names array
166
+ */
167
+ addNames(names) {
168
+ return names.map(n => this.addName(n));
169
+ }
170
+
171
+ /**
172
+ * Appends a source to the sourcemap's sources array
173
+ *
174
+ * @param source a filepath that should be appended to the sources array
175
+ * @returns the index of the added source filepath in the sources array
176
+ */
177
+ addSource(source) {
178
+ return this.sourceMapInstance.addSource(source);
179
+ }
180
+
181
+ /**
182
+ * Appends an array of sources to the sourcemap's sources array
183
+ *
184
+ * @param sources an array of filepaths which should sbe appended to the sources array
185
+ * @returns an array of indexes of the sources that have been added to the sourcemap, returned in the same order as provided in the argument
186
+ */
187
+ addSources(sources) {
188
+ return sources.map(s => this.addSource(s));
189
+ }
190
+
191
+ /**
192
+ * Get the index in the sources array for a certain source file filepath
193
+ *
194
+ * @param source the filepath of the source file
195
+ */
196
+ getSourceIndex(source) {
197
+ return this.sourceMapInstance.getSourceIndex(source);
198
+ }
199
+
200
+ /**
201
+ * Get the source file filepath for a certain index of the sources array
202
+ *
203
+ * @param index the index of the source in the sources array
204
+ */
205
+ getSource(index) {
206
+ return this.sourceMapInstance.getSource(index);
207
+ }
208
+
209
+ /**
210
+ * Get a list of all sources
211
+ */
212
+ getSources() {
213
+ return this.sourceMapInstance.getSources();
214
+ }
215
+
216
+ /**
217
+ * Set the sourceContent for a certain file
218
+ * this is optional and is only recommended for files that we cannot read in at the end when we serialise the sourcemap
219
+ *
220
+ * @param sourceName the path of the sourceFile
221
+ * @param sourceContent the content of the sourceFile
222
+ */
223
+ setSourceContent(sourceName, sourceContent) {
224
+ return this.sourceMapInstance.setSourceContentBySource(sourceName, sourceContent);
225
+ }
226
+
227
+ /**
228
+ * Get the content of a source file if it is inlined as part of the source-map
229
+ *
230
+ * @param sourceName filename
231
+ */
232
+ getSourceContent(sourceName) {
233
+ return this.sourceMapInstance.getSourceContentBySource(sourceName);
234
+ }
235
+
236
+ /**
237
+ * Get a list of all sources
238
+ */
239
+ getSourcesContent() {
240
+ return this.sourceMapInstance.getSourcesContent();
241
+ }
242
+
243
+ /**
244
+ * Get a map of the source and it's corresponding source content
245
+ */
246
+ getSourcesContentMap() {
247
+ let sources = this.getSources();
248
+ let sourcesContent = this.getSourcesContent();
249
+ let results = {};
250
+ for (let i = 0; i < sources.length; i++) {
251
+ results[sources[i]] = sourcesContent[i] || null;
252
+ }
253
+ return results;
254
+ }
255
+
256
+ /**
257
+ * Get the index in the names array for a certain name
258
+ *
259
+ * @param name the name you want to find the index of
260
+ */
261
+ getNameIndex(name) {
262
+ return this.sourceMapInstance.getNameIndex(name);
263
+ }
264
+
265
+ /**
266
+ * Get the name for a certain index of the names array
267
+ *
268
+ * @param index the index of the name in the names array
269
+ */
270
+ getName(index) {
271
+ return this.sourceMapInstance.getName(index);
272
+ }
273
+
274
+ /**
275
+ * Get a list of all names
276
+ */
277
+ getNames() {
278
+ return this.sourceMapInstance.getNames();
279
+ }
280
+
281
+ /**
282
+ * Get a list of all mappings
283
+ */
284
+ getMappings() {
285
+ return this.sourceMapInstance.getMappings();
286
+ }
287
+
288
+ /**
289
+ * Convert a Mapping object that uses indexes for name and source to the actual value of name and source
290
+ *
291
+ * Note: This is only used internally, should not be used externally and will probably eventually get
292
+ * handled directly in C++ for improved performance
293
+ *
294
+ * @param index the Mapping that should get converted to a string-based Mapping
295
+ */
296
+ indexedMappingToStringMapping(mapping) {
297
+ if (!mapping) return mapping;
298
+ if (mapping.source != null && mapping.source > -1) {
299
+ // @ts-expect-error TS2322
300
+ mapping.source = this.getSource(mapping.source);
301
+ }
302
+ if (mapping.name != null && mapping.name > -1) {
303
+ // @ts-expect-error TS2322
304
+ mapping.name = this.getName(mapping.name);
305
+ }
306
+
307
+ // @ts-expect-error TS2322
308
+ return mapping;
309
+ }
310
+
311
+ /**
312
+ * Remaps original positions from this map to the ones in the provided map
313
+ *
314
+ * This works by finding the closest generated mapping in the provided map
315
+ * to original mappings of this map and remapping those to be the original
316
+ * mapping of the provided map.
317
+ *
318
+ * @param buffer exported SourceMap as a buffer
319
+ */
320
+ extends(buffer) {
321
+ throw new Error('Should be implemented by extending');
322
+ }
323
+
324
+ /**
325
+ * Returns an object with mappings, sources and names
326
+ * This should only be used for tests, debugging and visualising sourcemaps
327
+ *
328
+ * Note: This is a fairly slow operation
329
+ */
330
+ getMap() {
331
+ return {
332
+ mappings: this.getMappings(),
333
+ sources: this.getSources(),
334
+ sourcesContent: this.getSourcesContent(),
335
+ names: this.getNames()
336
+ };
337
+ }
338
+
339
+ /**
340
+ * Searches through the sourcemap and returns a mapping that is close to the provided generated line and column
341
+ *
342
+ * @param line the line in the generated code (starts at 1)
343
+ * @param column the column in the generated code (starts at 0)
344
+ */
345
+ findClosestMapping(line, column) {
346
+ let mapping = this.sourceMapInstance.findClosestMapping(line - 1, column);
347
+ if (mapping) {
348
+ let v = this.indexedMappingToStringMapping(mapping);
349
+ return v;
350
+ } else {
351
+ return null;
352
+ }
353
+ }
354
+
355
+ /**
356
+ * Offset mapping lines from a certain position
357
+ *
358
+ * @param line the line in the generated code (starts at 1)
359
+ * @param lineOffset the amount of lines to offset mappings by
360
+ */
361
+ offsetLines(line, lineOffset) {
362
+ if (line < 1 || line + lineOffset < 1) {
363
+ throw new Error('Line has to be positive');
364
+ }
365
+ if (lineOffset === 0) {
366
+ return;
367
+ }
368
+ this.sourceMapInstance.offsetLines(line - 1, lineOffset);
369
+ }
370
+
371
+ /**
372
+ * Offset mapping columns from a certain position
373
+ *
374
+ * @param line the line in the generated code (starts at 1)
375
+ * @param column the column in the generated code (starts at 0)
376
+ * @param columnOffset the amount of columns to offset mappings by
377
+ */
378
+ offsetColumns(line, column, columnOffset) {
379
+ if (line < 1 || column < 0 || column + columnOffset < 0) {
380
+ throw new Error('Line and Column has to be positive');
381
+ }
382
+ if (columnOffset === 0) {
383
+ return;
384
+ }
385
+ this.sourceMapInstance.offsetColumns(line - 1, column, columnOffset);
386
+ }
387
+
388
+ /**
389
+ * Returns a buffer that represents this sourcemap, used for caching
390
+ */
391
+ toBuffer() {
392
+ return this.sourceMapInstance.toBuffer();
393
+ }
394
+
395
+ /**
396
+ * Returns a serialised map using VLQ Mappings
397
+ */
398
+ toVLQ() {
399
+ return this.sourceMapInstance.toVLQ();
400
+ }
401
+
402
+ /**
403
+ * A function that has to be called at the end of the SourceMap's lifecycle to ensure all memory and native bindings get de-allocated
404
+ */
405
+ delete() {
406
+ throw new Error('SourceMap.delete() must be implemented when extending SourceMap');
407
+ }
408
+
409
+ /**
410
+ * Returns a serialised map
411
+ *
412
+ * @param options options used for formatting the serialised map
413
+ */
414
+ stringify(options) {
415
+ return (0, _utils.partialVlqMapToSourceMap)(this.toVLQ(), {
416
+ ...options,
417
+ rootDir: this.projectRoot || options.rootDir
418
+ });
419
+ }
420
+ }
421
+ exports.default = SourceMap;
package/lib/node.js ADDED
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.init = exports.default = void 0;
7
+ var _SourceMap = _interopRequireWildcard(require("./SourceMap"));
8
+ function _rust() {
9
+ const data = require("@atlaspack/rust");
10
+ _rust = function () {
11
+ return data;
12
+ };
13
+ return data;
14
+ }
15
+ 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); }
16
+ 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; }
17
+ // Re-export types for consumers
18
+
19
+ class NodeSourceMap extends _SourceMap.default {
20
+ constructor(projectRoot = '/', buffer) {
21
+ super(projectRoot);
22
+ this.projectRoot = projectRoot;
23
+ this.sourceMapInstance = new (_rust().SourceMap)(projectRoot, buffer);
24
+ }
25
+ addVLQMap(map, lineOffset = 0, columnOffset = 0) {
26
+ let {
27
+ sourcesContent,
28
+ sources = [],
29
+ mappings,
30
+ names = []
31
+ } = map;
32
+ if (!sourcesContent) {
33
+ sourcesContent = sources.map(() => '');
34
+ } else {
35
+ sourcesContent = sourcesContent.map(content => content ? content : '');
36
+ }
37
+ this.sourceMapInstance.addVLQMap(mappings, sources, sourcesContent.map(content => content ? content : ''), names, lineOffset, columnOffset);
38
+ return this;
39
+ }
40
+ addSourceMap(sourcemap, lineOffset = 0) {
41
+ if (!(sourcemap.sourceMapInstance instanceof _rust().SourceMap)) {
42
+ throw new Error('The sourcemap provided to addSourceMap is not a valid sourcemap instance');
43
+ }
44
+ this.sourceMapInstance.addSourceMap(sourcemap.sourceMapInstance, lineOffset);
45
+ return this;
46
+ }
47
+ addBuffer(buffer, lineOffset = 0) {
48
+ let previousMap = new NodeSourceMap(this.projectRoot, buffer);
49
+ return this.addSourceMap(previousMap, lineOffset);
50
+ }
51
+ extends(input) {
52
+ // $FlowFixMe
53
+ let inputSourceMap = Buffer.isBuffer(input) ? new NodeSourceMap(this.projectRoot, input) : input;
54
+ this.sourceMapInstance.extends(inputSourceMap.sourceMapInstance);
55
+ return this;
56
+ }
57
+ getNames() {
58
+ return this.sourceMapInstance.getNames();
59
+ }
60
+ getSources() {
61
+ return this.sourceMapInstance.getSources();
62
+ }
63
+ delete() {}
64
+ static generateEmptyMap({
65
+ projectRoot,
66
+ sourceName,
67
+ sourceContent,
68
+ lineOffset = 0
69
+ }) {
70
+ let map = new NodeSourceMap(projectRoot);
71
+ map.addEmptyMap(sourceName, sourceContent, lineOffset);
72
+ return map;
73
+ }
74
+
75
+ // This function exists to ensure that source map instances from (for example) @parcel/source-map
76
+ // are not used in place of @atlaspack/source-map, as from a JS point of view this is fine, but the
77
+ // underlying buffer may be different, and can cause build time errors.
78
+ static safeToBuffer(sourceMap) {
79
+ if (sourceMap == null || sourceMap == undefined) {
80
+ return undefined;
81
+ }
82
+
83
+ // We can't use instanceof here because if we're using a resolution for @parcel/source-map,
84
+ // it will be a different instance as it'll be a "copy" of @atlaspack/source-map
85
+ if (sourceMap.libraryVersion === _SourceMap.SOURCE_MAP_VERSION) {
86
+ return sourceMap.toBuffer();
87
+ }
88
+ throw new Error('Source map is not an Atlaspack SourceMap (Expected version ' + _SourceMap.SOURCE_MAP_VERSION + ', got ' + sourceMap.libraryVersion + ')');
89
+ }
90
+ }
91
+ exports.default = NodeSourceMap;
92
+ const init = exports.init = Promise.resolve();