@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/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
|
+
}
|
package/tsconfig.json
ADDED