Skip to content

Commit

Permalink
[FIX] minify: In case of error, include full resource path in the err…
Browse files Browse the repository at this point in the history
…or messsage

As suggested by Vest in SAP/ui5-tooling#890 (comment)

This should make it easier to identify the resource that is causing an error in the minify task.
  • Loading branch information
RandomByte committed Jan 10, 2025
1 parent c1e5e9d commit aadb463
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/processors/minifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ export default async function({
filename,
dbgFilename,
code,
resourcePath,
sourceMapOptions
}, taskUtil);
resource.setString(result.code);
Expand Down
4 changes: 3 additions & 1 deletion lib/processors/minifierWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ const copyrightCommentsAndBundleCommentPattern = /copyright|\(c\)(?:[0-9]+|\s+[0
* @param {string} parameters.filename
* @param {string} parameters.dbgFilename
* @param {string} parameters.code
* @param {string} parameters.resourcePath
* @param {object} parameters.sourceMapOptions
* @returns {Promise<undefined>} Promise resolving once minification of the resource has finished
*/
export default async function execMinification({
filename,
dbgFilename,
code,
resourcePath,
sourceMapOptions
}) {
try {
Expand All @@ -61,7 +63,7 @@ export default async function execMinification({
} catch (err) {
// Note: err.filename contains the debug-name
throw new Error(
`Minification failed with error: ${err.message} in file ${filename} ` +
`Minification failed with error: ${err.message} in file ${resourcePath} ` +
`(line ${err.line}, col ${err.col}, pos ${err.pos})`, {
cause: err
});
Expand Down
67 changes: 67 additions & 0 deletions test/lib/tasks/minify.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,70 @@ ${SOURCE_MAPPING_URL}=test.js.map`;
}
t.deepEqual(await resSourceMap.getString(), expectedSourceMap, "Correct source map content");
});

test.serial("integration: minify error", async (t) => {
const taskUtil = {
setTag: t.context.sinon.stub(),
STANDARD_TAGS: {
HasDebugVariant: "1️⃣",
IsDebugVariant: "2️⃣",
OmitFromBuildResult: "3️⃣"
},
registerCleanupTask: t.context.sinon.stub()
};
const {reader, workspace} = createWorkspace();
const content = `
// Top level return will cause a parsing error
return;`;
const testResource = resourceFactory.createResource({
path: "/resources/my/namespace/test.js",
string: content
});
await reader.write(testResource);

await t.throwsAsync(() => {
return minify({
workspace,
taskUtil,
options: {
pattern: "/resources/my/namespace/test.js",
omitSourceMapResources: true
}
});
}, {
message:
`Minification failed with error: 'return' outside of function in file ` +
`/resources/my/namespace/test.js (line 3, col 0, pos 48)`
}, `Threw with expected error message`);

// Ensure to call cleanup task so that workerpool is terminated - otherwise the test will time out!
const cleanupTask = taskUtil.registerCleanupTask.getCall(0).args[0];
await cleanupTask();
});


test.serial("integration: minify error (without taskUtil)", async (t) => {
const {reader, workspace} = createWorkspace();
const content = `
// Top level return will cause a parsing error
return;`;
const testResource = resourceFactory.createResource({
path: "/resources/my/namespace/test.js",
string: content
});
await reader.write(testResource);

await t.throwsAsync(() => {
return minify({
workspace,
options: {
pattern: "/resources/my/namespace/test.js",
omitSourceMapResources: true
}
});
}, {
message:
`Minification failed with error: 'return' outside of function in file ` +
`/resources/my/namespace/test.js (line 3, col 0, pos 48)`
}, `Threw with expected error message`);
});

0 comments on commit aadb463

Please sign in to comment.