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