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