{"id":20831,"library":"webpack-sources","title":"webpack-sources","description":"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+.","status":"active","version":"3.4.0","language":"javascript","source_language":"en","source_url":"https://github.com/webpack/webpack-sources","tags":["javascript","webpack","source-map","typescript"],"install":[{"cmd":"npm install webpack-sources","lang":"bash","label":"npm"},{"cmd":"yarn add webpack-sources","lang":"bash","label":"yarn"},{"cmd":"pnpm add webpack-sources","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"TypeScript definitions reference Node.js Buffer type","package":"@types/node","optional":true}],"imports":[{"note":"ESM and CJS both supported; named import preferred. Note the plural 'sources'.","wrong":"const RawSource = require('webpack-sources').RawSource","symbol":"RawSource","correct":"import { RawSource } from 'webpack-sources'"},{"note":"Used to represent original source code with name.","symbol":"OriginalSource","correct":"import { OriginalSource } from 'webpack-sources'"},{"note":"For source code with existing source map.","symbol":"SourceMapSource","correct":"import { SourceMapSource } from 'webpack-sources'"},{"note":"ConcatSource is part of webpack-sources, not directly from webpack package.","wrong":"import { ConcatSource } from 'webpack'","symbol":"ConcatSource","correct":"import { ConcatSource } from 'webpack-sources'"},{"note":"Caches source/map/hash for repeated access.","symbol":"CachedSource","correct":"import { CachedSource } from 'webpack-sources'"},{"note":"Prepends a string to the wrapped source.","symbol":"PrefixSource","correct":"import { PrefixSource } from 'webpack-sources'"},{"note":"Allows targeted replacements in the source.","symbol":"ReplaceSource","correct":"import { ReplaceSource } from 'webpack-sources'"}],"quickstart":{"code":"import { RawSource, ConcatSource, OriginalSource, SourceMapSource } from 'webpack-sources';\nimport { SourceNode } from 'source-map';  // for demonstration\n\n// Create a simple RawSource\nconst raw = new RawSource('console.log(\"hello world\");');\nconsole.log('source:', raw.source());\nconsole.log('size:', raw.size());\n\n// Concat multiple sources\nconst concat = new ConcatSource();\nconcat.add(new RawSource('var x = 1;\\n'));\nconcat.add(new OriginalSource('var y = 2;\\n', 'file2.js'));\nconsole.log('concat source:', concat.source());\n\n// SourceMapSource with identity mapping\nconst rawSource = 'var a = 1;';\nconst sourceMap = {\n  version: 3,\n  file: 'file.js',\n  sources: ['file.js'],\n  mappings: 'AAAA',\n  names: [],\n  sourcesContent: [rawSource]\n};\nconst sms = new SourceMapSource(rawSource, 'file.js', sourceMap);\nconsole.log('map:', sms.map());\n\n// CachedSource for repeated reads\nimport { CachedSource } from 'webpack-sources';\nconst cached = new CachedSource(raw);\nconsole.log('cached source:', cached.source());  // identical to raw","lang":"typescript","description":"Demonstrates creating various Source types (RawSource, ConcatSource, OriginalSource, SourceMapSource, CachedSource) and accessing their source, size, and map methods."},"warnings":[{"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.","message":"Source.prototype.buffer() returns Buffer, not string","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Replace .add([source1, source2]) with separate .add(source1).add(source2) calls.","message":"ConcatSource.add() no longer accepts array; use multiple calls or spread","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Use .size() for estimate; for exact byte length, convert to Buffer and check .length.","message":"Source.prototype.size() returns number, but may not be accurate for all source types","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Consider using SourceMapSource with explicit source map if column mapping is critical.","message":"OriginalSource may not produce column mappings if source code is short or has few statement boundaries","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure provided source map has consistent mappings; otherwise identity assumption may produce incorrect map.","message":"SourceMapSource identity mapping only works when original source matches generated source for that mapping segment","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Verify the object is an instance of a Source class: `if (source instanceof Source) { source.source(); }`","cause":"Trying to call .source() on a source object that is actually a string or a raw module object from webpack compilation","error":"TypeError: (intermediate value).source is not a function"},{"fix":"Check that the source is defined before calling methods: `if (source) { source.source(); }`","cause":"Accessing .source() on a source that is undefined or null, often from webpack compilation.","error":"Cannot read properties of undefined (reading 'source')"},{"fix":"Ensure sourceMap is a valid SourceMap object (with version, sources, mappings), a JSON string, or a Buffer.","cause":"Passing an invalid type for the sourceMap parameter to SourceMapSource constructor (e.g., number or boolean).","error":"The 'sourceMap' property must be an object, string, or Buffer"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}