Skip to content

Commit

Permalink
fix: webpack protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito authored Jun 30, 2020
1 parent e0ec5d9 commit a2e4cd6
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export default async function loader(input, inputMap) {
: null;
const skipReading = originalSourceContent !== null;

// 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.
// https://github.com/webpack-contrib/source-map-loader/issues/51
try {
({ sourceURL, sourceContent } = await fetchFromURL(
this,
Expand Down
12 changes: 5 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,14 @@ async function fetchFromURL(
return { sourceContent };
}

if (skipReading) {
return { sourceURL: url };
}

if (protocol === 'file:') {
const pathFromURL = urlUtils.fileURLToPath(url);

const sourceURL = path.normalize(pathFromURL);

let sourceContent;

if (!skipReading) {
sourceContent = await fetchFromFilesystem(loaderContext, sourceURL);
}
const sourceContent = await fetchFromFilesystem(loaderContext, sourceURL);

return { sourceURL, sourceContent };
}
Expand Down
57 changes: 51 additions & 6 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,7 @@ anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\
}
`;

exports[`source-map-loader should process css sourceMap: warnings 1`] = `
Array [
"ModuleWarning: Module Warning (from \`replaced original path\`):
Failed to parse source map: \\"webpack:///./src/app/app.scss\\" URL is not supported",
]
`;
exports[`source-map-loader should process css sourceMap: warnings 1`] = `Array []`;

exports[`source-map-loader should process css sourceMap: warnings 2`] = `
Array [
Expand Down Expand Up @@ -310,6 +305,28 @@ Failed to parse source map: \\"ftp://exampleurl.com\\" URL is not supported",
]
`;

exports[`source-map-loader should skip file protocol path if sourcesContent is set: css 1`] = `
"// Some content
"
`;

exports[`source-map-loader should skip file protocol path if sourcesContent is set: errors 1`] = `Array []`;

exports[`source-map-loader should skip file protocol path if sourcesContent is set: map 1`] = `
Object {
"mappings": "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA",
"sources": Array [
"file:///external normal-file2.js",
],
"sourcesContent": Array [
"export default 3;",
],
"version": 3,
}
`;

exports[`source-map-loader should skip file protocol path if sourcesContent is set: warnings 1`] = `Array []`;

exports[`source-map-loader should skip invalid base64 SourceMap: css 1`] = `
"without SourceMap
// @sourceMappingURL=data:application/source-map;base64,\\"something invalid\\"
Expand All @@ -325,6 +342,34 @@ Failed to parse source map from 'undefined': SyntaxError: Unexpected end of JSON
]
`;

exports[`source-map-loader should skip webpack protocol path if sourcesContent is set: css 1`] = `
"!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,\\"a\\",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p=\\"\\",r(r.s=0)}([function(e,t,r){\\"use strict\\";r.r(t),t.default=3}]);
"
`;

exports[`source-map-loader should skip webpack protocol path if sourcesContent is set: errors 1`] = `Array []`;

exports[`source-map-loader should skip webpack protocol path if sour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,OAAOA,GAEtC,OADAH,EAAoBU,EAAEE,EAAQ,IAAKA,GAC5BA,GAIRZ,EAAoBa,EAAI,SAASiB,EAAQC,GAAY,OAAOjB,OAAOkB,UAAUC,eAAe1B,KAAKuB,EAAQC,IAGzG/B,EAAoBkC,EAAI,GAIjBlC,EAAoBA,EAAoBmC,EAAI,G,+BClFrD,OAAe",
"names": Array [],
"sources": Array [
"webpack:///webpack/bootstrap",
"webpack:///./src/index.js",
"webpack:///external \\"$\\"",
],
"sourcesContent": Array [
" //webpack bootstrap;",
"export default 3;",
"module.exports = $;",
],
"version": 3,
}
`;

exports[`source-map-loader should skip webpack protocol path if sourcesContent is set: warnings 1`] = `Array []`;

exports[`source-map-loader should support absolute paths to sourcemaps: css 1`] = `
"// Some content
"
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/file-broken-protocol-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Some content
// # sourceMappingURL=file-broken-protocol-path.js.map
1 change: 1 addition & 0 deletions test/fixtures/file-broken-protocol-path.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/webpack/main.js

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

1 change: 1 addition & 0 deletions test/fixtures/webpack/main.js.map

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

26 changes: 26 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,32 @@ describe('source-map-loader', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should skip file protocol path if sourcesContent is set', async () => {
const testId = 'file-broken-protocol-path.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);

expect(codeFromBundle.map).toBeDefined();
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should skip webpack protocol path if sourcesContent is set', async () => {
const testId = 'webpack/main.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);

expect(codeFromBundle.map).toBeDefined();
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should use last SourceMap directive', async () => {
const testId = 'multi-source-map.js';
const compiler = getCompiler(testId);
Expand Down

0 comments on commit a2e4cd6

Please sign in to comment.