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.

230 lines
5.5 KiB

4 years ago
  1. # webpack-sources
  2. Contains multiple classes which represent a `Source`. A `Source` can be asked for source code, size, source map and hash.
  3. ## `Source`
  4. Base class for all sources.
  5. ### Public methods
  6. All methods should be considered as expensive as they may need to do computations.
  7. #### `source`
  8. ``` js
  9. Source.prototype.source() -> String | ArrayBuffer
  10. ```
  11. Returns the represented source code as string.
  12. #### `size`
  13. ``` js
  14. Source.prototype.size() -> Number
  15. ```
  16. Returns the size in chars of the represented source code.
  17. #### `map`
  18. ``` js
  19. Source.prototype.map(options: Object) -> Object | null
  20. ```
  21. Returns the SourceMap of the represented source code as JSON. May return `null` if no SourceMap is available.
  22. The `options` object can contain the following keys:
  23. * `columns: Boolean` (default `true`): If set to false the implementation may omit mappings for columns.
  24. * `module: Boolean` (default `true`): If set to false the implementation may omit inner mappings for modules.
  25. #### `sourceAndMap`
  26. ``` js
  27. Source.prototype.sourceAndMap(options: Object) -> {
  28. source: String,
  29. map: Object
  30. }
  31. ```
  32. Returns both, source code (like `Source.prototype.source()` and SourceMap (like `Source.prototype.map()`). This method could have better performance than calling `source()` and `map()` separately.
  33. See `map()` for `options`.
  34. #### `updateHash`
  35. ``` js
  36. Source.prototype.updateHash(hash: Hash) -> void
  37. ```
  38. Updates the provided `Hash` object with the content of the represented source code. (`Hash` is an object with an `update` method, which is called with string values)
  39. #### `node` (optional)
  40. ``` js
  41. Source.prototype.node(options: Object) -> SourceNode
  42. ```
  43. This is an optional method. It may be `null` if not implemented.
  44. Returns a `SourceNode` (see source-map library) for the represented source code.
  45. See `map()` for `options`.
  46. #### `listNode` (optional)
  47. ``` js
  48. Source.prototype.listNode(options: Object) -> SourceNode
  49. ```
  50. This is an optional method. It may be `null` if not implemented.
  51. Returns a `SourceListMap` (see source-list-map library) for the represented source code.
  52. See `map()` for `options`.
  53. ## `RawSource`
  54. Represents source code without SourceMap.
  55. ``` js
  56. new RawSource(sourceCode: String)
  57. ```
  58. ## `OriginalSource`
  59. Represents source code, which is a copy of the original file.
  60. ``` js
  61. new OriginalSource(
  62. sourceCode: String,
  63. name: String
  64. )
  65. ```
  66. * `sourceCode`: The source code.
  67. * `name`: The filename of the original source code.
  68. OriginalSource tries to create column mappings if requested, by splitting the source code at typical statement borders (`;`, `{`, `}`).
  69. ## `SourceMapSource`
  70. Represents source code with SourceMap, optionally having an additional SourceMap for the original source.
  71. ``` js
  72. new SourceMapSource(
  73. sourceCode: String,
  74. name: String,
  75. sourceMap: Object | String,
  76. originalSource?: String,
  77. innerSourceMap?: Object | String,
  78. removeOriginalSource?: boolean
  79. )
  80. ```
  81. * `sourceCode`: The source code.
  82. * `name`: The filename of the original source code.
  83. * `sourceMap`: The SourceMap for the source code.
  84. * `originalSource`: The source code of the original file. Can be omitted if the `sourceMap` already contains the original source code.
  85. * `innerSourceMap`: The SourceMap for the `originalSource`/`name`.
  86. * `removeOriginalSource`: Removes the source code for `name` from the final map, keeping only the deeper mappings for that file.
  87. The `SourceMapSource` supports "identity" mappings for the `innerSourceMap`.
  88. When original source matches generated source for a mapping it's assumed to be mapped char by char allowing to keep finer mappings from `sourceMap`.
  89. ## `LineToLineMappedSource`
  90. Represents source code, which is mapped line by line to the original file.
  91. ``` js
  92. new LineToLineMappedSource(
  93. sourceCode: String,
  94. name: String,
  95. originalSource: String
  96. )
  97. ```
  98. * `sourceCode`: The source code.
  99. * `name`: The filename of the original source code.
  100. * `originalSource`: The original source code.
  101. ## `CachedSource`
  102. Decorates a `Source` and caches returned results of `map`, `source`, `size` and `sourceAndMap` in memory. Every other operation is delegated to the wrapped `Source`.
  103. ``` js
  104. new CachedSource(source: Source)
  105. ```
  106. ## `PrefixSource`
  107. Prefix every line of the decorated `Source` with a provided string.
  108. ``` js
  109. new PrefixSource(
  110. prefix: String,
  111. source: Source
  112. )
  113. ```
  114. ## `ConcatSource`
  115. Concatenate multiple `Source`s or strings to a single source.
  116. ``` js
  117. new ConcatSource(
  118. ...items?: Source | String
  119. )
  120. ```
  121. ### Public methods
  122. #### `add`
  123. ``` js
  124. ConcatSource.prototype.add(item: Source | String)
  125. ```
  126. Adds an item to the source.
  127. ## `ReplaceSource`
  128. Decorates a `Source` with replacements and insertions of source code.
  129. The `ReplaceSource` supports "identity" mappings for child source.
  130. When original source matches generated source for a mapping it's assumed to be mapped char by char allowing to split mappings at replacements/insertions.
  131. ### Public methods
  132. #### `replace`
  133. ``` js
  134. ReplaceSource.prototype.replace(
  135. start: Number,
  136. end: Number,
  137. replacement: String
  138. )
  139. ```
  140. Replaces chars from `start` (0-indexed, inclusive) to `end` (0-indexed, inclusive) with `replacement`.
  141. Locations represents locations in the original source and are not influenced by other replacements or insertions.
  142. #### `insert`
  143. ``` js
  144. ReplaceSource.prototype.insert(
  145. pos: Number,
  146. insertion: String
  147. )
  148. ```
  149. Inserts the `insertion` before char `pos` (0-indexed).
  150. Location represents location in the original source and is not influenced by other replacements or insertions.
  151. #### `original`
  152. Get decorated `Source`.