UNPKG

5.13 kBMarkdownView Raw
1<div align="center">
2 <a href="https://github.com/webpack/webpack">
3 <img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
4 </a>
5</div>
6
7[![npm][npm]][npm-url]
8[![node][node]][node-url]
9[![tests][tests]][tests-url]
10[![coverage][cover]][cover-url]
11[![discussion][discussion]][discussion-url]
12[![size][size]][size-url]
13
14# source-map-loader
15
16Extracts source maps from existing source files (from their <code>sourceMappingURL</code>).
17
18## Getting Started
19
20To begin, you'll need to install `source-map-loader`:
21
22```console
23npm i -D source-map-loader
24```
25
26or
27
28```console
29yarn add -D source-map-loader
30```
31
32or
33
34```console
35pnpm add -D source-map-loader
36```
37
38Then add the plugin to your `webpack` config. For example:
39
40**file.js**
41
42```js
43import css from "file.css";
44```
45
46**webpack.config.js**
47
48```js
49module.exports = {
50 module: {
51 rules: [
52 {
53 test: /\.js$/,
54 enforce: "pre",
55 use: ["source-map-loader"],
56 },
57 ],
58 },
59};
60```
61
62The `source-map-loader` extracts existing source maps from all JavaScript entries.
63This includes both inline source maps as well as those linked via URL.
64All source map data is passed to webpack for processing as per a chosen [source map style](https://webpack.js.org/configuration/devtool/) specified by the `devtool` option in [webpack.config.js](https://webpack.js.org/configuration/).
65This loader is especially useful when using 3rd-party libraries having their own source maps.
66If 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.
67The `source-map-loader` will extract from any JavaScript file, including those in the `node_modules` directory.
68Be mindful in setting [include](https://webpack.js.org/configuration/module/#rule-include) and [exclude](https://webpack.js.org/configuration/module/#rule-exclude) rule conditions to maximize bundling performance.
69
70And run `webpack` via your preferred method.
71
72## Options
73
74| Name | Type | Default | Description |
75| :-----------------------------------------------------: | :----------: | :---------: | :--------------------------------------------- |
76| **[`filterSourceMappingUrl`](#filtersourcemappingurl)** | `{Function}` | `undefined` | Allows to control `SourceMappingURL` behaviour |
77
78### filterSourceMappingUrl
79
80Type: `Function`
81Default: `undefined`
82
83Allows you to specify the behavior of the loader for `SourceMappingURL` comment.
84
85The function must return one of the values:
86
87- `true` or `'consume'` - consume the source map and remove `SourceMappingURL` comment (default behavior)
88- `false` or `'remove'` - do not consume the source map and remove `SourceMappingURL` comment
89- `skip` - do not consume the source map and do not remove `SourceMappingURL` comment
90
91Example configuration:
92
93**webpack.config.js**
94
95```js
96module.exports = {
97 module: {
98 rules: [
99 {
100 test: /\.js$/,
101 enforce: "pre",
102 use: [
103 {
104 loader: "source-map-loader",
105 options: {
106 filterSourceMappingUrl: (url, resourcePath) => {
107 if (/broker-source-map-url\.js$/i.test(url)) {
108 return false;
109 }
110
111 if (/keep-source-mapping-url\.js$/i.test(resourcePath)) {
112 return "skip";
113 }
114
115 return true;
116 },
117 },
118 },
119 ],
120 },
121 ],
122 },
123};
124```
125
126## Examples
127
128### Ignoring Warnings
129
130To ignore warnings, you can use the following configuration:
131
132**webpack.config.js**
133
134```js
135module.exports = {
136 module: {
137 rules: [
138 {
139 test: /\.js$/,
140 enforce: "pre",
141 use: ["source-map-loader"],
142 },
143 ],
144 },
145 ignoreWarnings: [/Failed to parse source map/],
146};
147```
148
149More information about the `ignoreWarnings` option can be found [here](https://webpack.js.org/configuration/other-options/#ignorewarnings)
150
151## Contributing
152
153Please take a moment to read our contributing guidelines if you haven't yet done so.
154
155[CONTRIBUTING](./.github/CONTRIBUTING.md)
156
157## License
158
159[MIT](./LICENSE)
160
161[npm]: https://img.shields.io/npm/v/source-map-loader.svg
162[npm-url]: https://npmjs.com/package/source-map-loader
163[node]: https://img.shields.io/node/v/source-map-loader.svg
164[node-url]: https://nodejs.org
165[tests]: https://github.com/webpack-contrib/source-map-loader/workflows/source-map-loader/badge.svg
166[tests-url]: https://github.com/webpack-contrib/source-map-loader/actions
167[cover]: https://codecov.io/gh/webpack-contrib/source-map-loader/branch/master/graph/badge.svg
168[cover-url]: https://codecov.io/gh/webpack-contrib/source-map-loader
169[discussion]: https://img.shields.io/github/discussions/webpack/webpack
170[discussion-url]: https://github.com/webpack/webpack/discussions
171[size]: https://packagephobia.now.sh/badge?p=source-map-loader
172[size-url]: https://packagephobia.now.sh/result?p=source-map-loader