Skip to content

Commit 30317f8

Browse files
fix: resolve server-relative url
1 parent 7d127a9 commit 30317f8

8 files changed

+149
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ test/fixtures/file-protocol-path.js.map
77
test/fixtures/map-with-sourceroot.js.map
88
test/fixtures/map-without-sourceroot.js.map
99
test/fixtures/normal-map.js.map
10+
/test/fixtures/server-relative-url-path.js.map
1011
logs
1112
*.log
1213
npm-debug.log*

src/utils.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,41 @@ async function fetchFromFilesystem(loaderContext, sourceURL) {
143143
return buffer.toString();
144144
}
145145

146+
async function fetchPathsFromFilesystem(
147+
loaderContext,
148+
possibleRequests,
149+
errorsAccumulator = ''
150+
) {
151+
let result;
152+
153+
try {
154+
result = await fetchFromFilesystem(
155+
loaderContext,
156+
possibleRequests[0],
157+
errorsAccumulator
158+
);
159+
} catch (error) {
160+
// eslint-disable-next-line no-param-reassign
161+
errorsAccumulator += `${error.message}\n\n`;
162+
163+
const [, ...tailPossibleRequests] = possibleRequests;
164+
165+
if (tailPossibleRequests.length === 0) {
166+
error.message = errorsAccumulator;
167+
168+
throw error;
169+
}
170+
171+
return fetchPathsFromFilesystem(
172+
loaderContext,
173+
tailPossibleRequests,
174+
errorsAccumulator
175+
);
176+
}
177+
178+
return result;
179+
}
180+
146181
async function fetchFromURL(
147182
loaderContext,
148183
context,
@@ -191,7 +226,18 @@ async function fetchFromURL(
191226
let sourceContent;
192227

193228
if (!skipReading) {
194-
sourceContent = await fetchFromFilesystem(loaderContext, sourceURL);
229+
const possibleRequests = [sourceURL];
230+
231+
if (url.startsWith('/')) {
232+
possibleRequests.push(
233+
getAbsolutePath(context, sourceURL.slice(1), sourceRoot)
234+
);
235+
}
236+
237+
sourceContent = await fetchPathsFromFilesystem(
238+
loaderContext,
239+
possibleRequests
240+
);
195241
}
196242

197243
return { sourceURL, sourceContent };

test/__snapshots__/loader.test.js.snap

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ Failed to parse source map from \\"data\\" URL: data:application/source-map;base
1515
]
1616
`;
1717

18+
exports[`source-map-loader should emit warning when unresolved server-relative-url-path: css 1`] = `
19+
"with SourceMap
20+
// #sourceMappingURL=/unresolved-server-relative-url-path.js.map
21+
// comment
22+
"
23+
`;
24+
25+
exports[`source-map-loader should emit warning when unresolved server-relative-url-path: errors 1`] = `Array []`;
26+
27+
exports[`source-map-loader should emit warning when unresolved server-relative-url-path: warnings 1`] = `
28+
Array [
29+
"ModuleWarning: Module Warning (from \`replaced original path\`):",
30+
]
31+
`;
32+
1833
exports[`source-map-loader should leave normal files untouched: css 1`] = `"without SourceMap"`;
1934

2035
exports[`source-map-loader should leave normal files untouched: errors 1`] = `Array []`;
@@ -269,6 +284,33 @@ Failed to parse source map: \\"//sampledomain.com/external-source-map2.map\\" UR
269284
]
270285
`;
271286

287+
exports[`source-map-loader should process server-relative-url-path: css 1`] = `
288+
"with SourceMap
289+
// comment
290+
"
291+
`;
292+
293+
exports[`source-map-loader should process server-relative-url-path: errors 1`] = `Array []`;
294+
295+
exports[`source-map-loader should process server-relative-url-path: map 1`] = `
296+
Object {
297+
"file": "server-relative-url-path.js",
298+
"mappings": "AAAA",
299+
"sources": Array [
300+
"/test/fixtures/server-relative-url-path.js - (normalized for test)",
301+
],
302+
"sourcesContent": Array [
303+
"with SourceMap
304+
// #sourceMappingURL=/server-relative-url-path.js.map
305+
// comment
306+
",
307+
],
308+
"version": 3,
309+
}
310+
`;
311+
312+
exports[`source-map-loader should process server-relative-url-path: warnings 1`] = `Array []`;
313+
272314
exports[`source-map-loader should reject http SourceMaps: css 1`] = `
273315
"with SourceMap
274316
//#sourceMappingURL=http://sampledomain.com/external-source-map2.map
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
with SourceMap
2+
// #sourceMappingURL=/server-relative-url-path.js.map
3+
// comment
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
with SourceMap
2+
// #sourceMappingURL=/unresolved-server-relative-url-path.js.map
3+
// comment

test/helpers/getWarnings.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import normalizeErrors from './normalizeErrors';
22

3-
export default (stats) => {
4-
return normalizeErrors(stats.compilation.warnings.sort());
3+
export default (stats, shortError) => {
4+
return normalizeErrors(stats.compilation.warnings.sort(), shortError);
55
};

test/helpers/normalizeErrors.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ function removeCWD(str) {
1414
.replace(new RegExp(cwd, 'g'), '');
1515
}
1616

17-
export default (errors) => {
18-
return errors.map((error) =>
19-
removeCWD(error.toString().split('\n').slice(0, 2).join('\n'))
20-
);
17+
export default (errors, shortError) => {
18+
return errors.map((error) => {
19+
let errorMessage = error.toString();
20+
21+
if (shortError) {
22+
errorMessage = errorMessage.split('\n').slice(0, 1).join('\n');
23+
}
24+
25+
return removeCWD(errorMessage.split('\n').slice(0, 2).join('\n'));
26+
});
2127
};

test/loader.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,4 +617,45 @@ describe('source-map-loader', () => {
617617
expect(getWarnings(stats)).toMatchSnapshot('warnings');
618618
expect(getErrors(stats)).toMatchSnapshot('errors');
619619
});
620+
621+
it('should process server-relative-url-path', async () => {
622+
const sourceRoot = path.resolve(__dirname, 'fixtures');
623+
const javaScriptFilename = 'server-relative-url-path.js';
624+
const sourceFilename = 'server-relative-url-path.js';
625+
const sourceMapPath = path.join(
626+
sourceRoot,
627+
'server-relative-url-path.js.map'
628+
);
629+
630+
// Create the sourcemap file
631+
const rawSourceMap = {
632+
version: 3,
633+
file: javaScriptFilename,
634+
sourceRoot,
635+
sources: [sourceFilename],
636+
mappings: 'AAAA',
637+
};
638+
fs.writeFileSync(sourceMapPath, JSON.stringify(rawSourceMap));
639+
640+
const testId = 'server-relative-url-path.js';
641+
const compiler = getCompiler(testId);
642+
const stats = await compile(compiler);
643+
const codeFromBundle = getCodeFromBundle(stats, compiler);
644+
645+
expect(codeFromBundle.css).toMatchSnapshot('css');
646+
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
647+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
648+
expect(getErrors(stats)).toMatchSnapshot('errors');
649+
});
650+
651+
it('should emit warning when unresolved server-relative-url-path', async () => {
652+
const testId = 'unresolved-server-relative-url-path.js';
653+
const compiler = getCompiler(testId);
654+
const stats = await compile(compiler);
655+
const codeFromBundle = getCodeFromBundle(stats, compiler);
656+
657+
expect(codeFromBundle.css).toMatchSnapshot('css');
658+
expect(getWarnings(stats, true)).toMatchSnapshot('warnings');
659+
expect(getErrors(stats)).toMatchSnapshot('errors');
660+
});
620661
});

0 commit comments

Comments
 (0)