Skip to content

Commit

Permalink
fix: resolve server-relative url
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito authored Aug 19, 2020
1 parent 7d127a9 commit 30317f8
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ test/fixtures/file-protocol-path.js.map
test/fixtures/map-with-sourceroot.js.map
test/fixtures/map-without-sourceroot.js.map
test/fixtures/normal-map.js.map
/test/fixtures/server-relative-url-path.js.map
logs
*.log
npm-debug.log*
Expand Down
48 changes: 47 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,41 @@ async function fetchFromFilesystem(loaderContext, sourceURL) {
return buffer.toString();
}

async function fetchPathsFromFilesystem(
loaderContext,
possibleRequests,
errorsAccumulator = ''
) {
let result;

try {
result = await fetchFromFilesystem(
loaderContext,
possibleRequests[0],
errorsAccumulator
);
} catch (error) {
// eslint-disable-next-line no-param-reassign
errorsAccumulator += `${error.message}\n\n`;

const [, ...tailPossibleRequests] = possibleRequests;

if (tailPossibleRequests.length === 0) {
error.message = errorsAccumulator;

throw error;
}

return fetchPathsFromFilesystem(
loaderContext,
tailPossibleRequests,
errorsAccumulator
);
}

return result;
}

async function fetchFromURL(
loaderContext,
context,
Expand Down Expand Up @@ -191,7 +226,18 @@ async function fetchFromURL(
let sourceContent;

if (!skipReading) {
sourceContent = await fetchFromFilesystem(loaderContext, sourceURL);
const possibleRequests = [sourceURL];

if (url.startsWith('/')) {
possibleRequests.push(
getAbsolutePath(context, sourceURL.slice(1), sourceRoot)
);
}

sourceContent = await fetchPathsFromFilesystem(
loaderContext,
possibleRequests
);
}

return { sourceURL, sourceContent };
Expand Down
42 changes: 42 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ Failed to parse source map from \\"data\\" URL: data:application/source-map;base
]
`;

exports[`source-map-loader should emit warning when unresolved server-relative-url-path: css 1`] = `
"with SourceMap
// #sourceMappingURL=/unresolved-server-relative-url-path.js.map
// comment
"
`;

exports[`source-map-loader should emit warning when unresolved server-relative-url-path: errors 1`] = `Array []`;

exports[`source-map-loader should emit warning when unresolved server-relative-url-path: warnings 1`] = `
Array [
"ModuleWarning: Module Warning (from \`replaced original path\`):",
]
`;

exports[`source-map-loader should leave normal files untouched: css 1`] = `"without SourceMap"`;

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

exports[`source-map-loader should process server-relative-url-path: css 1`] = `
"with SourceMap
// comment
"
`;

exports[`source-map-loader should process server-relative-url-path: errors 1`] = `Array []`;

exports[`source-map-loader should process server-relative-url-path: map 1`] = `
Object {
"file": "server-relative-url-path.js",
"mappings": "AAAA",
"sources": Array [
"/test/fixtures/server-relative-url-path.js - (normalized for test)",
],
"sourcesContent": Array [
"with SourceMap
// #sourceMappingURL=/server-relative-url-path.js.map
// comment
",
],
"version": 3,
}
`;

exports[`source-map-loader should process server-relative-url-path: warnings 1`] = `Array []`;

exports[`source-map-loader should reject http SourceMaps: css 1`] = `
"with SourceMap
//#sourceMappingURL=http://sampledomain.com/external-source-map2.map
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/server-relative-url-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
with SourceMap
// #sourceMappingURL=/server-relative-url-path.js.map
// comment
3 changes: 3 additions & 0 deletions test/fixtures/unresolved-server-relative-url-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
with SourceMap
// #sourceMappingURL=/unresolved-server-relative-url-path.js.map
// comment
4 changes: 2 additions & 2 deletions test/helpers/getWarnings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import normalizeErrors from './normalizeErrors';

export default (stats) => {
return normalizeErrors(stats.compilation.warnings.sort());
export default (stats, shortError) => {
return normalizeErrors(stats.compilation.warnings.sort(), shortError);
};
14 changes: 10 additions & 4 deletions test/helpers/normalizeErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ function removeCWD(str) {
.replace(new RegExp(cwd, 'g'), '');
}

export default (errors) => {
return errors.map((error) =>
removeCWD(error.toString().split('\n').slice(0, 2).join('\n'))
);
export default (errors, shortError) => {
return errors.map((error) => {
let errorMessage = error.toString();

if (shortError) {
errorMessage = errorMessage.split('\n').slice(0, 1).join('\n');
}

return removeCWD(errorMessage.split('\n').slice(0, 2).join('\n'));
});
};
41 changes: 41 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,4 +617,45 @@ describe('source-map-loader', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should process server-relative-url-path', async () => {
const sourceRoot = path.resolve(__dirname, 'fixtures');
const javaScriptFilename = 'server-relative-url-path.js';
const sourceFilename = 'server-relative-url-path.js';
const sourceMapPath = path.join(
sourceRoot,
'server-relative-url-path.js.map'
);

// Create the sourcemap file
const rawSourceMap = {
version: 3,
file: javaScriptFilename,
sourceRoot,
sources: [sourceFilename],
mappings: 'AAAA',
};
fs.writeFileSync(sourceMapPath, JSON.stringify(rawSourceMap));

const testId = 'server-relative-url-path.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);

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

it('should emit warning when unresolved server-relative-url-path', async () => {
const testId = 'unresolved-server-relative-url-path.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);

expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats, true)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
});

0 comments on commit 30317f8

Please sign in to comment.