Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

source-map-loader

webpack-contrib34.7mMIT5.0.0

extracts inlined source map and offers it to webpack

webpack

readme

npm node tests coverage discussion size

source-map-loader

Extracts source maps from existing source files (from their sourceMappingURL).

Getting Started

To begin, you'll need to install source-map-loader:

npm i -D source-map-loader

or

yarn add -D source-map-loader

or

pnpm add -D source-map-loader

Then add the plugin to your webpack config. For example:

file.js

import css from "file.css";

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        enforce: "pre",
        use: ["source-map-loader"],
      },
    ],
  },
};

The source-map-loader extracts existing source maps from all JavaScript entries. This includes both inline source maps as well as those linked via URL. All source map data is passed to webpack for processing as per a chosen source map style specified by the devtool option in webpack.config.js. This loader is especially useful when using 3rd-party libraries having their own source maps. If not extracted and processed into the source map of the webpack bundle, browsers may misinterpret source map data. source-map-loader allows webpack to maintain source map data continuity across libraries so ease of debugging is preserved. The source-map-loader will extract from any JavaScript file, including those in the node_modules directory. Be mindful in setting include and exclude rule conditions to maximize bundling performance.

And run webpack via your preferred method.

Options

Name Type Default Description
filterSourceMappingUrl {Function} undefined Allows to control SourceMappingURL behaviour

filterSourceMappingUrl

Type: Function Default: undefined

Allows you to specify the behavior of the loader for SourceMappingURL comment.

The function must return one of the values:

  • true or 'consume' - consume the source map and remove SourceMappingURL comment (default behavior)
  • false or 'remove' - do not consume the source map and remove SourceMappingURL comment
  • skip - do not consume the source map and do not remove SourceMappingURL comment

Example configuration:

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        enforce: "pre",
        use: [
          {
            loader: "source-map-loader",
            options: {
              filterSourceMappingUrl: (url, resourcePath) => {
                if (/broker-source-map-url\.js$/i.test(url)) {
                  return false;
                }

                if (/keep-source-mapping-url\.js$/i.test(resourcePath)) {
                  return "skip";
                }

                return true;
              },
            },
          },
        ],
      },
    ],
  },
};

Examples

Ignoring Warnings

To ignore warnings, you can use the following configuration:

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        enforce: "pre",
        use: ["source-map-loader"],
      },
    ],
  },
  ignoreWarnings: [/Failed to parse source map/],
};

More information about the ignoreWarnings option can be found here

Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.

CONTRIBUTING

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

5.0.0 (2024-01-15)

⚠ BREAKING CHANGES

  • minimum supported Node.js version is 18.12.0 (#230) (7fcab17)

4.0.2 (2023-12-27)

Bug Fixes

  • avoid deprecation message of abab package (#228) (9daafb3)

4.0.1 (2022-10-07)

Bug Fixes

  • logic when sourceRoot contains absolute URL (e724a1f)

4.0.0 (2022-06-13)

⚠ BREAKING CHANGES

  • minimum supported Node.js version is 14.15.0

3.0.1 (2022-01-03)

Chore

  • update source-map-js

3.0.0 (2021-05-21)

⚠ BREAKING CHANGES

  • minimum supported Node.js version is 12.13.0

2.0.2 (2021-05-17)

Bug Fixes

  • don't ignore source file when sourceContent contains null (#148) (8ec2b86)

2.0.1 (2021-02-10)

Bug Fixes

  • switch from source-map to source-map-js package (perf increase)
  • fix watch with invalid sourceURL

2.0.0 (2020-12-24)

⚠ BREAKING CHANGES

  • minimum supported webpack version is 5

1.1.3 (2020-12-04)

Bug Fixes

1.1.2 (2020-10-27)

Bug Fixes

  • do not watch unnecessary sources (23768df)

1.1.1 (2020-10-09)

Chore

  • update schema-utils

1.1.0 (2020-08-29)

Features

  • filter source mapping url (5cc3dfd)

Bug Fixes

1.0.2 (2020-08-19)

Bug Fixes

  • resolve server-relative url (30317f8)

1.0.1 (2020-06-30)

Bug Fixes

1.0.0 (2020-05-26)

⚠ BREAKING CHANGES

  • minimum supported Node.js version is 10.13
  • minimum supported webpack version is 4

Features

  • support indexed source maps (c18d1f9)
  • support charsert for Data URLs

Bug Fixes

  • absolute path for sources (b64f7d8)
  • avoid crash on big data URL source maps (7f769aa)
  • improve performance (#101) (4c39c22)
  • use webpack fs (#105) (1e785a1)
  • support file protocol
  • improve error messages
  • avoid conflicts with other source maps
  • fix compatibility with 5 version of webpack

0.2.4 (2018-08-14)

Bug Fixes

  • index: handle exception on loading invalid source maps (#67) (78ad469)
  • index: resolve source maps with root-relative paths correctly (#68) (e2fdbfd)
  • package: 5 low severity vulnerabilities (#72) (8262587)

0.2.3 (2017-10-23)

Chores

0.2.2 (2017-09-30)

Bug Fixes

  • Handle exception on loading invalid base64 source maps (#53) (38da2eb)

0.2.1 (2017-03-30)

Bug Fixes

  • Regex does not work for minified artifact of style-loader (#39) (582f8dc)

0.2.0 (2017-03-10)

Bug Fixes

  • Load source map only from last directive (#31) (eabfc7e)

Features

  • allow charset in inline-source-map support (#21) (2730ccb)