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