[79a0317] | 1 | # @jridgewell/source-map
|
---|
| 2 |
|
---|
| 3 | > Packages `@jridgewell/trace-mapping` and `@jridgewell/gen-mapping` into the familiar source-map API
|
---|
| 4 |
|
---|
| 5 | This isn't the full API, but it's the core functionality. This wraps
|
---|
| 6 | [@jridgewell/trace-mapping][trace-mapping] and [@jridgewell/gen-mapping][gen-mapping]
|
---|
| 7 | implementations.
|
---|
| 8 |
|
---|
| 9 | ## Installation
|
---|
| 10 |
|
---|
| 11 | ```sh
|
---|
| 12 | npm install @jridgewell/source-map
|
---|
| 13 | ```
|
---|
| 14 |
|
---|
| 15 | ## Usage
|
---|
| 16 |
|
---|
| 17 | TODO
|
---|
| 18 |
|
---|
| 19 | ### SourceMapConsumer
|
---|
| 20 |
|
---|
| 21 | ```typescript
|
---|
| 22 | import { SourceMapConsumer } from '@jridgewell/source-map';
|
---|
| 23 | const smc = new SourceMapConsumer({
|
---|
| 24 | version: 3,
|
---|
| 25 | names: ['foo'],
|
---|
| 26 | sources: ['input.js'],
|
---|
| 27 | mappings: 'AAAAA',
|
---|
| 28 | });
|
---|
| 29 | ```
|
---|
| 30 |
|
---|
| 31 | #### SourceMapConsumer.fromSourceMap(mapGenerator[, mapUrl])
|
---|
| 32 |
|
---|
| 33 | Transforms a `SourceMapGenerator` into a `SourceMapConsumer`.
|
---|
| 34 |
|
---|
| 35 | ```typescript
|
---|
| 36 | const smg = new SourceMapGenerator();
|
---|
| 37 |
|
---|
| 38 | const smc = SourceMapConsumer.fromSourceMap(map);
|
---|
| 39 | smc.originalPositionFor({ line: 1, column: 0 });
|
---|
| 40 | ```
|
---|
| 41 |
|
---|
| 42 | #### SourceMapConsumer.prototype.originalPositionFor(generatedPosition)
|
---|
| 43 |
|
---|
| 44 | ```typescript
|
---|
| 45 | const smc = new SourceMapConsumer(map);
|
---|
| 46 | smc.originalPositionFor({ line: 1, column: 0 });
|
---|
| 47 | ```
|
---|
| 48 |
|
---|
| 49 | #### SourceMapConsumer.prototype.mappings
|
---|
| 50 |
|
---|
| 51 | ```typescript
|
---|
| 52 | const smc = new SourceMapConsumer(map);
|
---|
| 53 | smc.mappings; // AAAA
|
---|
| 54 | ```
|
---|
| 55 |
|
---|
| 56 | #### SourceMapConsumer.prototype.allGeneratedPositionsFor(originalPosition)
|
---|
| 57 |
|
---|
| 58 | ```typescript
|
---|
| 59 | const smc = new SourceMapConsumer(map);
|
---|
| 60 | smc.allGeneratedpositionsfor({ line: 1, column: 5, source: "baz.ts" });
|
---|
| 61 | // [
|
---|
| 62 | // { line: 2, column: 8 }
|
---|
| 63 | // ]
|
---|
| 64 | ```
|
---|
| 65 |
|
---|
| 66 | #### SourceMapConsumer.prototype.eachMapping(callback[, context[, order]])
|
---|
| 67 |
|
---|
| 68 | > This implementation currently does not support the "order" parameter.
|
---|
| 69 | > This function can only iterate in Generated order.
|
---|
| 70 |
|
---|
| 71 | ```typescript
|
---|
| 72 | const smc = new SourceMapConsumer(map);
|
---|
| 73 | smc.eachMapping((mapping) => {
|
---|
| 74 | // { source: 'baz.ts',
|
---|
| 75 | // generatedLine: 4,
|
---|
| 76 | // generatedColumn: 5,
|
---|
| 77 | // originalLine: 4,
|
---|
| 78 | // originalColumn: 5,
|
---|
| 79 | // name: null }
|
---|
| 80 | });
|
---|
| 81 | ```
|
---|
| 82 |
|
---|
| 83 | #### SourceMapConsumer.prototype.generatedPositionFor(originalPosition)
|
---|
| 84 |
|
---|
| 85 | ```typescript
|
---|
| 86 | const smc = new SourceMapConsumer(map);
|
---|
| 87 | smc.generatedPositionFor({ line: 1, column: 5, source: "baz.ts" });
|
---|
| 88 | // { line: 2, column: 8 }
|
---|
| 89 | ```
|
---|
| 90 |
|
---|
| 91 | #### SourceMapConsumer.prototype.hasContentsOfAllSources()
|
---|
| 92 |
|
---|
| 93 | ```typescript
|
---|
| 94 | const smc = new SourceMapConsumer(map);
|
---|
| 95 | smc.hasContentsOfAllSources();
|
---|
| 96 | // true
|
---|
| 97 | ```
|
---|
| 98 |
|
---|
| 99 | #### SourceMapConsumer.prototype.sourceContentFor(source[, returnNullOnMissing])
|
---|
| 100 |
|
---|
| 101 | ```typescript
|
---|
| 102 | const smc = new SourceMapConsumer(map);
|
---|
| 103 | smc.generatedPositionFor("baz.ts");
|
---|
| 104 | // "export default ..."
|
---|
| 105 | ```
|
---|
| 106 |
|
---|
| 107 | #### SourceMapConsumer.prototype.version
|
---|
| 108 |
|
---|
| 109 | Returns the source map's version
|
---|
| 110 |
|
---|
| 111 | ### SourceMapGenerator
|
---|
| 112 |
|
---|
| 113 | ```typescript
|
---|
| 114 | import { SourceMapGenerator } from '@jridgewell/source-map';
|
---|
| 115 | const smg = new SourceMapGenerator({
|
---|
| 116 | file: 'output.js',
|
---|
| 117 | sourceRoot: 'https://example.com/',
|
---|
| 118 | });
|
---|
| 119 | ```
|
---|
| 120 |
|
---|
| 121 | #### SourceMapGenerator.fromSourceMap(map)
|
---|
| 122 |
|
---|
| 123 | Transform a `SourceMapConsumer` into a `SourceMapGenerator`.
|
---|
| 124 |
|
---|
| 125 | ```typescript
|
---|
| 126 | const smc = new SourceMapConsumer();
|
---|
| 127 | const smg = SourceMapGenerator.fromSourceMap(smc);
|
---|
| 128 | ```
|
---|
| 129 |
|
---|
| 130 | #### SourceMapGenerator.prototype.applySourceMap(sourceMapConsumer[, sourceFile[, sourceMapPath]])
|
---|
| 131 |
|
---|
| 132 | > This method is not implemented yet
|
---|
| 133 |
|
---|
| 134 | #### SourceMapGenerator.prototype.addMapping(mapping)
|
---|
| 135 |
|
---|
| 136 | ```typescript
|
---|
| 137 | const smg = new SourceMapGenerator();
|
---|
| 138 | smg.addMapping({
|
---|
| 139 | generated: { line: 1, column: 0 },
|
---|
| 140 | source: 'input.js',
|
---|
| 141 | original: { line: 1, column: 0 },
|
---|
| 142 | name: 'foo',
|
---|
| 143 | });
|
---|
| 144 | ```
|
---|
| 145 |
|
---|
| 146 | #### SourceMapGenerator.prototype.setSourceContent(sourceFile, sourceContent)
|
---|
| 147 |
|
---|
| 148 | ```typescript
|
---|
| 149 | const smg = new SourceMapGenerator();
|
---|
| 150 | smg.setSourceContent('input.js', 'foobar');
|
---|
| 151 | ```
|
---|
| 152 |
|
---|
| 153 | #### SourceMapGenerator.prototype.toJSON()
|
---|
| 154 |
|
---|
| 155 | ```typescript
|
---|
| 156 | const smg = new SourceMapGenerator();
|
---|
| 157 | smg.toJSON(); // { version: 3, names: [], sources: [], mappings: '' }
|
---|
| 158 | ```
|
---|
| 159 |
|
---|
| 160 | #### SourceMapGenerator.prototype.toString()
|
---|
| 161 |
|
---|
| 162 | ```typescript
|
---|
| 163 | const smg = new SourceMapGenerator();
|
---|
| 164 | smg.toJSON(); // "{version:3,names:[],sources:[],mappings:''}"
|
---|
| 165 | ```
|
---|
| 166 |
|
---|
| 167 | #### SourceMapGenerator.prototype.toDecodedMap()
|
---|
| 168 |
|
---|
| 169 | ```typescript
|
---|
| 170 | const smg = new SourceMapGenerator();
|
---|
| 171 | smg.toDecodedMap(); // { version: 3, names: [], sources: [], mappings: [] }
|
---|
| 172 | ```
|
---|
| 173 |
|
---|
| 174 | ## Known differences with other implementations
|
---|
| 175 |
|
---|
| 176 | This implementation has some differences with `source-map` and `source-map-js`.
|
---|
| 177 |
|
---|
| 178 | - `SourceMapConsumer.prototype.eachMapping()`
|
---|
| 179 | - Does not support the `order` argument
|
---|
| 180 | - `SourceMapGenerator.prototype.applySourceMap()`
|
---|
| 181 | - Not implemented
|
---|
| 182 |
|
---|
| 183 | [trace-mapping]: https://github.com/jridgewell/trace-mapping/
|
---|
| 184 | [gen-mapping]: https://github.com/jridgewell/gen-mapping/
|
---|