Skip to content

Commit 23768df

Browse files
fix: do not watch unnecessary sources
1 parent ae3a7b1 commit 23768df

12 files changed

+1209
-2186
lines changed

package-lock.json

Lines changed: 988 additions & 2149 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,28 @@
4949
"whatwg-mimetype": "^2.3.0"
5050
},
5151
"devDependencies": {
52-
"@babel/cli": "^7.11.6",
53-
"@babel/core": "^7.11.6",
54-
"@babel/preset-env": "^7.11.5",
52+
"@babel/cli": "^7.12.1",
53+
"@babel/core": "^7.12.3",
54+
"@babel/preset-env": "^7.12.1",
5555
"@commitlint/cli": "^11.0.0",
5656
"@commitlint/config-conventional": "^11.0.0",
5757
"@webpack-contrib/defaults": "^6.3.0",
5858
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
59-
"babel-jest": "^26.5.2",
59+
"babel-jest": "^26.6.1",
6060
"cross-env": "^7.0.2",
6161
"del": "^6.0.0",
6262
"del-cli": "^3.0.1",
63-
"eslint": "^7.10.0",
64-
"eslint-config-prettier": "^6.12.0",
63+
"eslint": "^7.12.0",
64+
"eslint-config-prettier": "^6.14.0",
6565
"eslint-plugin-import": "^2.22.1",
6666
"husky": "^4.3.0",
67-
"jest": "^26.5.2",
68-
"lint-staged": "^10.4.0",
67+
"jest": "^26.6.1",
68+
"lint-staged": "^10.4.2",
6969
"memfs": "^3.2.0",
7070
"npm-run-all": "^4.1.5",
7171
"prettier": "^2.1.2",
7272
"standard-version": "^9.0.0",
73-
"webpack": "^4.44.2"
73+
"webpack": "^5.2.0"
7474
},
7575
"keywords": [
7676
"webpack"

src/index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ export default async function loader(input, inputMap) {
7979
map = JSON.parse(sourceContent.replace(/^\)\]\}'/, ''));
8080
} catch (parseError) {
8181
this.emitWarning(
82-
new Error(`Failed to parse source map from '${sourceURL}': ${parseError}`)
82+
new Error(
83+
`Failed to parse source map from '${sourceMappingURL}': ${parseError}`
84+
)
8385
);
8486

8587
callback(null, input, inputMap);
@@ -102,10 +104,12 @@ export default async function loader(input, inputMap) {
102104
let sourceContent;
103105

104106
const originalSourceContent =
105-
map.sourcesContent && map.sourcesContent[i]
107+
map.sourcesContent && typeof map.sourcesContent[i] !== 'undefined'
106108
? map.sourcesContent[i]
107-
: null;
108-
const skipReading = originalSourceContent !== null;
109+
: // eslint-disable-next-line no-undefined
110+
undefined;
111+
const skipReading = typeof originalSourceContent !== 'undefined';
112+
let errored = false;
109113

110114
// We do not skipReading here, because we need absolute paths in sources.
111115
// This is necessary so that for sourceMaps with the same file structure in sources, name collisions do not occur.
@@ -119,20 +123,19 @@ export default async function loader(input, inputMap) {
119123
skipReading
120124
));
121125
} catch (error) {
122-
this.emitWarning(error);
126+
errored = true;
123127

124-
sourceURL = source;
128+
this.emitWarning(error);
125129
}
126130

127-
if (originalSourceContent) {
131+
if (skipReading) {
128132
sourceContent = originalSourceContent;
129-
}
130-
131-
if (sourceURL) {
133+
} else if (!errored && sourceURL) {
132134
this.addDependency(sourceURL);
133135
}
134136

135-
return { sourceURL, sourceContent };
137+
// Return original value of `source` when error happens
138+
return { sourceURL: errored ? source : sourceURL, sourceContent };
136139
})
137140
);
138141

src/utils.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,13 @@ async function fetchFromURL(
189189
const { protocol } = urlUtils.parse(url);
190190

191191
if (protocol === 'data:') {
192+
if (skipReading) {
193+
return { sourceURL: '' };
194+
}
195+
192196
const sourceContent = fetchFromDataURL(loaderContext, url);
193197

194-
return { sourceContent };
198+
return { sourceURL: '', sourceContent };
195199
}
196200

197201
if (skipReading) {
@@ -207,14 +211,14 @@ async function fetchFromURL(
207211
}
208212

209213
throw new Error(
210-
`Failed to parse source map: "${url}" URL is not supported`
214+
`Failed to parse source map: '${url}' URL is not supported`
211215
);
212216
}
213217

214218
// 2. It's a scheme-relative
215219
if (/^\/\//.test(url)) {
216220
throw new Error(
217-
`Failed to parse source map: "${url}" URL is not supported`
221+
`Failed to parse source map: '${url}' URL is not supported`
218222
);
219223
}
220224

test/__snapshots__/loader.test.js.snap

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

3+
exports[`source-map-loader should add only valid \`sources\` to dependencies: css 1`] = `
4+
"// Dependencies
5+
"
6+
`;
7+
8+
exports[`source-map-loader should add only valid \`sources\` to dependencies: errors 1`] = `Array []`;
9+
10+
exports[`source-map-loader should add only valid \`sources\` to dependencies: map 1`] = `
11+
Object {
12+
"file": "inline-sources.js",
13+
"mappings": "AAAA",
14+
"sources": Array [
15+
"/test/fixtures/data/relative-sourceRoot-source-map.txt - (normalized for test)",
16+
"./data/not-found.txt",
17+
"",
18+
"data:invalid;A;a",
19+
],
20+
"sourcesContent": Array [
21+
"with SourceMap
22+
// comment",
23+
"",
24+
"some kind content",
25+
"",
26+
],
27+
"version": 3,
28+
}
29+
`;
30+
31+
exports[`source-map-loader should add only valid \`sources\` to dependencies: warnings 1`] = `
32+
Array [
33+
"ModuleWarning: Module Warning (from \`replaced original path\`):
34+
Failed to parse source map from \\"data\\" URL: data:invalid;A;a",
35+
"ModuleWarning: Module Warning (from \`replaced original path\`):
36+
Failed to parse source map from '/test/fixtures/data/not-found.txt' file: Error: ENOENT: no such file or directory, open '/test/fixtures/data/not-found.txt'",
37+
]
38+
`;
39+
40+
exports[`source-map-loader should add only valid \`sources\` with \`sourceContent\` to dependencies: css 1`] = `
41+
"// Dependencies
42+
"
43+
`;
44+
45+
exports[`source-map-loader should add only valid \`sources\` with \`sourceContent\` to dependencies: errors 1`] = `Array []`;
46+
47+
exports[`source-map-loader should add only valid \`sources\` with \`sourceContent\` to dependencies: map 1`] = `
48+
Object {
49+
"file": "inline-sources.js",
50+
"mappings": "AAAA",
51+
"sources": Array [
52+
"/test/fixtures/data/relative-sourceRoot-source-map.txt - (normalized for test)",
53+
"/test/fixtures/data/not-found.txt - (normalized for test)",
54+
"",
55+
"",
56+
],
57+
"sourcesContent": Array [
58+
"foo",
59+
"bar",
60+
"",
61+
"baz",
62+
],
63+
"version": 3,
64+
}
65+
`;
66+
67+
exports[`source-map-loader should add only valid \`sources\` with \`sourceContent\` to dependencies: warnings 1`] = `Array []`;
68+
369
exports[`source-map-loader should allow to filter warnings: css 1`] = `
470
"without SourceMap
571
// @sourceMappingURL=data:application/source-map;base64,invalid/base64=
@@ -280,7 +346,7 @@ exports[`source-map-loader should process protocol-relative-url-path: errors 1`]
280346
exports[`source-map-loader should process protocol-relative-url-path: warnings 1`] = `
281347
Array [
282348
"ModuleWarning: Module Warning (from \`replaced original path\`):
283-
Failed to parse source map: \\"//sampledomain.com/external-source-map2.map\\" URL is not supported",
349+
Failed to parse source map: '//sampledomain.com/external-source-map2.map' URL is not supported",
284350
]
285351
`;
286352

@@ -323,7 +389,7 @@ exports[`source-map-loader should reject http SourceMaps: errors 1`] = `Array []
323389
exports[`source-map-loader should reject http SourceMaps: warnings 1`] = `
324390
Array [
325391
"ModuleWarning: Module Warning (from \`replaced original path\`):
326-
Failed to parse source map: \\"http://sampledomain.com/external-source-map2.map\\" URL is not supported",
392+
Failed to parse source map: 'http://sampledomain.com/external-source-map2.map' URL is not supported",
327393
]
328394
`;
329395

@@ -343,7 +409,7 @@ exports[`source-map-loader should reject not support url: errors 1`] = `Array []
343409
exports[`source-map-loader should reject not support url: warnings 1`] = `
344410
Array [
345411
"ModuleWarning: Module Warning (from \`replaced original path\`):
346-
Failed to parse source map: \\"ftp://exampleurl.com\\" URL is not supported",
412+
Failed to parse source map: 'ftp://exampleurl.com' URL is not supported",
347413
]
348414
`;
349415

@@ -380,7 +446,7 @@ exports[`source-map-loader should skip invalid base64 SourceMap: errors 1`] = `A
380446
exports[`source-map-loader should skip invalid base64 SourceMap: warnings 1`] = `
381447
Array [
382448
"ModuleWarning: Module Warning (from \`replaced original path\`):
383-
Failed to parse source map from 'undefined': SyntaxError: Unexpected end of JSON input",
449+
Failed to parse source map from 'data:application/source-map;base64,': SyntaxError: Unexpected end of JSON input",
384450
]
385451
`;
386452

@@ -400,11 +466,15 @@ Object {
400466
"webpack:///webpack/bootstrap",
401467
"webpack:///./src/index.js",
402468
"webpack:///external \\"$\\"",
469+
"/test/fixtures/external {\\"commonjs\\":\\"react\\",\\"commonjs2\\":\\"react\\",\\"amd\\":\\"react\\",\\"root\\":\\"React\\"} - (normalized for test)",
470+
"/test/fixtures/external {\\"commonjs\\":\\"react-dom\\",\\"commonjs2\\":\\"react-dom\\",\\"amd\\":\\"react-dom\\",\\"root\\":\\"ReactDOM\\"} - (normalized for test)",
403471
],
404472
"sourcesContent": Array [
405473
" //webpack bootstrap;",
406474
"export default 3;",
407475
"module.exports = $;",
476+
"module.exports = __WEBPACK_EXTERNAL_MODULE__5__;",
477+
"module.exports = __WEBPACK_EXTERNAL_MODULE__6__;",
408478
],
409479
"version": 3,
410480
}
@@ -583,9 +653,9 @@ anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\
583653
exports[`source-map-loader should support mixed paths in sources with sourceRoot: warnings 1`] = `
584654
Array [
585655
"ModuleWarning: Module Warning (from \`replaced original path\`):
586-
Failed to parse source map: \\"ftp://path-to-map.com\\" URL is not supported",
656+
Failed to parse source map: 'ftp://path-to-map.com' URL is not supported",
587657
"ModuleWarning: Module Warning (from \`replaced original path\`):
588-
Failed to parse source map: \\"http://path-to-map.com\\" URL is not supported",
658+
Failed to parse source map: 'http://path-to-map.com' URL is not supported",
589659
]
590660
`;
591661

@@ -621,9 +691,9 @@ anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\
621691
exports[`source-map-loader should support mixed paths in sources without sourceRoot: warnings 1`] = `
622692
Array [
623693
"ModuleWarning: Module Warning (from \`replaced original path\`):
624-
Failed to parse source map: \\"ftp://path-to-map.com\\" URL is not supported",
694+
Failed to parse source map: 'ftp://path-to-map.com' URL is not supported",
625695
"ModuleWarning: Module Warning (from \`replaced original path\`):
626-
Failed to parse source map: \\"http://path-to-map.com\\" URL is not supported",
696+
Failed to parse source map: 'http://path-to-map.com' URL is not supported",
627697
]
628698
`;
629699

@@ -686,7 +756,7 @@ exports[`source-map-loader should warn on invalid SourceMap: errors 1`] = `Array
686756
exports[`source-map-loader should warn on invalid SourceMap: warnings 1`] = `
687757
Array [
688758
"ModuleWarning: Module Warning (from \`replaced original path\`):
689-
Failed to parse source map from '/test/fixtures/invalid-source-map.map': SyntaxError: Unexpected string in JSON at position 102",
759+
Failed to parse source map from 'invalid-source-map.map': SyntaxError: Unexpected string in JSON at position 102",
690760
]
691761
`;
692762

test/fixtures/dependencies.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Dependencies
2+
//#sourceMappingURL=dependencies.js.map

test/fixtures/dependencies.js.map

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/dependencies2.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Dependencies
2+
//#sourceMappingURL=dependencies2.js.map

test/fixtures/dependencies2.js.map

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/external-by-webpack.js.map

Whitespace-only changes.

test/fixtures/webpack/main.js.map

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)