You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
4.3 KiB

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