Skip to content

Commit 1c259ea

Browse files
feat: report error type
1 parent 1ccc708 commit 1c259ea

8 files changed

+480
-13
lines changed

README.md

+110
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,116 @@ Be mindful in setting [include](https://webpack.js.org/configuration/module/#rul
5858

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

61+
## Options
62+
63+
### `brokenMapUrlReportType`
64+
65+
Type: `String`
66+
Default: `warning`
67+
68+
Type of error message, when the map failed to load.
69+
70+
Possible values:
71+
72+
- `ignore`
73+
- `warning`
74+
- `error`
75+
76+
**webpack.config.js**
77+
78+
```js
79+
module.exports = {
80+
module: {
81+
rules: [
82+
{
83+
test: /\.js$/,
84+
enforce: 'pre',
85+
use: [
86+
{
87+
loader: 'source-map-loader',
88+
options: {
89+
brokenMapUrlReportType: 'ignore',
90+
},
91+
},
92+
],
93+
},
94+
],
95+
},
96+
};
97+
```
98+
99+
### `brokenMapParseReportType`
100+
101+
Type: `String`
102+
Default: `warning`
103+
104+
Type of error message, when the card is received, but cannot be correctly parsed.
105+
106+
Possible values:
107+
108+
- `ignore`
109+
- `warning`
110+
- `error`
111+
112+
**webpack.config.js**
113+
114+
```js
115+
module.exports = {
116+
module: {
117+
rules: [
118+
{
119+
test: /\.js$/,
120+
enforce: 'pre',
121+
use: [
122+
{
123+
loader: 'source-map-loader',
124+
options: {
125+
brokenMapParseReportType: 'ignore',
126+
},
127+
},
128+
],
129+
},
130+
],
131+
},
132+
};
133+
```
134+
135+
### `brokenSourceUrlReportType`
136+
137+
Type: `String`
138+
Default: `warning`
139+
140+
Type of error message, when the source (from `map.sources`) failed to load.
141+
142+
Possible values:
143+
144+
- `ignore`
145+
- `warning`
146+
- `error`
147+
148+
**webpack.config.js**
149+
150+
```js
151+
module.exports = {
152+
module: {
153+
rules: [
154+
{
155+
test: /\.js$/,
156+
enforce: 'pre',
157+
use: [
158+
{
159+
loader: 'source-map-loader',
160+
options: {
161+
brokenSourceUrlReportType: 'ignore',
162+
},
163+
},
164+
],
165+
},
166+
],
167+
},
168+
};
169+
```
170+
61171
## Contributing
62172

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

src/index.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import validateOptions from 'schema-utils';
88
import { getOptions } from 'loader-utils';
99

1010
import schema from './options.json';
11-
import { getSourceMappingURL, fetchFromURL, flattenSourceMap } from './utils';
11+
import {
12+
getSourceMappingURL,
13+
fetchFromURL,
14+
flattenSourceMap,
15+
getErrorReporter,
16+
} from './utils';
1217

1318
export default async function loader(input, inputMap) {
1419
const options = getOptions(this);
@@ -37,7 +42,11 @@ export default async function loader(input, inputMap) {
3742
sourceMappingURL
3843
));
3944
} catch (error) {
40-
this.emitWarning(error);
45+
const brokenMapUrlReporter = getErrorReporter(
46+
this,
47+
options.brokenMapUrlReportType
48+
);
49+
brokenMapUrlReporter(error);
4150

4251
callback(null, input, inputMap);
4352

@@ -53,7 +62,11 @@ export default async function loader(input, inputMap) {
5362
try {
5463
map = JSON.parse(sourceContent.replace(/^\)\]\}'/, ''));
5564
} catch (parseError) {
56-
this.emitWarning(
65+
const brokenMapParseReporter = getErrorReporter(
66+
this,
67+
options.brokenMapParseReportType
68+
);
69+
brokenMapParseReporter(
5770
new Error(`Cannot parse source map from '${sourceURL}': ${parseError}`)
5871
);
5972

@@ -91,7 +104,11 @@ export default async function loader(input, inputMap) {
91104
skipReading
92105
));
93106
} catch (error) {
94-
this.emitWarning(error);
107+
const brokenSourceUrlReporter = getErrorReporter(
108+
this,
109+
options.brokenSourceUrlReportType
110+
);
111+
brokenSourceUrlReporter(error);
95112

96113
sourceURL = source;
97114
}

src/options.json

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
{
22
"type": "object",
3+
"properties": {
4+
"brokenMapUrlReportType": {
5+
"enum": ["ignore", "warning", "error"]
6+
},
7+
"brokenMapParseReportType": {
8+
"enum": ["ignore", "warning", "error"]
9+
},
10+
"brokenSourceUrlReportType": {
11+
"enum": ["ignore", "warning", "error"]
12+
}
13+
},
314
"additionalProperties": false
415
}

src/utils.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,20 @@ async function fetchFromURL(
196196
return { sourceURL, sourceContent };
197197
}
198198

199-
export { getSourceMappingURL, fetchFromURL, flattenSourceMap };
199+
function getErrorReporter(loaderContext, typeReport) {
200+
switch (typeReport) {
201+
case 'error':
202+
return loaderContext.emitError;
203+
case 'ignore':
204+
return function ignore() {};
205+
default:
206+
return loaderContext.emitWarning;
207+
}
208+
}
209+
210+
export {
211+
getSourceMappingURL,
212+
fetchFromURL,
213+
flattenSourceMap,
214+
getErrorReporter,
215+
};

test/__snapshots__/loader.test.js.snap

+95
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,70 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`source-map-loader should error on broken map url error SourceMap: css 1`] = `
4+
"without SourceMap
5+
// @sourceMappingURL=data:application/source-map;base64,invalid/base64=
6+
// comment"
7+
`;
8+
9+
exports[`source-map-loader should error on broken map url error SourceMap: errors 1`] = `
10+
Array [
11+
"ModuleError: Module Error (from \`replaced original path\`):
12+
Can not parse inline source map: data:application/source-map;base64,invalid/base64=",
13+
]
14+
`;
15+
16+
exports[`source-map-loader should error on broken map url error SourceMap: warnings 1`] = `Array []`;
17+
18+
exports[`source-map-loader should error on broken source in map.sources: css 1`] = `
19+
"// Skip SourcesContent in SourceMap
20+
// comment
21+
"
22+
`;
23+
24+
exports[`source-map-loader should error on broken source in map.sources: errors 1`] = `
25+
Array [
26+
"ModuleError: Module Error (from \`replaced original path\`):
27+
Cannot read '/test/fixtures/unresolved-file.js' file: Error: ENOENT: no such file or directory, open '/test/fixtures/unresolved-file.js'",
28+
]
29+
`;
30+
31+
exports[`source-map-loader should error on broken source in map.sources: warnings 1`] = `Array []`;
32+
33+
exports[`source-map-loader should error on map parse error: css 1`] = `
34+
"with SourceMap
35+
//#sourceMappingURL=invalid-source-map.map
36+
// comment"
37+
`;
38+
39+
exports[`source-map-loader should error on map parse error: errors 1`] = `
40+
Array [
41+
"ModuleError: Module Error (from \`replaced original path\`):
42+
Cannot parse source map from '/test/fixtures/invalid-source-map.map': SyntaxError: Unexpected string in JSON at position 102",
43+
]
44+
`;
45+
46+
exports[`source-map-loader should error on map parse error: warnings 1`] = `Array []`;
47+
48+
exports[`source-map-loader should ignore report on broken map url error SourceMap: css 1`] = `
49+
"without SourceMap
50+
// @sourceMappingURL=data:application/source-map;base64,invalid/base64=
51+
// comment"
52+
`;
53+
54+
exports[`source-map-loader should ignore report on broken map url error SourceMap: errors 1`] = `Array []`;
55+
56+
exports[`source-map-loader should ignore report on broken map url error SourceMap: warnings 1`] = `Array []`;
57+
58+
exports[`source-map-loader should ignore report on map parse error: css 1`] = `
59+
"with SourceMap
60+
//#sourceMappingURL=invalid-source-map.map
61+
// comment"
62+
`;
63+
64+
exports[`source-map-loader should ignore report on map parse error: errors 1`] = `Array []`;
65+
66+
exports[`source-map-loader should ignore report on map parse error: warnings 1`] = `Array []`;
67+
368
exports[`source-map-loader should leave normal files untouched: css 1`] = `"without SourceMap"`;
469

570
exports[`source-map-loader should leave normal files untouched: errors 1`] = `Array []`;
@@ -642,3 +707,33 @@ Array [
642707
Cannot read '/test/fixtures/missing-source-map2.txt' file: Error: ENOENT: no such file or directory, open '/test/fixtures/missing-source-map2.txt'",
643708
]
644709
`;
710+
711+
exports[`source-map-loader should warning on broken map url error SourceMap: css 1`] = `
712+
"without SourceMap
713+
// @sourceMappingURL=data:application/source-map;base64,invalid/base64=
714+
// comment"
715+
`;
716+
717+
exports[`source-map-loader should warning on broken map url error SourceMap: errors 1`] = `Array []`;
718+
719+
exports[`source-map-loader should warning on broken map url error SourceMap: warnings 1`] = `
720+
Array [
721+
"ModuleWarning: Module Warning (from \`replaced original path\`):
722+
Can not parse inline source map: data:application/source-map;base64,invalid/base64=",
723+
]
724+
`;
725+
726+
exports[`source-map-loader should warning on map parse error: css 1`] = `
727+
"with SourceMap
728+
//#sourceMappingURL=invalid-source-map.map
729+
// comment"
730+
`;
731+
732+
exports[`source-map-loader should warning on map parse error: errors 1`] = `Array []`;
733+
734+
exports[`source-map-loader should warning on map parse error: warnings 1`] = `
735+
Array [
736+
"ModuleWarning: Module Warning (from \`replaced original path\`):
737+
Cannot parse source map from '/test/fixtures/invalid-source-map.map': SyntaxError: Unexpected string in JSON at position 102",
738+
]
739+
`;

0 commit comments

Comments
 (0)