Skip to content

Commit

Permalink
feat(assemble-lite): improve intergartion of error messages in webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
mbehzad committed Jul 2, 2024
1 parent fdc4be4 commit 90a3a7d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
46 changes: 32 additions & 14 deletions packages/assemble-lite/Assemble.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ module.exports = class Assemble {
if (this.verbose) console.log("[Assemble-Lite] ", ...args);
}

// first error message during build, will be used to throw when there was no custom onError handler and the build should hard fail
_errorMsg = null;
// call the optional provided callback function with the error message
error(msg, error) {
console.error(msg);
console.error(error);

const errorMsg = `${msg} :: ${error.message}`;

if (this.onError) this.onError?.(errorMsg);
// there is no custom error handler, throw and fail the build
else if (!this._errorMsg) this._errorMsg = errorMsg;
}

/**
* builds all the html pages, based on the provided arguments,
* an re-uses cached items from the previous builds
Expand All @@ -60,6 +74,7 @@ module.exports = class Assemble {
* layouts,
* componentsTargetDirectory,
* pagesTargetDirectory,
* onError?,
* }
* @param {string[] | null} modifiedFiles - list of paths of files which have been modified compared to the last build
* @memberof Assemble
Expand All @@ -75,11 +90,14 @@ module.exports = class Assemble {
layouts,
componentsTargetDirectory,
pagesTargetDirectory,
onError,
},
modifiedFiles
) {
this.log("--------------------------------");
this.log("Build start ...");
this.onError = onError;
this._errorMsg = null;

// use a timer to measure execution duration for individual tasks
const timer = new Timer();
Expand Down Expand Up @@ -289,6 +307,8 @@ module.exports = class Assemble {
);
this.log("Build end. took:", timer.measure("BUILD", true), "s");
this.log("--------------------------------");

if (this._errorMsg) throw this._errorMsg;
}

/**
Expand Down Expand Up @@ -449,10 +469,10 @@ module.exports = class Assemble {
pvHandlebars.registerHelper(helperFn);
this.helpers[getName(path)] = { path, name: getName(path) };
} catch (error) {
console.error(
`[Assemble-Lite] Failed reading handlebars helper ${basename(path)}`
this.error(
`[Assemble-Lite] Failed reading handlebars helper ${basename(path)}`,
error
);
console.error(error);
// make sure on the next iteration, the helper is re-read,
// so the user can't forget about this issue
this.failedPaths.push(path);
Expand Down Expand Up @@ -483,10 +503,10 @@ module.exports = class Assemble {
});
}
} catch (error) {
console.error(
`[assemble-lite] Failed reading data file ${basename(path)}`
this.error(
`[assemble-lite] Failed reading data file ${basename(path)}`,
error
);
console.error(error);
// make sure on the next iteration, the helper is re-read,
// so the user can't forget about this issue (if it still exist)
this.failedPaths.push(path);
Expand Down Expand Up @@ -520,8 +540,7 @@ module.exports = class Assemble {
path
)}`;
const errorMarkup = `<!-- ${errorMessage} -->`;
console.error(errorMessage);
console.error(error);
this.error(errorMessage, error);

return {
clearedMarkup: errorMarkup,
Expand All @@ -545,11 +564,11 @@ module.exports = class Assemble {
failed: false,
};
} catch (error) {
console.error();
const errorMessage = `[assemble-lite] error parsing ${filename}.hbs`;
const errorMessage = `[assemble-lite] error parsing ${basename(
filename
)}`;
const errorMarkup = `<!-- ${errorMessage} -->`;
console.error(errorMessage);
console.error(error);
this.error(errorMessage, error);

return {
ast: pvHandlebars.parse(errorMarkup),
Expand Down Expand Up @@ -578,8 +597,7 @@ module.exports = class Assemble {
const errorMessage = `[assemble-lite] failed to render template for ${basename(
path
)}`;
console.error(errorMessage);
console.error(error);
this.error(errorMessage, error);

return `<!-- ${errorMessage} -->`;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/pv-stylemark/webpack-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class PvStylemarkPlugin {
layouts: resolveApp(fileGlobes.assembleFiles.layouts),
componentsTargetDirectory: resolveApp(join(destPath, "components")),
pagesTargetDirectory: resolveApp(join(destPath, "pages")),
onError(errorMessage) {
compilation.errors.push(errorMessage);
},
},
this.firstRun ? null : changedAssembleFiles,
);
Expand Down

0 comments on commit 90a3a7d

Please sign in to comment.