UNPKG

6.04 kBMarkdownView Raw
1# webpack-sources
2
3Contains multiple classes which represent a `Source`. A `Source` can be asked for source code, size, source map and hash.
4
5## `Source`
6
7Base class for all sources.
8
9### Public methods
10
11All methods should be considered as expensive as they may need to do computations.
12
13#### `source`
14
15```typescript
16Source.prototype.source() -> String | Buffer
17```
18
19Returns the represented source code as string or Buffer (for binary Sources).
20
21#### `buffer`
22
23```typescript
24Source.prototype.buffer() -> Buffer
25```
26
27Returns the represented source code as Buffer. Strings are converted to utf-8.
28
29#### `size`
30
31```typescript
32Source.prototype.size() -> Number
33```
34
35Returns the size in bytes of the represented source code.
36
37#### `map`
38
39```typescript
40Source.prototype.map(options?: Object) -> Object | null
41```
42
43Returns the SourceMap of the represented source code as JSON. May return `null` if no SourceMap is available.
44
45The `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
53Source.prototype.sourceAndMap(options?: Object) -> {
54 source: String | Buffer,
55 map: Object | null
56}
57```
58
59Returns 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
61See `map()` for `options`.
62
63#### `updateHash`
64
65```typescript
66Source.prototype.updateHash(hash: Hash) -> void
67```
68
69Updates 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
73Represents source code without SourceMap.
74
75```typescript
76new RawSource(sourceCode: String)
77```
78
79## `OriginalSource`
80
81Represents source code, which is a copy of the original file.
82
83```typescript
84new 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
93OriginalSource tries to create column mappings if requested, by splitting the source code at typical statement borders (`;`, `{`, `}`).
94
95## `SourceMapSource`
96
97Represents source code with SourceMap, optionally having an additional SourceMap for the original source.
98
99```typescript
100new 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
117The `SourceMapSource` supports "identity" mappings for the `innerSourceMap`.
118When 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
122Decorates a `Source` and caches returned results of `map`, `source`, `buffer`, `size` and `sourceAndMap` in memory. `updateHash` is not cached.
123It 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
126new CachedSource(source: Source)
127new CachedSource(source: Source | () => Source, cachedData?: CachedData)
128```
129
130Instead 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
136Returns the cached data for passing to the constructor. All cached entries are converted to Buffers and strings are avoided.
137
138#### `original()`
139
140Returns the original `Source` object.
141
142#### `originalLazy()`
143
144Returns the original `Source` object or a function returning these.
145
146## `PrefixSource`
147
148Prefix every line of the decorated `Source` with a provided string.
149
150```typescript
151new PrefixSource(
152 prefix: String,
153 source: Source
154)
155```
156
157## `ConcatSource`
158
159Concatenate multiple `Source`s or strings to a single source.
160
161```typescript
162new ConcatSource(
163 ...items?: Source | String
164)
165```
166
167### Public methods
168
169#### `add`
170
171```typescript
172ConcatSource.prototype.add(item: Source | String)
173```
174
175Adds an item to the source.
176
177## `ReplaceSource`
178
179Decorates a `Source` with replacements and insertions of source code.
180
181The `ReplaceSource` supports "identity" mappings for child source.
182When 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
189ReplaceSource.prototype.replace(
190 start: Number,
191 end: Number,
192 replacement: String
193)
194```
195
196Replaces chars from `start` (0-indexed, inclusive) to `end` (0-indexed, inclusive) with `replacement`.
197
198Locations represents locations in the original source and are not influenced by other replacements or insertions.
199
200#### `insert`
201
202```typescript
203ReplaceSource.prototype.insert(
204 pos: Number,
205 insertion: String
206)
207```
208
209Inserts the `insertion` before char `pos` (0-indexed).
210
211Location represents location in the original source and is not influenced by other replacements or insertions.
212
213#### `original`
214
215Get decorated `Source`.
216
217## `CompatSource`
218
219Converts a Source-like object into a real Source object.
220
221### Public methods
222
223#### static `from`
224
225```typescript
226CompatSource.from(sourceLike: any | Source)
227```
228
229If `sourceLike` is a real Source it returns it unmodified. Otherwise it returns it wrapped in a CompatSource.