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.

228 lines
5.9 KiB

3 months 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. ```typescript
  9. Source.prototype.source() -> String | Buffer
  10. ```
  11. Returns the represented source code as string or Buffer (for binary Sources).
  12. #### `buffer`
  13. ```typescript
  14. Source.prototype.buffer() -> Buffer
  15. ```
  16. Returns the represented source code as Buffer. Strings are converted to utf-8.
  17. #### `size`
  18. ```typescript
  19. Source.prototype.size() -> Number
  20. ```
  21. Returns the size in bytes of the represented source code.
  22. #### `map`
  23. ```typescript
  24. Source.prototype.map(options?: Object) -> Object | null
  25. ```
  26. Returns the SourceMap of the represented source code as JSON. May return `null` if no SourceMap is available.
  27. The `options` object can contain the following keys:
  28. - `columns: Boolean` (default `true`): If set to false the implementation may omit mappings for columns.
  29. #### `sourceAndMap`
  30. ```typescript
  31. Source.prototype.sourceAndMap(options?: Object) -> {
  32. source: String | Buffer,
  33. map: Object | null
  34. }
  35. ```
  36. 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.
  37. See `map()` for `options`.
  38. #### `updateHash`
  39. ```typescript
  40. Source.prototype.updateHash(hash: Hash) -> void
  41. ```
  42. 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)
  43. ## `RawSource`
  44. Represents source code without SourceMap.
  45. ```typescript
  46. new RawSource(sourceCode: String | Buffer)
  47. ```
  48. ## `OriginalSource`
  49. Represents source code, which is a copy of the original file.
  50. ```typescript
  51. new OriginalSource(
  52. sourceCode: String | Buffer,
  53. name: String
  54. )
  55. ```
  56. - `sourceCode`: The source code.
  57. - `name`: The filename of the original source code.
  58. OriginalSource tries to create column mappings if requested, by splitting the source code at typical statement borders (`;`, `{`, `}`).
  59. ## `SourceMapSource`
  60. Represents source code with SourceMap, optionally having an additional SourceMap for the original source.
  61. ```typescript
  62. new SourceMapSource(
  63. sourceCode: String | Buffer,
  64. name: String,
  65. sourceMap: Object | String | Buffer,
  66. originalSource?: String | Buffer,
  67. innerSourceMap?: Object | String | Buffer,
  68. removeOriginalSource?: boolean
  69. )
  70. ```
  71. - `sourceCode`: The source code.
  72. - `name`: The filename of the original source code.
  73. - `sourceMap`: The SourceMap for the source code.
  74. - `originalSource`: The source code of the original file. Can be omitted if the `sourceMap` already contains the original source code.
  75. - `innerSourceMap`: The SourceMap for the `originalSource`/`name`.
  76. - `removeOriginalSource`: Removes the source code for `name` from the final map, keeping only the deeper mappings for that file.
  77. The `SourceMapSource` supports "identity" mappings for the `innerSourceMap`.
  78. 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`.
  79. ## `CachedSource`
  80. Decorates a `Source` and caches returned results of `map`, `source`, `buffer`, `size` and `sourceAndMap` in memory. `updateHash` is not cached.
  81. It tries to reused cached results from other methods to avoid calculations, i. e. when `source` is already cached, calling `size` will get the size from the cached source, calling `sourceAndMap` will only call `map` on the wrapped Source.
  82. ```typescript
  83. new CachedSource(source: Source)
  84. new CachedSource(source: Source | () => Source, cachedData?: CachedData)
  85. ```
  86. Instead of passing a `Source` object directly one can pass an function that returns a `Source` object. The function is only called when needed and once.
  87. ### Public methods
  88. #### `getCachedData()`
  89. Returns the cached data for passing to the constructor. All cached entries are converted to Buffers and strings are avoided.
  90. #### `original()`
  91. Returns the original `Source` object.
  92. #### `originalLazy()`
  93. Returns the original `Source` object or a function returning these.
  94. ## `PrefixSource`
  95. Prefix every line of the decorated `Source` with a provided string.
  96. ```typescript
  97. new PrefixSource(
  98. prefix: String,
  99. source: Source | String | Buffer
  100. )
  101. ```
  102. ## `ConcatSource`
  103. Concatenate multiple `Source`s or strings to a single source.
  104. ```typescript
  105. new ConcatSource(
  106. ...items?: Source | String
  107. )
  108. ```
  109. ### Public methods
  110. #### `add`
  111. ```typescript
  112. ConcatSource.prototype.add(item: Source | String)
  113. ```
  114. Adds an item to the source.
  115. ## `ReplaceSource`
  116. Decorates a `Source` with replacements and insertions of source code.
  117. The `ReplaceSource` supports "identity" mappings for child source.
  118. 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.
  119. ### Public methods
  120. #### `replace`
  121. ```typescript
  122. ReplaceSource.prototype.replace(
  123. start: Number,
  124. end: Number,
  125. replacement: String
  126. )
  127. ```
  128. Replaces chars from `start` (0-indexed, inclusive) to `end` (0-indexed, inclusive) with `replacement`.
  129. Locations represents locations in the original source and are not influenced by other replacements or insertions.
  130. #### `insert`
  131. ```typescript
  132. ReplaceSource.prototype.insert(
  133. pos: Number,
  134. insertion: String
  135. )
  136. ```
  137. Inserts the `insertion` before char `pos` (0-indexed).
  138. Location represents location in the original source and is not influenced by other replacements or insertions.
  139. #### `original`
  140. Get decorated `Source`.
  141. ## `CompatSource`
  142. Converts a Source-like object into a real Source object.
  143. ### Public methods
  144. #### static `from`
  145. ```typescript
  146. CompatSource.from(sourceLike: any | Source)
  147. ```
  148. If `sourceLike` is a real Source it returns it unmodified. Otherwise it returns it wrapped in a CompatSource.