UNPKG

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