Skip to content

Commit 4d043f0

Browse files
docs: ignore example
1 parent 1ccc708 commit 4d043f0

File tree

5 files changed

+82
-19
lines changed

5 files changed

+82
-19
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,33 @@ Be mindful in setting [include](https://webpack.js.org/configuration/module/#rul
5858

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

61+
## Examples
62+
63+
### Ignoring Warnings
64+
65+
To ignore warnings, you can use the following configuration:
66+
67+
**webpack.config.js**
68+
69+
```js
70+
module.exports = {
71+
module: {
72+
rules: [
73+
{
74+
test: /\.js$/,
75+
enforce: 'pre',
76+
use: ['source-map-loader'],
77+
},
78+
],
79+
},
80+
stats: {
81+
warningsFilter: [/Failed to parse source map/],
82+
},
83+
};
84+
```
85+
86+
More information about the `warningsFilters` option you can find [here](https://webpack.js.org/configuration/stats/#statswarningsfilter);
87+
6188
## Contributing
6289

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

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default async function loader(input, inputMap) {
5454
map = JSON.parse(sourceContent.replace(/^\)\]\}'/, ''));
5555
} catch (parseError) {
5656
this.emitWarning(
57-
new Error(`Cannot parse source map from '${sourceURL}': ${parseError}`)
57+
new Error(`Failed to parse source map from '${sourceURL}': ${parseError}`)
5858
);
5959

6060
callback(null, input, inputMap);

src/utils.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function fetchFromDataURL(loaderContext, sourceURL) {
109109
return decode(dataURL.body, dataURL.encodingName);
110110
}
111111

112-
throw new Error(`Can not parse inline source map: ${sourceURL}`);
112+
throw new Error(`Failed to parse source map from "data" URL: ${sourceURL}`);
113113
}
114114

115115
async function fetchFromFilesystem(loaderContext, sourceURL) {
@@ -126,7 +126,9 @@ async function fetchFromFilesystem(loaderContext, sourceURL) {
126126
});
127127
});
128128
} catch (error) {
129-
throw new Error(`Cannot read '${sourceURL}' file: ${error}`);
129+
throw new Error(
130+
`Failed to parse source map from '${sourceURL}' file: ${error}`
131+
);
130132
}
131133

132134
return buffer.toString();
@@ -163,12 +165,16 @@ async function fetchFromURL(
163165
return { sourceURL, sourceContent };
164166
}
165167

166-
throw new Error(`Absolute '${url}' URL is not supported`);
168+
throw new Error(
169+
`Failed to parse source map: "${url}" URL is not supported`
170+
);
167171
}
168172

169173
// 2. It's a scheme-relative
170174
if (/^\/\//.test(url)) {
171-
throw new Error(`Scheme-relative '${url}' URL is not supported`);
175+
throw new Error(
176+
`Failed to parse source map: "${url}" URL is not supported`
177+
);
172178
}
173179

174180
// 3. Absolute path

test/__snapshots__/loader.test.js.snap

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`source-map-loader should allow to filter warnings: css 1`] = `
4+
"without SourceMap
5+
// @sourceMappingURL=data:application/source-map;base64,invalid/base64=
6+
// comment"
7+
`;
8+
9+
exports[`source-map-loader should allow to filter warnings: errors 1`] = `Array []`;
10+
11+
exports[`source-map-loader should allow to filter warnings: warnings 1`] = `
12+
Array [
13+
"ModuleWarning: Module Warning (from \`replaced original path\`):
14+
Failed to parse source map from \\"data\\" URL: data:application/source-map;base64,invalid/base64=",
15+
]
16+
`;
17+
318
exports[`source-map-loader should leave normal files untouched: css 1`] = `"without SourceMap"`;
419

520
exports[`source-map-loader should leave normal files untouched: errors 1`] = `Array []`;
@@ -116,14 +131,14 @@ anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\
116131
exports[`source-map-loader should process css sourceMap: warnings 1`] = `
117132
Array [
118133
"ModuleWarning: Module Warning (from \`replaced original path\`):
119-
Absolute 'webpack:///./src/app/app.scss' URL is not supported",
134+
Failed to parse source map: \\"webpack:///./src/app/app.scss\\" URL is not supported",
120135
]
121136
`;
122137

123138
exports[`source-map-loader should process css sourceMap: warnings 2`] = `
124139
Array [
125140
"ModuleWarning: Module Warning (from \`replaced original path\`):
126-
Cannot read '/test/fixtures/unresolved-file.js' file: Error: ENOENT: no such file or directory, open '/test/fixtures/unresolved-file.js'",
141+
Failed to parse source map from '/test/fixtures/unresolved-file.js' file: Error: ENOENT: no such file or directory, open '/test/fixtures/unresolved-file.js'",
127142
]
128143
`;
129144

@@ -255,7 +270,7 @@ exports[`source-map-loader should process protocol-relative-url-path: errors 1`]
255270
exports[`source-map-loader should process protocol-relative-url-path: warnings 1`] = `
256271
Array [
257272
"ModuleWarning: Module Warning (from \`replaced original path\`):
258-
Scheme-relative '//sampledomain.com/external-source-map2.map' URL is not supported",
273+
Failed to parse source map: \\"//sampledomain.com/external-source-map2.map\\" URL is not supported",
259274
]
260275
`;
261276

@@ -271,7 +286,7 @@ exports[`source-map-loader should reject http SourceMaps: errors 1`] = `Array []
271286
exports[`source-map-loader should reject http SourceMaps: warnings 1`] = `
272287
Array [
273288
"ModuleWarning: Module Warning (from \`replaced original path\`):
274-
Absolute 'http://sampledomain.com/external-source-map2.map' URL is not supported",
289+
Failed to parse source map: \\"http://sampledomain.com/external-source-map2.map\\" URL is not supported",
275290
]
276291
`;
277292

@@ -291,7 +306,7 @@ exports[`source-map-loader should reject not support url: errors 1`] = `Array []
291306
exports[`source-map-loader should reject not support url: warnings 1`] = `
292307
Array [
293308
"ModuleWarning: Module Warning (from \`replaced original path\`):
294-
Absolute 'ftp://exampleurl.com' URL is not supported",
309+
Failed to parse source map: \\"ftp://exampleurl.com\\" URL is not supported",
295310
]
296311
`;
297312

@@ -306,7 +321,7 @@ exports[`source-map-loader should skip invalid base64 SourceMap: errors 1`] = `A
306321
exports[`source-map-loader should skip invalid base64 SourceMap: warnings 1`] = `
307322
Array [
308323
"ModuleWarning: Module Warning (from \`replaced original path\`):
309-
Cannot parse source map from 'undefined': SyntaxError: Unexpected end of JSON input",
324+
Failed to parse source map from 'undefined': SyntaxError: Unexpected end of JSON input",
310325
]
311326
`;
312327

@@ -481,9 +496,9 @@ anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\
481496
exports[`source-map-loader should support mixed paths in sources with sourceRoot: warnings 1`] = `
482497
Array [
483498
"ModuleWarning: Module Warning (from \`replaced original path\`):
484-
Absolute 'ftp://path-to-map.com' URL is not supported",
499+
Failed to parse source map: \\"ftp://path-to-map.com\\" URL is not supported",
485500
"ModuleWarning: Module Warning (from \`replaced original path\`):
486-
Absolute 'http://path-to-map.com' URL is not supported",
501+
Failed to parse source map: \\"http://path-to-map.com\\" URL is not supported",
487502
]
488503
`;
489504

@@ -519,9 +534,9 @@ anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\
519534
exports[`source-map-loader should support mixed paths in sources without sourceRoot: warnings 1`] = `
520535
Array [
521536
"ModuleWarning: Module Warning (from \`replaced original path\`):
522-
Absolute 'ftp://path-to-map.com' URL is not supported",
537+
Failed to parse source map: \\"ftp://path-to-map.com\\" URL is not supported",
523538
"ModuleWarning: Module Warning (from \`replaced original path\`):
524-
Absolute 'http://path-to-map.com' URL is not supported",
539+
Failed to parse source map: \\"http://path-to-map.com\\" URL is not supported",
525540
]
526541
`;
527542

@@ -584,7 +599,7 @@ exports[`source-map-loader should warn on invalid SourceMap: errors 1`] = `Array
584599
exports[`source-map-loader should warn on invalid SourceMap: warnings 1`] = `
585600
Array [
586601
"ModuleWarning: Module Warning (from \`replaced original path\`):
587-
Cannot parse source map from '/test/fixtures/invalid-source-map.map': SyntaxError: Unexpected string in JSON at position 102",
602+
Failed to parse source map from '/test/fixtures/invalid-source-map.map': SyntaxError: Unexpected string in JSON at position 102",
588603
]
589604
`;
590605

@@ -599,7 +614,7 @@ exports[`source-map-loader should warn on invalid base64 SourceMap: errors 1`] =
599614
exports[`source-map-loader should warn on invalid base64 SourceMap: warnings 1`] = `
600615
Array [
601616
"ModuleWarning: Module Warning (from \`replaced original path\`):
602-
Can not parse inline source map: data:application/source-map;base64,invalid/base64=",
617+
Failed to parse source map from \\"data\\" URL: data:application/source-map;base64,invalid/base64=",
603618
]
604619
`;
605620

@@ -614,7 +629,7 @@ exports[`source-map-loader should warn on missing SourceMap: errors 1`] = `Array
614629
exports[`source-map-loader should warn on missing SourceMap: warnings 1`] = `
615630
Array [
616631
"ModuleWarning: Module Warning (from \`replaced original path\`):
617-
Cannot read '/test/fixtures/missing-source-map.map' file: Error: ENOENT: no such file or directory, open '/test/fixtures/missing-source-map.map'",
632+
Failed to parse source map from '/test/fixtures/missing-source-map.map' file: Error: ENOENT: no such file or directory, open '/test/fixtures/missing-source-map.map'",
618633
]
619634
`;
620635

@@ -639,6 +654,6 @@ Object {
639654
exports[`source-map-loader should warn on missing source file: warnings 1`] = `
640655
Array [
641656
"ModuleWarning: Module Warning (from \`replaced original path\`):
642-
Cannot read '/test/fixtures/missing-source-map2.txt' file: Error: ENOENT: no such file or directory, open '/test/fixtures/missing-source-map2.txt'",
657+
Failed to parse source map from '/test/fixtures/missing-source-map2.txt' file: Error: ENOENT: no such file or directory, open '/test/fixtures/missing-source-map2.txt'",
643658
]
644659
`;

test/loader.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,4 +576,19 @@ describe('source-map-loader', () => {
576576
expect(getWarnings(stats)).toMatchSnapshot('warnings');
577577
expect(getErrors(stats)).toMatchSnapshot('errors');
578578
});
579+
580+
it('should allow to filter warnings', async () => {
581+
const testId = 'invalid-inline-source-map2.js';
582+
const compiler = getCompiler(testId);
583+
const stats = await compile(compiler);
584+
const codeFromBundle = getCodeFromBundle(stats, compiler);
585+
586+
expect(
587+
stats.toString({ warningsFilter: /Failed to parse source map/ })
588+
).not.toContain('Failed to parse source map');
589+
expect(codeFromBundle.map).toBeUndefined();
590+
expect(codeFromBundle.css).toMatchSnapshot('css');
591+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
592+
expect(getErrors(stats)).toMatchSnapshot('errors');
593+
});
579594
});

0 commit comments

Comments
 (0)