Registry / devops / webpack-sources

webpack-sources

JSON →
library3.4.0jsnpmunverified

Provides base and concrete Source classes for representing source code with optional source maps, used primarily by webpack. Current stable version is 3.4.0, released in 2025-01-06. The package is actively maintained as part of webpack ecosystem. Key classes include RawSource, OriginalSource, SourceMapSource, ConcatSource, CachedSource, and PrefixSource. Differentiators: designed for webpack's compilation pipeline, supports efficient buffer operations (buffer/buffers), hash computation, and identity mappings for source maps. It ships TypeScript types and works with Node 10.13+.

npm install webpack-sources
INSTALL
IMPORT
SIG · WEBPACK-SOURCES
W
webpack-sources
devopsjavascriptv3.4.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RawSource
import { RawSource } from 'webpack-sources'
const RawSource = require('webpack-sources').RawSource
ESM and CJS both supported; named import preferred. Note the plural 'sources'.
OriginalSource
import { OriginalSource } from 'webpack-sources'
Used to represent original source code with name.
SourceMapSource
import { SourceMapSource } from 'webpack-sources'
For source code with existing source map.
ConcatSource
import { ConcatSource } from 'webpack-sources'
import { ConcatSource } from 'webpack'
ConcatSource is part of webpack-sources, not directly from webpack package.
CachedSource
import { CachedSource } from 'webpack-sources'
Caches source/map/hash for repeated access.
PrefixSource
import { PrefixSource } from 'webpack-sources'
Prepends a string to the wrapped source.
ReplaceSource
import { ReplaceSource } from 'webpack-sources'
Allows targeted replacements in the source.

Demonstrates creating various Source types (RawSource, ConcatSource, OriginalSource, SourceMapSource, CachedSource) and accessing their source, size, and map methods.

import { RawSource, ConcatSource, OriginalSource, SourceMapSource } from 'webpack-sources'; import { SourceNode } from 'source-map'; // for demonstration // Create a simple RawSource const raw = new RawSource('console.log("hello world");'); console.log('source:', raw.source()); console.log('size:', raw.size()); // Concat multiple sources const concat = new ConcatSource(); concat.add(new RawSource('var x = 1;\n')); concat.add(new OriginalSource('var y = 2;\n', 'file2.js')); console.log('concat source:', concat.source()); // SourceMapSource with identity mapping const rawSource = 'var a = 1;'; const sourceMap = { version: 3, file: 'file.js', sources: ['file.js'], mappings: 'AAAA', names: [], sourcesContent: [rawSource] }; const sms = new SourceMapSource(rawSource, 'file.js', sourceMap); console.log('map:', sms.map()); // CachedSource for repeated reads import { CachedSource } from 'webpack-sources'; const cached = new CachedSource(raw); console.log('cached source:', cached.source()); // identical to raw
Debug
Known issues
breakingSource.prototype.buffer() returns Buffer, not string
fix
Use .source() if you need a string, or .buffer() for Buffer. Note that .buffer() returns a Buffer, which is not implicitly convertible to string.
affects: >=3.0.0
breakingConcatSource.add() no longer accepts array; use multiple calls or spread
fix
Replace .add([source1, source2]) with separate .add(source1).add(source2) calls.
affects: >=3.0.0
deprecatedSource.prototype.size() returns number, but may not be accurate for all source types
fix
Use .size() for estimate; for exact byte length, convert to Buffer and check .length.
affects: >=1.0.0
gotchaOriginalSource may not produce column mappings if source code is short or has few statement boundaries
fix
Consider using SourceMapSource with explicit source map if column mapping is critical.
affects: >=1.0.0
gotchaSourceMapSource identity mapping only works when original source matches generated source for that mapping segment
fix
Ensure provided source map has consistent mappings; otherwise identity assumption may produce incorrect map.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (intermediate value).source is not a function
Trying to call .source() on a source object that is actually a string or a raw module object from webpack compilation
fix
Verify the object is an instance of a Source class: `if (source instanceof Source) { source.source(); }`
Cannot read properties of undefined (reading 'source')
Accessing .source() on a source that is undefined or null, often from webpack compilation.
fix
Check that the source is defined before calling methods: `if (source) { source.source(); }`
The 'sourceMap' property must be an object, string, or Buffer
Passing an invalid type for the sourceMap parameter to SourceMapSource constructor (e.g., number or boolean).
fix
Ensure sourceMap is a valid SourceMap object (with version, sources, mappings), a JSON string, or a Buffer.
Upgrade
Version history
3.4.0latest on npm
Audit
Dependencies
@types/nodeoptionalTypeScript definitions reference Node.js Buffer type
Agent activity
9 hits · last 30 days
node
8
Amazon
1
Resources
webpack-sources — npm install webpack-sources · libregistry