| 1 | # webpack-sources
|
|---|
| 2 |
|
|---|
| 3 | Contains multiple classes which represent a `Source`. A `Source` can be asked for source code, size, source map and hash.
|
|---|
| 4 |
|
|---|
| 5 | ## `Source`
|
|---|
| 6 |
|
|---|
| 7 | Base class for all sources.
|
|---|
| 8 |
|
|---|
| 9 | ### Public methods
|
|---|
| 10 |
|
|---|
| 11 | All methods should be considered as expensive as they may need to do computations.
|
|---|
| 12 |
|
|---|
| 13 | #### `source`
|
|---|
| 14 |
|
|---|
| 15 | <!-- eslint-skip -->
|
|---|
| 16 | ```typescript
|
|---|
| 17 | Source.prototype.source() -> String | Buffer
|
|---|
| 18 | ```
|
|---|
| 19 |
|
|---|
| 20 | Returns the represented source code as string or Buffer (for binary Sources).
|
|---|
| 21 |
|
|---|
| 22 | #### `buffer`
|
|---|
| 23 |
|
|---|
| 24 | <!-- eslint-skip -->
|
|---|
| 25 | ```typescript
|
|---|
| 26 | Source.prototype.buffer() -> Buffer
|
|---|
| 27 | ```
|
|---|
| 28 |
|
|---|
| 29 | Returns the represented source code as Buffer. Strings are converted to utf-8.
|
|---|
| 30 |
|
|---|
| 31 | #### `buffers`
|
|---|
| 32 |
|
|---|
| 33 | <!-- eslint-skip -->
|
|---|
| 34 | ```typescript
|
|---|
| 35 | Source.prototype.buffers() -> Buffer[]
|
|---|
| 36 | ```
|
|---|
| 37 |
|
|---|
| 38 | Returns the represented source code as an array of Buffers. This avoids the
|
|---|
| 39 | intermediate `Buffer.concat` allocation performed by `buffer()` when the source
|
|---|
| 40 | is composed of multiple children (for example `ConcatSource`). Consumers that
|
|---|
| 41 | can accept an array of buffers (e.g. writing via `fs.createWriteStream` or
|
|---|
| 42 | `writev`) should prefer this method for better performance.
|
|---|
| 43 |
|
|---|
| 44 | The default implementation returns `[this.buffer()]`.
|
|---|
| 45 |
|
|---|
| 46 | #### `size`
|
|---|
| 47 |
|
|---|
| 48 | <!-- eslint-skip -->
|
|---|
| 49 | ```typescript
|
|---|
| 50 | Source.prototype.size() -> Number
|
|---|
| 51 | ```
|
|---|
| 52 |
|
|---|
| 53 | Returns the size in bytes of the represented source code.
|
|---|
| 54 |
|
|---|
| 55 | #### `map`
|
|---|
| 56 |
|
|---|
| 57 | <!-- eslint-skip -->
|
|---|
| 58 | ```typescript
|
|---|
| 59 | Source.prototype.map(options?: Object) -> Object | null
|
|---|
| 60 | ```
|
|---|
| 61 |
|
|---|
| 62 | Returns the SourceMap of the represented source code as JSON. May return `null` if no SourceMap is available.
|
|---|
| 63 |
|
|---|
| 64 | The `options` object can contain the following keys:
|
|---|
| 65 |
|
|---|
| 66 | - `columns: Boolean` (default `true`): If set to false the implementation may omit mappings for columns.
|
|---|
| 67 |
|
|---|
| 68 | #### `sourceAndMap`
|
|---|
| 69 |
|
|---|
| 70 | <!-- eslint-skip -->
|
|---|
| 71 | ```typescript
|
|---|
| 72 | Source.prototype.sourceAndMap(options?: Object) -> {
|
|---|
| 73 | source: String | Buffer,
|
|---|
| 74 | map: Object | null
|
|---|
| 75 | }
|
|---|
| 76 | ```
|
|---|
| 77 |
|
|---|
| 78 | 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.
|
|---|
| 79 |
|
|---|
| 80 | See `map()` for `options`.
|
|---|
| 81 |
|
|---|
| 82 | #### `updateHash`
|
|---|
| 83 |
|
|---|
| 84 | <!-- eslint-skip -->
|
|---|
| 85 | ```typescript
|
|---|
| 86 | Source.prototype.updateHash(hash: Hash) -> void
|
|---|
| 87 | ```
|
|---|
| 88 |
|
|---|
| 89 | 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)
|
|---|
| 90 |
|
|---|
| 91 | ## `RawSource`
|
|---|
| 92 |
|
|---|
| 93 | Represents source code without SourceMap.
|
|---|
| 94 |
|
|---|
| 95 | <!-- eslint-skip -->
|
|---|
| 96 | ```typescript
|
|---|
| 97 | new RawSource(sourceCode: String | Buffer)
|
|---|
| 98 | ```
|
|---|
| 99 |
|
|---|
| 100 | ## `OriginalSource`
|
|---|
| 101 |
|
|---|
| 102 | Represents source code, which is a copy of the original file.
|
|---|
| 103 |
|
|---|
| 104 | <!-- eslint-skip -->
|
|---|
| 105 | ```typescript
|
|---|
| 106 | new OriginalSource(
|
|---|
| 107 | sourceCode: String | Buffer,
|
|---|
| 108 | name: String
|
|---|
| 109 | )
|
|---|
| 110 | ```
|
|---|
| 111 |
|
|---|
| 112 | - `sourceCode`: The source code.
|
|---|
| 113 | - `name`: The filename of the original source code.
|
|---|
| 114 |
|
|---|
| 115 | OriginalSource tries to create column mappings if requested, by splitting the source code at typical statement borders (`;`, `{`, `}`).
|
|---|
| 116 |
|
|---|
| 117 | ## `SourceMapSource`
|
|---|
| 118 |
|
|---|
| 119 | Represents source code with SourceMap, optionally having an additional SourceMap for the original source.
|
|---|
| 120 |
|
|---|
| 121 | <!-- eslint-skip -->
|
|---|
| 122 | ```typescript
|
|---|
| 123 | new SourceMapSource(
|
|---|
| 124 | sourceCode: String | Buffer,
|
|---|
| 125 | name: String,
|
|---|
| 126 | sourceMap: Object | String | Buffer,
|
|---|
| 127 | originalSource?: String | Buffer,
|
|---|
| 128 | innerSourceMap?: Object | String | Buffer,
|
|---|
| 129 | removeOriginalSource?: boolean
|
|---|
| 130 | )
|
|---|
| 131 | ```
|
|---|
| 132 |
|
|---|
| 133 | - `sourceCode`: The source code.
|
|---|
| 134 | - `name`: The filename of the original source code.
|
|---|
| 135 | - `sourceMap`: The SourceMap for the source code.
|
|---|
| 136 | - `originalSource`: The source code of the original file. Can be omitted if the `sourceMap` already contains the original source code.
|
|---|
| 137 | - `innerSourceMap`: The SourceMap for the `originalSource`/`name`.
|
|---|
| 138 | - `removeOriginalSource`: Removes the source code for `name` from the final map, keeping only the deeper mappings for that file.
|
|---|
| 139 |
|
|---|
| 140 | The `SourceMapSource` supports "identity" mappings for the `innerSourceMap`.
|
|---|
| 141 | 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`.
|
|---|
| 142 |
|
|---|
| 143 | ## `CachedSource`
|
|---|
| 144 |
|
|---|
| 145 | Decorates a `Source` and caches returned results of `map`, `source`, `buffer`, `size` and `sourceAndMap` in memory. `updateHash` is not cached.
|
|---|
| 146 | 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.
|
|---|
| 147 |
|
|---|
| 148 | <!-- eslint-skip -->
|
|---|
| 149 | ```typescript
|
|---|
| 150 | new CachedSource(source: Source)
|
|---|
| 151 | new CachedSource(source: Source | () => Source, cachedData?: CachedData)
|
|---|
| 152 | ```
|
|---|
| 153 |
|
|---|
| 154 | 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.
|
|---|
| 155 |
|
|---|
| 156 | ### Public methods
|
|---|
| 157 |
|
|---|
| 158 | #### `getCachedData()`
|
|---|
| 159 |
|
|---|
| 160 | Returns the cached data for passing to the constructor. All cached entries are converted to Buffers and strings are avoided.
|
|---|
| 161 |
|
|---|
| 162 | #### `original()`
|
|---|
| 163 |
|
|---|
| 164 | Returns the original `Source` object.
|
|---|
| 165 |
|
|---|
| 166 | #### `originalLazy()`
|
|---|
| 167 |
|
|---|
| 168 | Returns the original `Source` object or a function returning these.
|
|---|
| 169 |
|
|---|
| 170 | ## `PrefixSource`
|
|---|
| 171 |
|
|---|
| 172 | Prefix every line of the decorated `Source` with a provided string.
|
|---|
| 173 |
|
|---|
| 174 | <!-- eslint-skip -->
|
|---|
| 175 | ```typescript
|
|---|
| 176 | new PrefixSource(
|
|---|
| 177 | prefix: String,
|
|---|
| 178 | source: Source | String | Buffer
|
|---|
| 179 | )
|
|---|
| 180 | ```
|
|---|
| 181 |
|
|---|
| 182 | ## `ConcatSource`
|
|---|
| 183 |
|
|---|
| 184 | Concatenate multiple `Source`s or strings to a single source.
|
|---|
| 185 |
|
|---|
| 186 | <!-- eslint-skip -->
|
|---|
| 187 | ```typescript
|
|---|
| 188 | new ConcatSource(
|
|---|
| 189 | ...items?: Source | String
|
|---|
| 190 | )
|
|---|
| 191 | ```
|
|---|
| 192 |
|
|---|
| 193 | ### Public methods
|
|---|
| 194 |
|
|---|
| 195 | #### `add`
|
|---|
| 196 |
|
|---|
| 197 | <!-- eslint-skip -->
|
|---|
| 198 | ```typescript
|
|---|
| 199 | ConcatSource.prototype.add(item: Source | String)
|
|---|
| 200 | ```
|
|---|
| 201 |
|
|---|
| 202 | Adds an item to the source.
|
|---|
| 203 |
|
|---|
| 204 | ## `ReplaceSource`
|
|---|
| 205 |
|
|---|
| 206 | Decorates a `Source` with replacements and insertions of source code.
|
|---|
| 207 |
|
|---|
| 208 | The `ReplaceSource` supports "identity" mappings for child source.
|
|---|
| 209 | 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.
|
|---|
| 210 |
|
|---|
| 211 | ### Public methods
|
|---|
| 212 |
|
|---|
| 213 | #### `replace`
|
|---|
| 214 |
|
|---|
| 215 | <!-- eslint-skip -->
|
|---|
| 216 | ```typescript
|
|---|
| 217 | ReplaceSource.prototype.replace(
|
|---|
| 218 | start: Number,
|
|---|
| 219 | end: Number,
|
|---|
| 220 | replacement: String
|
|---|
| 221 | )
|
|---|
| 222 | ```
|
|---|
| 223 |
|
|---|
| 224 | Replaces chars from `start` (0-indexed, inclusive) to `end` (0-indexed, inclusive) with `replacement`.
|
|---|
| 225 |
|
|---|
| 226 | Locations represents locations in the original source and are not influenced by other replacements or insertions.
|
|---|
| 227 |
|
|---|
| 228 | #### `insert`
|
|---|
| 229 |
|
|---|
| 230 | <!-- eslint-skip -->
|
|---|
| 231 | ```typescript
|
|---|
| 232 | ReplaceSource.prototype.insert(
|
|---|
| 233 | pos: Number,
|
|---|
| 234 | insertion: String
|
|---|
| 235 | )
|
|---|
| 236 | ```
|
|---|
| 237 |
|
|---|
| 238 | Inserts the `insertion` before char `pos` (0-indexed).
|
|---|
| 239 |
|
|---|
| 240 | Location represents location in the original source and is not influenced by other replacements or insertions.
|
|---|
| 241 |
|
|---|
| 242 | #### `original`
|
|---|
| 243 |
|
|---|
| 244 | Get decorated `Source`.
|
|---|
| 245 |
|
|---|
| 246 | ## `CompatSource`
|
|---|
| 247 |
|
|---|
| 248 | Converts a Source-like object into a real Source object.
|
|---|
| 249 |
|
|---|
| 250 | ### Public methods
|
|---|
| 251 |
|
|---|
| 252 | #### static `from`
|
|---|
| 253 |
|
|---|
| 254 | <!-- eslint-skip -->
|
|---|
| 255 | ```typescript
|
|---|
| 256 | CompatSource.from(sourceLike: any | Source)
|
|---|
| 257 | ```
|
|---|
| 258 |
|
|---|
| 259 | If `sourceLike` is a real Source it returns it unmodified. Otherwise it returns it wrapped in a CompatSource.
|
|---|