Skip to content

Commit a2e4cd6

Browse files
fix: webpack protocol
1 parent e0ec5d9 commit a2e4cd6

File tree

8 files changed

+91
-13
lines changed

8 files changed

+91
-13
lines changed

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ export default async function loader(input, inputMap) {
8282
: null;
8383
const skipReading = originalSourceContent !== null;
8484

85+
// We do not skipReading here, because we need absolute paths in sources.
86+
// This is necessary so that for sourceMaps with the same file structure in sources, name collisions do not occur.
87+
// https://github.com/webpack-contrib/source-map-loader/issues/51
8588
try {
8689
({ sourceURL, sourceContent } = await fetchFromURL(
8790
this,

src/utils.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,14 @@ async function fetchFromURL(
160160
return { sourceContent };
161161
}
162162

163+
if (skipReading) {
164+
return { sourceURL: url };
165+
}
166+
163167
if (protocol === 'file:') {
164168
const pathFromURL = urlUtils.fileURLToPath(url);
165-
166169
const sourceURL = path.normalize(pathFromURL);
167-
168-
let sourceContent;
169-
170-
if (!skipReading) {
171-
sourceContent = await fetchFromFilesystem(loaderContext, sourceURL);
172-
}
170+
const sourceContent = await fetchFromFilesystem(loaderContext, sourceURL);
173171

174172
return { sourceURL, sourceContent };
175173
}

test/__snapshots__/loader.test.js.snap

+51-6
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,7 @@ anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\
128128
}
129129
`;
130130

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

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

308+
exports[`source-map-loader should skip file protocol path if sourcesContent is set: css 1`] = `
309+
"// Some content
310+
"
311+
`;
312+
313+
exports[`source-map-loader should skip file protocol path if sourcesContent is set: errors 1`] = `Array []`;
314+
315+
exports[`source-map-loader should skip file protocol path if sourcesContent is set: map 1`] = `
316+
Object {
317+
"mappings": "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA",
318+
"sources": Array [
319+
"file:///external normal-file2.js",
320+
],
321+
"sourcesContent": Array [
322+
"export default 3;",
323+
],
324+
"version": 3,
325+
}
326+
`;
327+
328+
exports[`source-map-loader should skip file protocol path if sourcesContent is set: warnings 1`] = `Array []`;
329+
313330
exports[`source-map-loader should skip invalid base64 SourceMap: css 1`] = `
314331
"without SourceMap
315332
// @sourceMappingURL=data:application/source-map;base64,\\"something invalid\\"
@@ -325,6 +342,34 @@ Failed to parse source map from 'undefined': SyntaxError: Unexpected end of JSON
325342
]
326343
`;
327344

345+
exports[`source-map-loader should skip webpack protocol path if sourcesContent is set: css 1`] = `
346+
"!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}]);
347+
"
348+
`;
349+
350+
exports[`source-map-loader should skip webpack protocol path if sourcesContent is set: errors 1`] = `Array []`;
351+
352+
exports[`source-map-loader should skip webpack protocol path if sourcesContent is set: map 1`] = `
353+
Object {
354+
"file": "main.js",
355+
"mappings": "aACE,IAAIA,EAAmB,GAGvB,SAASC,EAAoBC,GAG5B,GAAGF,EAAiBE,GACnB,OAAOF,EAAiBE,GAAUC,QAGnC,IAAIC,EAASJ,EAAiBE,GAAY,CACzCG,EAAGH,EACHI,GAAG,EACHH,QAAS,IAUV,OANAI,EAAQL,GAAUM,KAAKJ,EAAOD,QAASC,EAAQA,EAAOD,QAASF,GAG/DG,EAAOE,GAAI,EAGJF,EAAOD,QAKfF,EAAoBQ,EAAIF,EAGxBN,EAAoBS,EAAIV,EAGxBC,EAAoBU,EAAI,SAASR,EAASS,EAAMC,GAC3CZ,EAAoBa,EAAEX,EAASS,IAClCG,OAAOC,eAAeb,EAASS,EAAM,CAAEK,YAAY,EAAMC,IAAKL,KAKhEZ,EAAoBkB,EAAI,SAAShB,GACX,oBAAXiB,QAA0BA,OAAOC,aAC1CN,OAAOC,eAAeb,EAASiB,OAAOC,YAAa,CAAEC,MAAO,WAE7DP,OAAOC,eAAeb,EAAS,aAAc,CAAEmB,OAAO,KAQvDrB,EAAoBsB,EAAI,SAASD,EAAOE,GAEvC,GADU,EAAPA,IAAUF,EAAQrB,EAAoBqB,IAC/B,EAAPE,EAAU,OAAOF,EACpB,GAAW,EAAPE,GAA8B,iBAAVF,GAAsBA,GAASA,EAAMG,WAAY,OAAOH,EAChF,IAAII,EAAKX,OAAOY,OAAO,MAGvB,GAFA1B,EAAoBkB,EAAEO,GACtBX,OAAOC,eAAeU,EAAI,UAAW,CAAET,YAAY,EAAMK,MAAOA,IACtD,EAAPE,GAA4B,iBAATF,EAAmB,IAAI,IAAIM,KAAON,EAAOrB,EAAoBU,EAAEe,EAAIE,EAAK,SAASA,GAAO,OAAON,EAAMM,IAAQC,KAAK,KAAMD,IAC9I,OAAOF,GAIRzB,EAAoB6B,EAAI,SAAS1B,GAChC,IAAIS,EAAST,GAAUA,EAAOqB,WAC7B,WAAwB,OAAOrB,EAAgB,SAC/C,WAA8B,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",
356+
"names": Array [],
357+
"sources": Array [
358+
"webpack:///webpack/bootstrap",
359+
"webpack:///./src/index.js",
360+
"webpack:///external \\"$\\"",
361+
],
362+
"sourcesContent": Array [
363+
" //webpack bootstrap;",
364+
"export default 3;",
365+
"module.exports = $;",
366+
],
367+
"version": 3,
368+
}
369+
`;
370+
371+
exports[`source-map-loader should skip webpack protocol path if sourcesContent is set: warnings 1`] = `Array []`;
372+
328373
exports[`source-map-loader should support absolute paths to sourcemaps: css 1`] = `
329374
"// Some content
330375
"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Some content
2+
// # sourceMappingURL=file-broken-protocol-path.js.map

test/fixtures/file-broken-protocol-path.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/webpack/main.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/webpack/main.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/loader.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,32 @@ describe('source-map-loader', () => {
154154
expect(getErrors(stats)).toMatchSnapshot('errors');
155155
});
156156

157+
it('should skip file protocol path if sourcesContent is set', async () => {
158+
const testId = 'file-broken-protocol-path.js';
159+
const compiler = getCompiler(testId);
160+
const stats = await compile(compiler);
161+
const codeFromBundle = getCodeFromBundle(stats, compiler);
162+
163+
expect(codeFromBundle.map).toBeDefined();
164+
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
165+
expect(codeFromBundle.css).toMatchSnapshot('css');
166+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
167+
expect(getErrors(stats)).toMatchSnapshot('errors');
168+
});
169+
170+
it('should skip webpack protocol path if sourcesContent is set', async () => {
171+
const testId = 'webpack/main.js';
172+
const compiler = getCompiler(testId);
173+
const stats = await compile(compiler);
174+
const codeFromBundle = getCodeFromBundle(stats, compiler);
175+
176+
expect(codeFromBundle.map).toBeDefined();
177+
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
178+
expect(codeFromBundle.css).toMatchSnapshot('css');
179+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
180+
expect(getErrors(stats)).toMatchSnapshot('errors');
181+
});
182+
157183
it('should use last SourceMap directive', async () => {
158184
const testId = 'multi-source-map.js';
159185
const compiler = getCompiler(testId);

0 commit comments

Comments
 (0)