Skip to content

Commit

Permalink
fix: do not watch unnecessary sources
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Oct 27, 2020
1 parent ae3a7b1 commit 23768df
Show file tree
Hide file tree
Showing 12 changed files with 1,209 additions and 2,186 deletions.
3,137 changes: 988 additions & 2,149 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,28 @@
"whatwg-mimetype": "^2.3.0"
},
"devDependencies": {
"@babel/cli": "^7.11.6",
"@babel/core": "^7.11.6",
"@babel/preset-env": "^7.11.5",
"@babel/cli": "^7.12.1",
"@babel/core": "^7.12.3",
"@babel/preset-env": "^7.12.1",
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@webpack-contrib/defaults": "^6.3.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^26.5.2",
"babel-jest": "^26.6.1",
"cross-env": "^7.0.2",
"del": "^6.0.0",
"del-cli": "^3.0.1",
"eslint": "^7.10.0",
"eslint-config-prettier": "^6.12.0",
"eslint": "^7.12.0",
"eslint-config-prettier": "^6.14.0",
"eslint-plugin-import": "^2.22.1",
"husky": "^4.3.0",
"jest": "^26.5.2",
"lint-staged": "^10.4.0",
"jest": "^26.6.1",
"lint-staged": "^10.4.2",
"memfs": "^3.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.1.2",
"standard-version": "^9.0.0",
"webpack": "^4.44.2"
"webpack": "^5.2.0"
},
"keywords": [
"webpack"
Expand Down
25 changes: 14 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ export default async function loader(input, inputMap) {
map = JSON.parse(sourceContent.replace(/^\)\]\}'/, ''));
} catch (parseError) {
this.emitWarning(
new Error(`Failed to parse source map from '${sourceURL}': ${parseError}`)
new Error(
`Failed to parse source map from '${sourceMappingURL}': ${parseError}`
)
);

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

const originalSourceContent =
map.sourcesContent && map.sourcesContent[i]
map.sourcesContent && typeof map.sourcesContent[i] !== 'undefined'
? map.sourcesContent[i]
: null;
const skipReading = originalSourceContent !== null;
: // eslint-disable-next-line no-undefined
undefined;
const skipReading = typeof originalSourceContent !== 'undefined';
let errored = false;

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

sourceURL = source;
this.emitWarning(error);
}

if (originalSourceContent) {
if (skipReading) {
sourceContent = originalSourceContent;
}

if (sourceURL) {
} else if (!errored && sourceURL) {
this.addDependency(sourceURL);
}

return { sourceURL, sourceContent };
// Return original value of `source` when error happens
return { sourceURL: errored ? source : sourceURL, sourceContent };
})
);

Expand Down
10 changes: 7 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,13 @@ async function fetchFromURL(
const { protocol } = urlUtils.parse(url);

if (protocol === 'data:') {
if (skipReading) {
return { sourceURL: '' };
}

const sourceContent = fetchFromDataURL(loaderContext, url);

return { sourceContent };
return { sourceURL: '', sourceContent };
}

if (skipReading) {
Expand All @@ -207,14 +211,14 @@ async function fetchFromURL(
}

throw new Error(
`Failed to parse source map: "${url}" URL is not supported`
`Failed to parse source map: '${url}' URL is not supported`
);
}

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

Expand Down
88 changes: 79 additions & 9 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,71 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`source-map-loader should add only valid \`sources\` to dependencies: css 1`] = `
"// Dependencies
"
`;

exports[`source-map-loader should add only valid \`sources\` to dependencies: errors 1`] = `Array []`;

exports[`source-map-loader should add only valid \`sources\` to dependencies: map 1`] = `
Object {
"file": "inline-sources.js",
"mappings": "AAAA",
"sources": Array [
"/test/fixtures/data/relative-sourceRoot-source-map.txt - (normalized for test)",
"./data/not-found.txt",
"",
"data:invalid;A;a",
],
"sourcesContent": Array [
"with SourceMap
// comment",
"",
"some kind content",
"",
],
"version": 3,
}
`;

exports[`source-map-loader should add only valid \`sources\` to dependencies: warnings 1`] = `
Array [
"ModuleWarning: Module Warning (from \`replaced original path\`):
Failed to parse source map from \\"data\\" URL: data:invalid;A;a",
"ModuleWarning: Module Warning (from \`replaced original path\`):
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'",
]
`;

exports[`source-map-loader should add only valid \`sources\` with \`sourceContent\` to dependencies: css 1`] = `
"// Dependencies
"
`;

exports[`source-map-loader should add only valid \`sources\` with \`sourceContent\` to dependencies: errors 1`] = `Array []`;

exports[`source-map-loader should add only valid \`sources\` with \`sourceContent\` to dependencies: map 1`] = `
Object {
"file": "inline-sources.js",
"mappings": "AAAA",
"sources": Array [
"/test/fixtures/data/relative-sourceRoot-source-map.txt - (normalized for test)",
"/test/fixtures/data/not-found.txt - (normalized for test)",
"",
"",
],
"sourcesContent": Array [
"foo",
"bar",
"",
"baz",
],
"version": 3,
}
`;

exports[`source-map-loader should add only valid \`sources\` with \`sourceContent\` to dependencies: warnings 1`] = `Array []`;

exports[`source-map-loader should allow to filter warnings: css 1`] = `
"without SourceMap
// @sourceMappingURL=data:application/source-map;base64,invalid/base64=
Expand Down Expand Up @@ -280,7 +346,7 @@ exports[`source-map-loader should process protocol-relative-url-path: errors 1`]
exports[`source-map-loader should process protocol-relative-url-path: warnings 1`] = `
Array [
"ModuleWarning: Module Warning (from \`replaced original path\`):
Failed to parse source map: \\"//sampledomain.com/external-source-map2.map\\" URL is not supported",
Failed to parse source map: '//sampledomain.com/external-source-map2.map' URL is not supported",
]
`;

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

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

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

Expand All @@ -400,11 +466,15 @@ Object {
"webpack:///webpack/bootstrap",
"webpack:///./src/index.js",
"webpack:///external \\"$\\"",
"/test/fixtures/external {\\"commonjs\\":\\"react\\",\\"commonjs2\\":\\"react\\",\\"amd\\":\\"react\\",\\"root\\":\\"React\\"} - (normalized for test)",
"/test/fixtures/external {\\"commonjs\\":\\"react-dom\\",\\"commonjs2\\":\\"react-dom\\",\\"amd\\":\\"react-dom\\",\\"root\\":\\"ReactDOM\\"} - (normalized for test)",
],
"sourcesContent": Array [
" //webpack bootstrap;",
"export default 3;",
"module.exports = $;",
"module.exports = __WEBPACK_EXTERNAL_MODULE__5__;",
"module.exports = __WEBPACK_EXTERNAL_MODULE__6__;",
],
"version": 3,
}
Expand Down Expand Up @@ -583,9 +653,9 @@ anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\
exports[`source-map-loader should support mixed paths in sources with sourceRoot: warnings 1`] = `
Array [
"ModuleWarning: Module Warning (from \`replaced original path\`):
Failed to parse source map: \\"ftp://path-to-map.com\\" URL is not supported",
Failed to parse source map: 'ftp://path-to-map.com' URL is not supported",
"ModuleWarning: Module Warning (from \`replaced original path\`):
Failed to parse source map: \\"http://path-to-map.com\\" URL is not supported",
Failed to parse source map: 'http://path-to-map.com' URL is not supported",
]
`;

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

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

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/dependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Dependencies
//#sourceMappingURL=dependencies.js.map
11 changes: 11 additions & 0 deletions test/fixtures/dependencies.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/fixtures/dependencies2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Dependencies
//#sourceMappingURL=dependencies2.js.map
17 changes: 17 additions & 0 deletions test/fixtures/dependencies2.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
22 changes: 21 additions & 1 deletion test/fixtures/webpack/main.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 23768df

Please sign in to comment.