0
# Base Source Interface
1
2
Core interface that all source representations implement, providing methods for accessing source code, size, source maps, and hash operations.
3
4
## Capabilities
5
6
### Source Class
7
8
Abstract base class that defines the common interface for all source types.
9
10
```typescript { .api }
11
/**
12
* Abstract base class for all source representations
13
* Provides core methods for accessing source content, size, and source maps
14
*/
15
abstract class Source {
16
/** Returns the represented source code as string or Buffer */
17
source(): string | Buffer;
18
/** Returns the represented source code as Buffer */
19
buffer(): Buffer;
20
/** Returns size in bytes of the represented source code */
21
size(): number;
22
/** Returns the SourceMap as JSON or null if not available */
23
map(options?: MapOptions): RawSourceMap | null;
24
/** Returns both source code and source map together */
25
sourceAndMap(options?: MapOptions): SourceAndMap;
26
/** Updates the provided Hash object with the content */
27
updateHash(hash: HashLike): void;
28
}
29
```
30
31
**Usage Example:**
32
33
```javascript
34
const { RawSource } = require("webpack-sources");
35
36
class CustomSource extends Source {
37
constructor(content) {
38
super();
39
this.content = content;
40
}
41
42
source() {
43
return this.content;
44
}
45
46
updateHash(hash) {
47
hash.update("CustomSource");
48
hash.update(this.content);
49
}
50
}
51
52
const source = new CustomSource("console.log('test');");
53
console.log(source.source()); // "console.log('test');"
54
console.log(source.size()); // 19
55
```
56
57
### Map Options
58
59
Configuration options for source map generation.
60
61
```typescript { .api }
62
/**
63
* Options for source map generation
64
*/
65
interface MapOptions {
66
/**
67
* If false, implementation may omit mappings for columns
68
* Default: true
69
*/
70
columns?: boolean;
71
72
/** Indicates if this is a module source */
73
module?: boolean;
74
}
75
```
76
77
### Source and Map Result
78
79
Combined result containing both source content and source map.
80
81
```typescript { .api }
82
/**
83
* Combined result of source content and source map
84
*/
85
interface SourceAndMap {
86
/** The source code */
87
source: string | Buffer;
88
/** The source map or null if not available */
89
map: RawSourceMap | null;
90
}
91
```
92
93
**Usage Example:**
94
95
```javascript
96
const { OriginalSource } = require("webpack-sources");
97
98
const source = new OriginalSource("const x = 1;", "example.js");
99
const result = source.sourceAndMap({ columns: true });
100
101
console.log(result.source); // "const x = 1;"
102
console.log(result.map); // SourceMap object or null
103
```
104
105
### Hash Interface
106
107
Interface for hash objects used in `updateHash` method.
108
109
```typescript { .api }
110
/**
111
* Interface for hash objects compatible with updateHash
112
*/
113
interface HashLike {
114
/** Update hash with data */
115
update(data: string | Buffer, inputEncoding?: string): HashLike;
116
/** Get hash digest */
117
digest(encoding?: string): string | Buffer;
118
}
119
```
120
121
**Usage Example:**
122
123
```javascript
124
const crypto = require("crypto");
125
const { RawSource } = require("webpack-sources");
126
127
const hash = crypto.createHash("sha256");
128
const source = new RawSource("console.log('hello');");
129
130
source.updateHash(hash);
131
const digest = hash.digest("hex");
132
console.log(digest); // Hash of the source content
133
```
134
135
### Raw Source Map
136
137
Standard source map format as defined by the Source Map specification.
138
139
```typescript { .api }
140
/**
141
* Standard source map format
142
*/
143
interface RawSourceMap {
144
/** Source map format version (typically 3) */
145
version: number;
146
/** Array of source file names */
147
sources: string[];
148
/** Array of identifier names used in mappings */
149
names: string[];
150
/** Optional root path for source files */
151
sourceRoot?: string;
152
/** Optional array of source file contents */
153
sourcesContent?: string[];
154
/** Base64 VLQ encoded mappings */
155
mappings: string;
156
/** Generated file name */
157
file: string;
158
/** Optional debug identifier */
159
debugId?: string;
160
/** Optional array of source indices to ignore */
161
ignoreList?: number[];
162
}
163
```