# webpack-sources

> Source code handling classes for webpack

Latest version **3.5.1** (published 2026-07-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install webpack-sources
pnpm add webpack-sources
yarn add webpack-sources
bun add webpack-sources
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.5.1 |
| Published | 2026-07-06 |
| First published | 2016-01-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=10.13.0 |
| Dependencies | 0 |
| Unpacked size | 178 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 275 |
| Author | Tobias Koppers @sokra |
| Maintainers | 15000621931, ev1stensberg, __hai, sokra, avivkeller, evilebottnawi, jhnns |
| Keywords | webpack, source-map |

## Links

- npm: https://www.npmjs.com/package/webpack-sources
- Repository: https://github.com/webpack/webpack-sources
- Homepage: https://github.com/webpack/webpack-sources#readme
- Issues: https://github.com/webpack/webpack-sources/issues
- npm.io page: https://npm.io/package/webpack-sources

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 3.5.1 (latest) — 2026-07-06
- 2.0.0-beta.10 (beta) — 2020-08-24
- 3.5.0 — 2026-05-22
- 3.4.1 — 2026-04-29
- 3.4.0 — 2026-04-23
- 3.3.4 — 2026-02-15
- 3.3.3 — 2025-06-20
- 3.3.2 — 2025-06-02
- 3.3.1 — 2025-06-02
- 3.3.0 — 2025-05-23
- 3.2.3 — 2022-01-11
- 3.2.2 — 2021-11-15
- 3.2.1 — 2021-09-13
- 3.2.0 — 2021-08-02
- 3.1.2 — 2021-07-30
- … 44 more at https://npm.io/package/webpack-sources/versions

## README

# webpack-sources

Contains multiple classes which represent a `Source`. A `Source` can be asked for source code, size, source map and hash.

## `Source`

Base class for all sources.

### Public methods

All methods should be considered as expensive as they may need to do computations.

#### `source`

<!-- eslint-skip -->
```typescript
Source.prototype.source() -> String | Buffer
```

Returns the represented source code as string or Buffer (for binary Sources).

#### `buffer`

<!-- eslint-skip -->
```typescript
Source.prototype.buffer() -> Buffer
```

Returns the represented source code as Buffer. Strings are converted to utf-8.

#### `buffers`

<!-- eslint-skip -->
```typescript
Source.prototype.buffers() -> Buffer[]
```

Returns the represented source code as an array of Buffers. This avoids the
intermediate `Buffer.concat` allocation performed by `buffer()` when the source
is composed of multiple children (for example `ConcatSource`). Consumers that
can accept an array of buffers (e.g. writing via `fs.createWriteStream` or
`writev`) should prefer this method for better performance.

The default implementation returns `[this.buffer()]`.

#### `size`

<!-- eslint-skip -->
```typescript
Source.prototype.size() -> Number
```

Returns the size in bytes of the represented source code.

#### `map`

<!-- eslint-skip -->
```typescript
Source.prototype.map(options?: Object) -> Object | null
```

Returns the SourceMap of the represented source code as JSON. May return `null` if no SourceMap is available.

The `options` object can contain the following keys:

- `columns: Boolean` (default `true`): If set to false the implementation may omit mappings for columns.

#### `sourceAndMap`

<!-- eslint-skip -->
```typescript
Source.prototype.sourceAndMap(options?: Object) -> {
	source: String | Buffer,
	map: Object | null
}
```

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.

See `map()` for `options`.

#### `updateHash`

<!-- eslint-skip -->
```typescript
Source.prototype.updateHash(hash: Hash) -> void
```

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)

## `RawSource`

Represents source code without SourceMap.

<!-- eslint-skip -->
```typescript
new RawSource(sourceCode: String | Buffer)
```

## `OriginalSource`

Represents source code, which is a copy of the original file.

<!-- eslint-skip -->
```typescript
new OriginalSource(
	sourceCode: String | Buffer,
	name: String
)
```

- `sourceCode`: The source code.
- `name`: The filename of the original source code.

OriginalSource tries to create column mappings if requested, by splitting the source code at typical statement borders (`;`, `{`, `}`).

## `SourceMapSource`

Represents source code with SourceMap, optionally having an additional SourceMap for the original source.

<!-- eslint-skip -->
```typescript
new SourceMapSource(
	sourceCode: String | Buffer,
	name: String,
	sourceMap: Object | String | Buffer,
	originalSource?: String | Buffer,
	innerSourceMap?: Object | String | Buffer,
	removeOriginalSource?: boolean
)
```

- `sourceCode`: The source code.
- `name`: The filename of the original source code.
- `sourceMap`: The SourceMap for the source code.
- `originalSource`: The source code of the original file. Can be omitted if the `sourceMap` already contains the original source code.
- `innerSourceMap`: The SourceMap for the `originalSource`/`name`.
- `removeOriginalSource`: Removes the source code for `name` from the final map, keeping only the deeper mappings for that file.

The `SourceMapSource` supports "identity" mappings for the `innerSourceMap`.
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`.

## `CachedSource`

Decorates a `Source` and caches returned results of `map`, `source`, `buffer`, `size` and `sourceAndMap` in memory. `updateHash` is not cached.
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.

<!-- eslint-skip -->
```typescript
new CachedSource(source: Source)
new CachedSource(source: Source | () => Source, cachedData?: CachedData)
```

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.

### Public methods

#### `getCachedData()`

Returns the cached data for passing to the constructor. All cached entries are converted to Buffers and strings are avoided.

#### `original()`

Returns the original `Source` object.

#### `originalLazy()`

Returns the original `Source` object or a function returning these.

## `PrefixSource`

Prefix every line of the decorated `Source` with a provided string.

<!-- eslint-skip -->
```typescript
new PrefixSource(
	prefix: String,
	source: Source | String | Buffer
)
```

## `ConcatSource`

Concatenate multiple `Source`s or strings to a single source.

<!-- eslint-skip -->
```typescript
new ConcatSource(
	...items?: Source | String
)
```

### Public methods

#### `add`

<!-- eslint-skip -->
```typescript
ConcatSource.prototype.add(item: Source | String)
```

Adds an item to the source.

## `ReplaceSource`

Decorates a `Source` with replacements and insertions of source code.

The `ReplaceSource` supports "identity" mappings for child source.
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.

### Public methods

#### `replace`

<!-- eslint-skip -->
```typescript
ReplaceSource.prototype.replace(
	start: Number,
	end: Number,
	replacement: String
)
```

Replaces chars from `start` (0-indexed, inclusive) to `end` (0-indexed, inclusive) with `replacement`.

Locations represents locations in the original source and are not influenced by other replacements or insertions.

#### `insert`

<!-- eslint-skip -->
```typescript
ReplaceSource.prototype.insert(
	pos: Number,
	insertion: String
)
```

Inserts the `insertion` before char `pos` (0-indexed).

Location represents location in the original source and is not influenced by other replacements or insertions.

#### `original`

Get decorated `Source`.

## `CompatSource`

Converts a Source-like object into a real Source object.

### Public methods

#### static `from`

<!-- eslint-skip -->
```typescript
CompatSource.from(sourceLike: any | Source)
```

If `sourceLike` is a real Source it returns it unmodified. Otherwise it returns it wrapped in a CompatSource.

---
_Source: https://npm.io/package/webpack-sources · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
