webpack-sources

中文文档
3.7.0发布于 2026/10/30MITTypeScriptCommonJS零依赖构建来源已验证近 30 天下载 2.5亿

Source code handling classes for webpack

原始使用文档
文档目录16 节
  1. Source
  2. Public methods
  3. RawSource
  4. OriginalSource
  5. SourceMapSource
  6. CachedSource
  7. Public methods
  8. PrefixSource
  9. ConcatSource
  10. Public methods
  11. ReplaceSource
  12. Public methods
  13. CompatSource
  14. Public methods
  15. util.sourceMap
  16. originalPositionFor

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

Source.prototype.source() -> String | Buffer

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

buffer

Source.prototype.buffer() -> Buffer

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

buffers

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

Source.prototype.size() -> Number

Returns the size in bytes of the represented source code.

map

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.
  • scopes: Boolean (default false): If set to true the map carries the scopes field of the Scopes proposal, naming for each source the bindings it declares and the expression each one reads in the generated code. Sources declare those bindings themselves — see OriginalSource — and a map whose sources declare none omits the field.

sourceAndMap

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

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.

new RawSource(sourceCode: String | Buffer)

OriginalSource

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

new OriginalSource(
    sourceCode: String | Buffer,
    name: String,
    scopeBindings?: Map<String, String>
)
  • sourceCode: The source code.
  • name: The filename of the original source code.
  • scopeBindings: What each name this source declares reads in the generated code, keyed by the name the source uses. Only consulted when a map is asked for with scopes: true, and carried through wrapping sources such as ReplaceSource and ConcatSource.

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.

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.

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.

new PrefixSource(
    prefix: String,
    source: Source | String | Buffer
)

ConcatSource

Concatenate multiple Sources or strings to a single source.

new ConcatSource(
    ...items?: Source | String
)

Public methods

add

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

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

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

CompatSource.from(sourceLike: any | Source)

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

util.sourceMap

Helpers for reading a source map.

originalPositionFor

util.sourceMap.originalPositionFor(
    sourceMap: RawSourceMap | string | Buffer,
    line: number,
    column: number,
): { source: string, line: number, column: number, name: string | null } | null

Returns where a generated position came from: the closest mapping at or before column on line, or null when nothing maps there. Lines count from one and columns from zero, on both sides. source is joined with the map's sourceRoot. It decodes the whole map, so it suits a one-off lookup such as an error location.

版本与发布记录

webpack-sources 共发布 63 个版本,首次发布于 2016/01/04;过去 12 个月发布了 9 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.7.0latest2026/10/306 天前
3.6.02026/09/271 个月前
3.5.32026/09/251 个月前
3.5.22026/09/251 个月前
3.5.12026/07/064 个月前
3.5.02026/05/225 个月前

其他发布通道:beta → 2.0.0-beta.10。

关于 webpack-sources 的常见问题

webpack-sources 是什么?

Source code handling classes for webpack

如何安装 webpack-sources?

在项目目录运行 npm install webpack-sources,或使用 pnpm add / yarn add / bun add。

webpack-sources 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

webpack-sources 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

webpack-sources 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10.13.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

webpack-sources 可以商用吗?

webpack-sources 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

webpack-sources 的下载量有多少?

根据 npm 官方下载统计,webpack-sources 在 2026/10/05 – 2026/11/03 这 30 天内被下载 248,130,860 次,日均约 8,271,029 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

webpack-sources 还在维护吗?

最新版本 3.7.0 发布于 2026/10/30(6 天前);至今共发布 63 个版本,其中过去 12 个月发布了 9 个;首次发布于 2016/01/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。