A webpack loader that extracts source maps from existing source files by reading their sourceMappingURL comments
npx @tessl/cli install tessl/npm-source-map-loader@5.0.0Source Map Loader is a webpack loader that extracts source maps from existing source files by reading their sourceMappingURL comments. It handles both inline source maps and those linked via URL, processing all source map data and passing it to webpack for integration into the final bundle's source map.
npm install --save-dev source-map-loaderThis package is used as a webpack loader, so it's not imported directly in application code. Instead, it's configured in your webpack configuration:
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.js$/,
enforce: "pre",
use: ["source-map-loader"],
},
],
},
};// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.js$/,
enforce: "pre",
use: ["source-map-loader"],
},
],
},
};// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.js$/,
enforce: "pre",
use: [
{
loader: "source-map-loader",
options: {
filterSourceMappingUrl: (url, resourcePath) => {
// Skip broken source maps
if (/broken-source-map\.js$/i.test(url)) {
return false;
}
// Keep source mapping URLs for specific files
if (/important-file\.js$/i.test(resourcePath)) {
return "skip";
}
return true; // Process normally
},
},
},
],
},
],
},
};The main webpack loader function that processes JavaScript files to extract source maps.
/**
* Main webpack loader function that extracts source maps from JavaScript files
* This function operates within webpack's loader context
* @param {string} input - The source code content
* @param {object} inputMap - Existing input source map (optional)
* @returns {void} - Uses webpack callback (this.async()) to return results asynchronously
*/
async function loader(input, inputMap);The loader is designed to be used in webpack's module processing pipeline. It:
sourceMappingURL comments in the input codeThe loader accepts an options object with configuration settings.
interface LoaderOptions {
/**
* Function to control sourceMappingURL comment behavior
* @param {string} sourceMappingURL - The URL from the sourceMappingURL comment
* @param {string} resourcePath - Path to the current resource being processed
* @returns {boolean | string} - Action to take for this source map
*/
filterSourceMappingUrl?: (sourceMappingURL: string, resourcePath: string) => boolean | string;
}Controls how the loader handles each sourceMappingURL comment it encounters.
/**
* Filter function for controlling sourceMappingURL behavior
* @param {string} sourceMappingURL - The URL extracted from sourceMappingURL comment
* @param {string} resourcePath - Absolute path to the file being processed
* @returns {boolean | string} - Action to take:
* - true or "consume": Process source map and remove comment (default)
* - false or "remove": Skip source map processing but remove comment
* - "skip": Skip source map processing and keep comment
*/
type FilterSourceMappingUrl = (sourceMappingURL: string, resourcePath: string) => boolean | "consume" | "remove" | "skip";Usage Examples:
// Skip processing for specific URLs
filterSourceMappingUrl: (url, resourcePath) => {
if (/broken-source-map\.js$/i.test(url)) {
return false; // Remove comment but don't process
}
return true; // Process normally
}
// Keep source mapping URLs for debugging
filterSourceMappingUrl: (url, resourcePath) => {
if (/debug-build\.js$/i.test(resourcePath)) {
return "skip"; // Keep comment and don't process
}
return "consume"; // Process and remove comment
}Source Map Loader operates as a pre-processing webpack loader with the following key components:
The loader is designed to be fault-tolerant and will emit webpack warnings (not errors) for:
This ensures that builds continue even when source maps are problematic, while still providing visibility into issues through webpack's warning system.