Skip to content

Commit 5cc3dfd

Browse files
feat: filter source mapping url
1 parent fbd1d76 commit 5cc3dfd

7 files changed

+326
-11
lines changed

README.md

+56-2
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,70 @@ module.exports = {
4848
};
4949
```
5050

51-
`source-map-loader` extracts existing source maps from all JavaScript entries.
51+
The `source-map-loader` extracts existing source maps from all JavaScript entries.
5252
This includes both inline source maps as well as those linked via URL.
5353
All 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/).
5454
This loader is especially useful when using 3rd-party libraries having their own source maps.
5555
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.
56-
`source-map-loader` will extract from any JavaScript file, including those in the `node_modules` directory.
56+
The `source-map-loader` will extract from any JavaScript file, including those in the `node_modules` directory.
5757
Be 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.
5858

5959
And run `webpack` via your preferred method.
6060

61+
## Options
62+
63+
| Name | Type | Default | Description |
64+
| :----------------------------------: | :----------: | :---------: | :--------------------------------------------- |
65+
| **[`filterSourceMappingUrl`](#url)** | `{Function}` | `undefined` | Allows to control `SourceMappingURL` behaviour |
66+
67+
### filterSourceMappingUrl
68+
69+
Type: `Function`
70+
Default: `undefined`
71+
72+
Allows you to specify the behavior of the loader for `SourceMappingURL` comment.
73+
74+
The function must return one of the values:
75+
76+
- `true` or `'consume'` - consume the source map and remove `SourceMappingURL` comment (default behavior)
77+
- `false` or `'remove'` - do not consume the source map and remove `SourceMappingURL` comment
78+
- `skip` - do not consume the source map and do not remove `SourceMappingURL` comment
79+
80+
Example configuration:
81+
82+
**webpack.config.js**
83+
84+
```js
85+
module.exports = {
86+
module: {
87+
rules: [
88+
{
89+
test: /\.js$/,
90+
enforce: 'pre',
91+
use: [
92+
{
93+
loader: 'source-map-loader',
94+
options: {
95+
filterSourceMappingUrl: (url, resourcePath) => {
96+
if (/broker-source-map-url\.js$/i.test(url)) {
97+
return false;
98+
}
99+
100+
if (/keep-source-mapping-url\.js$/i.test(resourcePath)) {
101+
return 'skip';
102+
}
103+
104+
return true;
105+
},
106+
},
107+
},
108+
],
109+
},
110+
],
111+
},
112+
};
113+
```
114+
61115
## Examples
62116

63117
### Ignoring Warnings

src/index.js

+25
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default async function loader(input, inputMap) {
1919
});
2020

2121
const { sourceMappingURL, replacementString } = getSourceMappingURL(input);
22+
2223
const callback = this.async();
2324

2425
if (!sourceMappingURL) {
@@ -27,6 +28,30 @@ export default async function loader(input, inputMap) {
2728
return;
2829
}
2930

31+
let behaviourSourceMappingUrl;
32+
33+
try {
34+
behaviourSourceMappingUrl =
35+
typeof options.filterSourceMappingUrl !== 'undefined'
36+
? options.filterSourceMappingUrl(sourceMappingURL, this.resourcePath)
37+
: 'consume';
38+
} catch (error) {
39+
callback(error);
40+
41+
return;
42+
}
43+
44+
// eslint-disable-next-line default-case
45+
switch (behaviourSourceMappingUrl) {
46+
case 'skip':
47+
callback(null, input, inputMap);
48+
return;
49+
case false:
50+
case 'remove':
51+
callback(null, input.replace(replacementString, ''), inputMap);
52+
return;
53+
}
54+
3055
let sourceURL;
3156
let sourceContent;
3257

src/options.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{
22
"type": "object",
3-
"additionalProperties": false
3+
"additionalProperties": false,
4+
"properties": {
5+
"filterSourceMappingUrl": {
6+
"instanceof": "Function"
7+
}
8+
}
49
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`filterSourceMappingUrl option should emit error: errors 1`] = `
4+
Array [
5+
"ModuleBuildError: Module build failed (from \`replaced original path\`):
6+
Error: error",
7+
]
8+
`;
9+
10+
exports[`filterSourceMappingUrl option should emit error: warnings 1`] = `Array []`;
11+
12+
exports[`filterSourceMappingUrl option should work with "remove" value: css 1`] = `
13+
"with SourceMap
14+
// comment
15+
"
16+
`;
17+
18+
exports[`filterSourceMappingUrl option should work with "remove" value: errors 1`] = `Array []`;
19+
20+
exports[`filterSourceMappingUrl option should work with "remove" value: warnings 1`] = `Array []`;
21+
22+
exports[`filterSourceMappingUrl option should work with "skip" value: css 1`] = `
23+
"with SourceMap
24+
//#sourceMappingURL=http://sampledomain.com/external-source-map2.map
25+
// comment
26+
"
27+
`;
28+
29+
exports[`filterSourceMappingUrl option should work with "skip" value: errors 1`] = `Array []`;
30+
31+
exports[`filterSourceMappingUrl option should work with "skip" value: warnings 1`] = `Array []`;
32+
33+
exports[`filterSourceMappingUrl option should work with false value: css 1`] = `
34+
"with SourceMap
35+
// comment
36+
"
37+
`;
38+
39+
exports[`filterSourceMappingUrl option should work with false value: errors 1`] = `Array []`;
40+
41+
exports[`filterSourceMappingUrl option should work with false value: warnings 1`] = `Array []`;
42+
43+
exports[`filterSourceMappingUrl option should work with true value: css 1`] = `
44+
"with SourceMap
45+
// comment"
46+
`;
47+
48+
exports[`filterSourceMappingUrl option should work with true value: errors 1`] = `Array []`;
49+
50+
exports[`filterSourceMappingUrl option should work with true value: map 1`] = `
51+
Object {
52+
"file": "external-source-map.js",
53+
"mappings": "AAAA",
54+
"sources": Array [
55+
"/test/fixtures/external-source-map.txt - (normalized for test)",
56+
],
57+
"sourcesContent": Array [
58+
"with SourceMap",
59+
],
60+
"version": 3,
61+
}
62+
`;
63+
64+
exports[`filterSourceMappingUrl option should work with true value: warnings 1`] = `Array []`;
65+
66+
exports[`filterSourceMappingUrl option should work: css 1`] = `
67+
"with SourceMap
68+
// comment"
69+
`;
70+
71+
exports[`filterSourceMappingUrl option should work: errors 1`] = `Array []`;
72+
73+
exports[`filterSourceMappingUrl option should work: map 1`] = `
74+
Object {
75+
"file": "external-source-map.js",
76+
"mappings": "AAAA",
77+
"sources": Array [
78+
"/test/fixtures/external-source-map.txt - (normalized for test)",
79+
],
80+
"sourcesContent": Array [
81+
"with SourceMap",
82+
],
83+
"version": 3,
84+
}
85+
`;
86+
87+
exports[`filterSourceMappingUrl option should work: warnings 1`] = `Array []`;
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,89 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`validate options should throw an error on the "filterSourceMappingUrl" option with "/test/" value 1`] = `
4+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
5+
- options.filterSourceMappingUrl should be an instance of function."
6+
`;
7+
8+
exports[`validate options should throw an error on the "filterSourceMappingUrl" option with "[]" value 1`] = `
9+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
10+
- options.filterSourceMappingUrl should be an instance of function."
11+
`;
12+
13+
exports[`validate options should throw an error on the "filterSourceMappingUrl" option with "{"foo":"bar"}" value 1`] = `
14+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
15+
- options.filterSourceMappingUrl should be an instance of function."
16+
`;
17+
18+
exports[`validate options should throw an error on the "filterSourceMappingUrl" option with "{}" value 1`] = `
19+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
20+
- options.filterSourceMappingUrl should be an instance of function."
21+
`;
22+
23+
exports[`validate options should throw an error on the "filterSourceMappingUrl" option with "1" value 1`] = `
24+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
25+
- options.filterSourceMappingUrl should be an instance of function."
26+
`;
27+
28+
exports[`validate options should throw an error on the "filterSourceMappingUrl" option with "false" value 1`] = `
29+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
30+
- options.filterSourceMappingUrl should be an instance of function."
31+
`;
32+
33+
exports[`validate options should throw an error on the "filterSourceMappingUrl" option with "test" value 1`] = `
34+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
35+
- options.filterSourceMappingUrl should be an instance of function."
36+
`;
37+
38+
exports[`validate options should throw an error on the "filterSourceMappingUrl" option with "true" value 1`] = `
39+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
40+
- options.filterSourceMappingUrl should be an instance of function."
41+
`;
42+
343
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
444
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
545
- options has an unknown property 'unknown'. These properties are valid:
6-
object {}"
46+
object { filterSourceMappingUrl? }"
747
`;
848
949
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
1050
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
1151
- options has an unknown property 'unknown'. These properties are valid:
12-
object {}"
52+
object { filterSourceMappingUrl? }"
1353
`;
1454
1555
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
1656
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
1757
- options has an unknown property 'unknown'. These properties are valid:
18-
object {}"
58+
object { filterSourceMappingUrl? }"
1959
`;
2060
2161
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
2262
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
2363
- options has an unknown property 'unknown'. These properties are valid:
24-
object {}"
64+
object { filterSourceMappingUrl? }"
2565
`;
2666
2767
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
2868
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
2969
- options has an unknown property 'unknown'. These properties are valid:
30-
object {}"
70+
object { filterSourceMappingUrl? }"
3171
`;
3272
3373
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
3474
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
3575
- options has an unknown property 'unknown'. These properties are valid:
36-
object {}"
76+
object { filterSourceMappingUrl? }"
3777
`;
3878
3979
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
4080
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
4181
- options has an unknown property 'unknown'. These properties are valid:
42-
object {}"
82+
object { filterSourceMappingUrl? }"
4383
`;
4484
4585
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
4686
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
4787
- options has an unknown property 'unknown'. These properties are valid:
48-
object {}"
88+
object { filterSourceMappingUrl? }"
4989
`;

0 commit comments

Comments
 (0)