@srcmap/gen-mapping 0.3.3
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/README.md +77 -0
- package/index.d.ts +155 -0
- package/package.json +47 -0
- package/src/gen-mapping-core.cjs +334 -0
- package/src/gen-mapping.cjs +22 -0
- package/src/gen-mapping.mjs +29 -0
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# @srcmap/gen-mapping
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@srcmap/gen-mapping)
|
|
4
|
+
[](https://github.com/fallow-rs/srcmap/actions/workflows/ci.yml)
|
|
5
|
+
|
|
6
|
+
Drop-in replacement for [`@jridgewell/gen-mapping`](https://github.com/jridgewell/gen-mapping) powered by Rust via WebAssembly.
|
|
7
|
+
|
|
8
|
+
Same API, same types, same behavior. Swap the import and get source map generation backed by Rust under the hood.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @srcmap/gen-mapping
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
// Before:
|
|
20
|
+
// import { GenMapping, addMapping, toEncodedMap } from '@jridgewell/gen-mapping'
|
|
21
|
+
|
|
22
|
+
// After:
|
|
23
|
+
import { GenMapping, addMapping, toEncodedMap } from '@srcmap/gen-mapping'
|
|
24
|
+
|
|
25
|
+
const map = new GenMapping({ file: 'bundle.js' })
|
|
26
|
+
|
|
27
|
+
addMapping(map, {
|
|
28
|
+
generated: { line: 1, column: 0 },
|
|
29
|
+
source: 'src/app.ts',
|
|
30
|
+
original: { line: 10, column: 4 },
|
|
31
|
+
name: 'handleClick',
|
|
32
|
+
content: 'const handleClick = () => { ... }',
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
const encoded = toEncodedMap(map)
|
|
36
|
+
|
|
37
|
+
// Cleanup WASM memory (optional, also via `using` with Symbol.dispose)
|
|
38
|
+
map.free()
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## API compatibility
|
|
42
|
+
|
|
43
|
+
All exports from `@jridgewell/gen-mapping` are supported:
|
|
44
|
+
|
|
45
|
+
| Export | Description |
|
|
46
|
+
|--------|-------------|
|
|
47
|
+
| `GenMapping` | Main class — creates a new source map builder |
|
|
48
|
+
| `addMapping(map, mapping)` | Add a mapping (1-based lines, 0-based columns) |
|
|
49
|
+
| `maybeAddMapping(map, mapping)` | Add only if it differs from the previous mapping on the same line |
|
|
50
|
+
| `setSourceContent(map, source, content)` | Set source content for a source file |
|
|
51
|
+
| `setIgnore(map, source, ignore?)` | Mark a source as ignored |
|
|
52
|
+
| `toEncodedMap(map)` | Return as encoded source map (VLQ string mappings) |
|
|
53
|
+
| `toDecodedMap(map)` | Return as decoded source map (array mappings) |
|
|
54
|
+
| `allMappings(map)` | Return all mappings as `Mapping[]` |
|
|
55
|
+
| `fromMap(input)` | Construct from an existing source map |
|
|
56
|
+
|
|
57
|
+
### Line/column convention
|
|
58
|
+
|
|
59
|
+
Follows `@jridgewell/gen-mapping` conventions:
|
|
60
|
+
- `addMapping` / `maybeAddMapping`: **1-based lines**, 0-based columns
|
|
61
|
+
- Decoded mappings output: **0-based lines**, 0-based columns
|
|
62
|
+
|
|
63
|
+
## Differences from @jridgewell/gen-mapping
|
|
64
|
+
|
|
65
|
+
- **WASM memory**: Call `map.free()` when done, or use `using map = new GenMapping(...)` with `Symbol.dispose`
|
|
66
|
+
- **VLQ encoding runs in WASM**: No JS-side encoding overhead
|
|
67
|
+
|
|
68
|
+
## Part of [srcmap](https://github.com/fallow-rs/srcmap)
|
|
69
|
+
|
|
70
|
+
High-performance source map tooling written in Rust. See also:
|
|
71
|
+
- [`@srcmap/trace-mapping`](https://www.npmjs.com/package/@srcmap/trace-mapping) - Drop-in for `@jridgewell/trace-mapping` (consumer)
|
|
72
|
+
- [`@srcmap/remapping`](https://www.npmjs.com/package/@srcmap/remapping) - Drop-in for `@ampproject/remapping` (composition)
|
|
73
|
+
- [`@srcmap/generator-wasm`](https://www.npmjs.com/package/@srcmap/generator-wasm) - Lower-level WASM generator API
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
MIT
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @srcmap/gen-mapping — drop-in replacement for @jridgewell/gen-mapping
|
|
3
|
+
* powered by Rust via WebAssembly.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// ── Segment types ────────────────────────────────────────────────
|
|
7
|
+
|
|
8
|
+
/** A segment with only a generated column (unmapped). */
|
|
9
|
+
export type UnmappedSegment = [generatedColumn: number];
|
|
10
|
+
|
|
11
|
+
/** A segment with source location but no name. */
|
|
12
|
+
export type MappedSegment = [
|
|
13
|
+
generatedColumn: number,
|
|
14
|
+
sourcesIndex: number,
|
|
15
|
+
sourceLine: number,
|
|
16
|
+
sourceColumn: number,
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
/** A segment with source location and name. */
|
|
20
|
+
export type NamedMappedSegment = [
|
|
21
|
+
generatedColumn: number,
|
|
22
|
+
sourcesIndex: number,
|
|
23
|
+
sourceLine: number,
|
|
24
|
+
sourceColumn: number,
|
|
25
|
+
namesIndex: number,
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
export type SourceMapSegment = UnmappedSegment | MappedSegment | NamedMappedSegment;
|
|
29
|
+
|
|
30
|
+
// ── Source map types ─────────────────────────────────────────────
|
|
31
|
+
|
|
32
|
+
export interface SourceMapV3 {
|
|
33
|
+
version: 3;
|
|
34
|
+
file?: string | undefined;
|
|
35
|
+
sourceRoot?: string | undefined;
|
|
36
|
+
sources: string[];
|
|
37
|
+
sourcesContent?: (string | null)[];
|
|
38
|
+
names: string[];
|
|
39
|
+
ignoreList?: number[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface EncodedSourceMap extends SourceMapV3 {
|
|
43
|
+
mappings: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface DecodedSourceMap extends SourceMapV3 {
|
|
47
|
+
mappings: SourceMapSegment[][];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ── Position types ───────────────────────────────────────────────
|
|
51
|
+
|
|
52
|
+
export interface Pos {
|
|
53
|
+
line: number;
|
|
54
|
+
column: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// ── Mapping type ─────────────────────────────────────────────────
|
|
58
|
+
|
|
59
|
+
export type Mapping =
|
|
60
|
+
| {
|
|
61
|
+
generated: Pos;
|
|
62
|
+
source: undefined;
|
|
63
|
+
original: undefined;
|
|
64
|
+
name: undefined;
|
|
65
|
+
}
|
|
66
|
+
| {
|
|
67
|
+
generated: Pos;
|
|
68
|
+
source: string;
|
|
69
|
+
original: Pos;
|
|
70
|
+
name: string;
|
|
71
|
+
}
|
|
72
|
+
| {
|
|
73
|
+
generated: Pos;
|
|
74
|
+
source: string;
|
|
75
|
+
original: Pos;
|
|
76
|
+
name: undefined;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// ── Options ──────────────────────────────────────────────────────
|
|
80
|
+
|
|
81
|
+
export interface Options {
|
|
82
|
+
file?: string | null;
|
|
83
|
+
sourceRoot?: string | null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ── Mapping input ────────────────────────────────────────────────
|
|
87
|
+
|
|
88
|
+
export interface MappingInput {
|
|
89
|
+
generated: Pos;
|
|
90
|
+
source?: string;
|
|
91
|
+
original?: Pos;
|
|
92
|
+
name?: string;
|
|
93
|
+
content?: string | null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ── GenMapping class ─────────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
export declare class GenMapping {
|
|
99
|
+
constructor(opts?: Options);
|
|
100
|
+
|
|
101
|
+
file: string | undefined;
|
|
102
|
+
sourceRoot: string | undefined;
|
|
103
|
+
|
|
104
|
+
free(): void;
|
|
105
|
+
[Symbol.dispose](): void;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// ── Free functions ───────────────────────────────────────────────
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Add a mapping to the source map.
|
|
112
|
+
* Lines are 1-based, columns are 0-based.
|
|
113
|
+
*/
|
|
114
|
+
export declare function addMapping(map: GenMapping, mapping: MappingInput): void;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Add a mapping only if it differs from the previous mapping on the same line.
|
|
118
|
+
* Requires mappings to be added in order.
|
|
119
|
+
*/
|
|
120
|
+
export declare function maybeAddMapping(map: GenMapping, mapping: MappingInput): void;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Set the source content for a source file by source name.
|
|
124
|
+
*/
|
|
125
|
+
export declare function setSourceContent(
|
|
126
|
+
map: GenMapping,
|
|
127
|
+
source: string,
|
|
128
|
+
content: string | null,
|
|
129
|
+
): void;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Mark a source as ignored (or not).
|
|
133
|
+
*/
|
|
134
|
+
export declare function setIgnore(map: GenMapping, source: string, ignore?: boolean): void;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Return all mappings as an array of Mapping objects.
|
|
138
|
+
* Lines are 1-based, columns are 0-based.
|
|
139
|
+
*/
|
|
140
|
+
export declare function allMappings(map: GenMapping): Mapping[];
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Return the source map as an encoded source map object (with VLQ string mappings).
|
|
144
|
+
*/
|
|
145
|
+
export declare function toEncodedMap(map: GenMapping): EncodedSourceMap;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Return the source map as a decoded source map object (with decoded mappings array).
|
|
149
|
+
*/
|
|
150
|
+
export declare function toDecodedMap(map: GenMapping): DecodedSourceMap;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Construct a GenMapping from an existing source map input.
|
|
154
|
+
*/
|
|
155
|
+
export declare function fromMap(input: EncodedSourceMap | DecodedSourceMap | string): GenMapping;
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@srcmap/gen-mapping",
|
|
3
|
+
"version": "0.3.3",
|
|
4
|
+
"description": "Drop-in replacement for @jridgewell/gen-mapping powered by Rust via WebAssembly",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"builder",
|
|
7
|
+
"drop-in",
|
|
8
|
+
"gen-mapping",
|
|
9
|
+
"generator",
|
|
10
|
+
"performance",
|
|
11
|
+
"source-map",
|
|
12
|
+
"sourcemap",
|
|
13
|
+
"wasm",
|
|
14
|
+
"webassembly"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/fallow-rs/srcmap.git"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"src/",
|
|
23
|
+
"index.d.ts",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"main": "src/gen-mapping.cjs",
|
|
28
|
+
"module": "src/gen-mapping.mjs",
|
|
29
|
+
"types": "index.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./index.d.ts",
|
|
33
|
+
"import": "./src/gen-mapping.mjs",
|
|
34
|
+
"require": "./src/gen-mapping.cjs",
|
|
35
|
+
"default": "./src/gen-mapping.mjs"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"test": "node --test __tests__/gen-mapping.test.mjs",
|
|
40
|
+
"test:cjs": "node --test __tests__/gen-mapping.cjs.test.cjs",
|
|
41
|
+
"test:coverage": "mkdir -p coverage && node --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info --test-reporter=spec --test-reporter-destination=stdout __tests__/gen-mapping.test.mjs"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@srcmap/generator-wasm": "0.3.3",
|
|
45
|
+
"@srcmap/sourcemap-wasm": "0.3.3"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const NO_NAME = -1;
|
|
4
|
+
|
|
5
|
+
const createGenMappingApi = ({ SourceMapGenerator, SourceMap }) => {
|
|
6
|
+
class GenMapping {
|
|
7
|
+
constructor({ file, sourceRoot } = {}) {
|
|
8
|
+
this._wasm = new SourceMapGenerator(file ?? undefined);
|
|
9
|
+
if (sourceRoot) this._wasm.setSourceRoot(sourceRoot);
|
|
10
|
+
|
|
11
|
+
this.file = file ?? undefined;
|
|
12
|
+
this.sourceRoot = sourceRoot ?? undefined;
|
|
13
|
+
|
|
14
|
+
this._sources = [];
|
|
15
|
+
this._sourceIndexMap = new Map();
|
|
16
|
+
this._names = [];
|
|
17
|
+
this._nameIndexMap = new Map();
|
|
18
|
+
this._sourcesContent = [];
|
|
19
|
+
|
|
20
|
+
this._lastLine = -1;
|
|
21
|
+
this._lastSourcesIndex = -1;
|
|
22
|
+
this._lastSourceLine = -1;
|
|
23
|
+
this._lastSourceColumn = -1;
|
|
24
|
+
this._lastNamesIndex = NO_NAME;
|
|
25
|
+
this._lastWasSourceless = false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
free() {
|
|
29
|
+
if (this._wasm) {
|
|
30
|
+
this._wasm.free();
|
|
31
|
+
this._wasm = null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
[Symbol.dispose]() {
|
|
36
|
+
this.free();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const putSource = (map, source) => {
|
|
41
|
+
const existing = map._sourceIndexMap.get(source);
|
|
42
|
+
if (existing !== undefined) return existing;
|
|
43
|
+
const idx = map._wasm.addSource(source);
|
|
44
|
+
map._sources.push(source);
|
|
45
|
+
map._sourceIndexMap.set(source, idx);
|
|
46
|
+
if (map._sourcesContent.length <= idx) {
|
|
47
|
+
map._sourcesContent[idx] = null;
|
|
48
|
+
}
|
|
49
|
+
return idx;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const putName = (map, name) => {
|
|
53
|
+
const existing = map._nameIndexMap.get(name);
|
|
54
|
+
if (existing !== undefined) return existing;
|
|
55
|
+
const idx = map._wasm.addName(name);
|
|
56
|
+
map._names.push(name);
|
|
57
|
+
map._nameIndexMap.set(name, idx);
|
|
58
|
+
return idx;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const addMappingInternal = (skippable, map, mapping) => {
|
|
62
|
+
const { generated, source, original, name, content } = mapping;
|
|
63
|
+
const genLine = generated.line - 1;
|
|
64
|
+
const genColumn = generated.column;
|
|
65
|
+
|
|
66
|
+
if (!source) {
|
|
67
|
+
if (skippable) {
|
|
68
|
+
if (map._lastLine === genLine && map._lastWasSourceless) return;
|
|
69
|
+
if (map._lastLine !== genLine) {
|
|
70
|
+
map._lastLine = genLine;
|
|
71
|
+
map._lastWasSourceless = true;
|
|
72
|
+
map._lastSourcesIndex = -1;
|
|
73
|
+
map._lastSourceLine = -1;
|
|
74
|
+
map._lastSourceColumn = -1;
|
|
75
|
+
map._lastNamesIndex = NO_NAME;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
map._wasm.addGeneratedMapping(genLine, genColumn);
|
|
80
|
+
map._lastLine = genLine;
|
|
81
|
+
map._lastWasSourceless = true;
|
|
82
|
+
map._lastSourcesIndex = -1;
|
|
83
|
+
map._lastSourceLine = -1;
|
|
84
|
+
map._lastSourceColumn = -1;
|
|
85
|
+
map._lastNamesIndex = NO_NAME;
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const sourcesIndex = putSource(map, source);
|
|
90
|
+
const sourceLine = original.line - 1;
|
|
91
|
+
const sourceColumn = original.column;
|
|
92
|
+
const namesIndex = name ? putName(map, name) : NO_NAME;
|
|
93
|
+
|
|
94
|
+
if (content !== undefined && content !== null) {
|
|
95
|
+
map._sourcesContent[sourcesIndex] = content;
|
|
96
|
+
map._wasm.setSourceContent(sourcesIndex, content);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (
|
|
100
|
+
skippable &&
|
|
101
|
+
map._lastLine === genLine &&
|
|
102
|
+
!map._lastWasSourceless &&
|
|
103
|
+
sourcesIndex === map._lastSourcesIndex &&
|
|
104
|
+
sourceLine === map._lastSourceLine &&
|
|
105
|
+
sourceColumn === map._lastSourceColumn &&
|
|
106
|
+
namesIndex === map._lastNamesIndex
|
|
107
|
+
) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (namesIndex !== NO_NAME) {
|
|
112
|
+
map._wasm.addNamedMapping(
|
|
113
|
+
genLine,
|
|
114
|
+
genColumn,
|
|
115
|
+
sourcesIndex,
|
|
116
|
+
sourceLine,
|
|
117
|
+
sourceColumn,
|
|
118
|
+
namesIndex,
|
|
119
|
+
);
|
|
120
|
+
} else {
|
|
121
|
+
map._wasm.addMapping(genLine, genColumn, sourcesIndex, sourceLine, sourceColumn);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
map._lastLine = genLine;
|
|
125
|
+
map._lastWasSourceless = false;
|
|
126
|
+
map._lastSourcesIndex = sourcesIndex;
|
|
127
|
+
map._lastSourceLine = sourceLine;
|
|
128
|
+
map._lastSourceColumn = sourceColumn;
|
|
129
|
+
map._lastNamesIndex = namesIndex;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const addMapping = (map, mapping) => {
|
|
133
|
+
addMappingInternal(false, map, mapping);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const maybeAddMapping = (map, mapping) => {
|
|
137
|
+
addMappingInternal(true, map, mapping);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const setSourceContent = (map, source, content) => {
|
|
141
|
+
const idx = putSource(map, source);
|
|
142
|
+
map._sourcesContent[idx] = content;
|
|
143
|
+
if (content != null) {
|
|
144
|
+
map._wasm.setSourceContent(idx, content);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const setIgnore = (map, source, ignore = true) => {
|
|
149
|
+
if (!ignore) return;
|
|
150
|
+
const idx = putSource(map, source);
|
|
151
|
+
map._wasm.addToIgnoreList(idx);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const allMappings = (map) => {
|
|
155
|
+
const json = map._wasm.toJSON();
|
|
156
|
+
const parsed = JSON.parse(json);
|
|
157
|
+
const sm = new SourceMap(json);
|
|
158
|
+
|
|
159
|
+
try {
|
|
160
|
+
const flat = sm.allMappingsFlat();
|
|
161
|
+
const out = [];
|
|
162
|
+
const sources = parsed.sources || [];
|
|
163
|
+
const names = parsed.names || [];
|
|
164
|
+
|
|
165
|
+
for (let i = 0; i < flat.length; i += 7) {
|
|
166
|
+
const genLine = flat[i];
|
|
167
|
+
const genCol = flat[i + 1];
|
|
168
|
+
const sourceIdx = flat[i + 2];
|
|
169
|
+
const origLine = flat[i + 3];
|
|
170
|
+
const origCol = flat[i + 4];
|
|
171
|
+
const nameIdx = flat[i + 5];
|
|
172
|
+
|
|
173
|
+
const generated = { line: genLine + 1, column: genCol };
|
|
174
|
+
let source, original, name;
|
|
175
|
+
|
|
176
|
+
if (sourceIdx !== -1) {
|
|
177
|
+
source = sources[sourceIdx];
|
|
178
|
+
original = { line: origLine + 1, column: origCol };
|
|
179
|
+
if (nameIdx !== -1) {
|
|
180
|
+
name = names[nameIdx];
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
out.push({ generated, source, original, name });
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return out;
|
|
188
|
+
} finally {
|
|
189
|
+
sm.free();
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const toEncodedMap = (map) => {
|
|
194
|
+
const json = map._wasm.toJSON();
|
|
195
|
+
const parsed = JSON.parse(json);
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
version: 3,
|
|
199
|
+
file: parsed.file || undefined,
|
|
200
|
+
sourceRoot: parsed.sourceRoot || undefined,
|
|
201
|
+
sources: parsed.sources || [],
|
|
202
|
+
sourcesContent:
|
|
203
|
+
parsed.sourcesContent || map._sourcesContent.slice(0, (parsed.sources || []).length),
|
|
204
|
+
names: parsed.names || [],
|
|
205
|
+
mappings: parsed.mappings || "",
|
|
206
|
+
ignoreList: parsed.ignoreList || [],
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const toDecodedMap = (map) => {
|
|
211
|
+
const json = map._wasm.toJSON();
|
|
212
|
+
const parsed = JSON.parse(json);
|
|
213
|
+
const sm = new SourceMap(json);
|
|
214
|
+
|
|
215
|
+
try {
|
|
216
|
+
const flat = sm.allMappingsFlat();
|
|
217
|
+
const lineCount = sm.lineCount;
|
|
218
|
+
const decoded = [];
|
|
219
|
+
|
|
220
|
+
for (let i = 0; i < lineCount; i++) {
|
|
221
|
+
decoded.push([]);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
for (let i = 0; i < flat.length; i += 7) {
|
|
225
|
+
const genLine = flat[i];
|
|
226
|
+
const genCol = flat[i + 1];
|
|
227
|
+
const sourceIdx = flat[i + 2];
|
|
228
|
+
const origLine = flat[i + 3];
|
|
229
|
+
const origCol = flat[i + 4];
|
|
230
|
+
const nameIdx = flat[i + 5];
|
|
231
|
+
|
|
232
|
+
while (decoded.length <= genLine) decoded.push([]);
|
|
233
|
+
|
|
234
|
+
if (sourceIdx === -1) {
|
|
235
|
+
decoded[genLine].push([genCol]);
|
|
236
|
+
} else if (nameIdx === -1) {
|
|
237
|
+
decoded[genLine].push([genCol, sourceIdx, origLine, origCol]);
|
|
238
|
+
} else {
|
|
239
|
+
decoded[genLine].push([genCol, sourceIdx, origLine, origCol, nameIdx]);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
while (decoded.length > 0 && decoded[decoded.length - 1].length === 0) {
|
|
244
|
+
decoded.pop();
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return {
|
|
248
|
+
version: 3,
|
|
249
|
+
file: parsed.file || undefined,
|
|
250
|
+
sourceRoot: parsed.sourceRoot || undefined,
|
|
251
|
+
sources: parsed.sources || [],
|
|
252
|
+
sourcesContent:
|
|
253
|
+
parsed.sourcesContent || map._sourcesContent.slice(0, (parsed.sources || []).length),
|
|
254
|
+
names: parsed.names || [],
|
|
255
|
+
mappings: decoded,
|
|
256
|
+
ignoreList: parsed.ignoreList || [],
|
|
257
|
+
};
|
|
258
|
+
} finally {
|
|
259
|
+
sm.free();
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const fromMap = (input) => {
|
|
264
|
+
const parsed = typeof input === "string" ? JSON.parse(input) : input;
|
|
265
|
+
const gen = new GenMapping({
|
|
266
|
+
file: parsed.file,
|
|
267
|
+
sourceRoot: parsed.sourceRoot,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
const sources = parsed.sources || [];
|
|
271
|
+
const names = parsed.names || [];
|
|
272
|
+
const sourcesContent = parsed.sourcesContent || [];
|
|
273
|
+
|
|
274
|
+
for (let i = 0; i < sources.length; i++) {
|
|
275
|
+
putSource(gen, sources[i]);
|
|
276
|
+
if (sourcesContent[i] != null) {
|
|
277
|
+
gen._sourcesContent[i] = sourcesContent[i];
|
|
278
|
+
gen._wasm.setSourceContent(i, sourcesContent[i]);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
for (let i = 0; i < names.length; i++) {
|
|
283
|
+
putName(gen, names[i]);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const json = typeof input === "string" ? input : JSON.stringify(input);
|
|
287
|
+
const sm = new SourceMap(json);
|
|
288
|
+
|
|
289
|
+
try {
|
|
290
|
+
const flat = sm.allMappingsFlat();
|
|
291
|
+
|
|
292
|
+
for (let i = 0; i < flat.length; i += 7) {
|
|
293
|
+
const genLine = flat[i];
|
|
294
|
+
const genCol = flat[i + 1];
|
|
295
|
+
const sourceIdx = flat[i + 2];
|
|
296
|
+
const origLine = flat[i + 3];
|
|
297
|
+
const origCol = flat[i + 4];
|
|
298
|
+
const nameIdx = flat[i + 5];
|
|
299
|
+
|
|
300
|
+
if (sourceIdx === -1) {
|
|
301
|
+
gen._wasm.addGeneratedMapping(genLine, genCol);
|
|
302
|
+
} else if (nameIdx === -1) {
|
|
303
|
+
gen._wasm.addMapping(genLine, genCol, sourceIdx, origLine, origCol);
|
|
304
|
+
} else {
|
|
305
|
+
gen._wasm.addNamedMapping(genLine, genCol, sourceIdx, origLine, origCol, nameIdx);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
} finally {
|
|
309
|
+
sm.free();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (parsed.ignoreList) {
|
|
313
|
+
for (const idx of parsed.ignoreList) {
|
|
314
|
+
gen._wasm.addToIgnoreList(idx);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return gen;
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
return {
|
|
322
|
+
GenMapping,
|
|
323
|
+
addMapping,
|
|
324
|
+
maybeAddMapping,
|
|
325
|
+
setSourceContent,
|
|
326
|
+
setIgnore,
|
|
327
|
+
allMappings,
|
|
328
|
+
toEncodedMap,
|
|
329
|
+
toDecodedMap,
|
|
330
|
+
fromMap,
|
|
331
|
+
};
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
module.exports = { createGenMappingApi };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { createGenMappingApi } = require("./gen-mapping-core.cjs");
|
|
4
|
+
|
|
5
|
+
let SourceMapGenerator;
|
|
6
|
+
try {
|
|
7
|
+
SourceMapGenerator = require("@srcmap/generator-wasm").SourceMapGenerator;
|
|
8
|
+
} catch {
|
|
9
|
+
// fallow-ignore-next-line unresolved-import
|
|
10
|
+
const GeneratedSourceMapGenerator =
|
|
11
|
+
require("../../generator-wasm/pkg/srcmap_generator_wasm.js").SourceMapGenerator;
|
|
12
|
+
SourceMapGenerator = GeneratedSourceMapGenerator;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let SourceMap;
|
|
16
|
+
try {
|
|
17
|
+
SourceMap = require("@srcmap/sourcemap-wasm").SourceMap;
|
|
18
|
+
} catch {
|
|
19
|
+
// fallow-ignore-next-line unresolved-import
|
|
20
|
+
SourceMap = require("../../sourcemap-wasm/pkg/srcmap_sourcemap_wasm.js").SourceMap;
|
|
21
|
+
}
|
|
22
|
+
module.exports = createGenMappingApi({ SourceMapGenerator, SourceMap });
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
|
|
3
|
+
const require = createRequire(import.meta.url);
|
|
4
|
+
const { createGenMappingApi } = require("./gen-mapping-core.cjs");
|
|
5
|
+
const { SourceMapGenerator } = require("@srcmap/generator-wasm");
|
|
6
|
+
const { SourceMap } = require("@srcmap/sourcemap-wasm");
|
|
7
|
+
const {
|
|
8
|
+
GenMapping,
|
|
9
|
+
addMapping,
|
|
10
|
+
maybeAddMapping,
|
|
11
|
+
setSourceContent,
|
|
12
|
+
setIgnore,
|
|
13
|
+
allMappings,
|
|
14
|
+
toEncodedMap,
|
|
15
|
+
toDecodedMap,
|
|
16
|
+
fromMap,
|
|
17
|
+
} = createGenMappingApi({ SourceMapGenerator, SourceMap });
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
GenMapping,
|
|
21
|
+
addMapping,
|
|
22
|
+
maybeAddMapping,
|
|
23
|
+
setSourceContent,
|
|
24
|
+
setIgnore,
|
|
25
|
+
allMappings,
|
|
26
|
+
toEncodedMap,
|
|
27
|
+
toDecodedMap,
|
|
28
|
+
fromMap,
|
|
29
|
+
};
|