Skip to content

Commit

Permalink
fix(webpack): fix progress order errors covering compilation errors, …
Browse files Browse the repository at this point in the history
…improve __webpack_require__.nmd compatibility
  • Loading branch information
naugtur committed May 7, 2024
1 parent aa04eda commit fe86454
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
36 changes: 29 additions & 7 deletions packages/webpack/src/buildtime/progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ const diag = require('./diagnostics')
* matches expectedStep
* @property {(expectedStep: string) => boolean} done - Checks if expectedStep
* was already reported.
* @property {(expectedStep: string) => void} assertDone - Throws if
* @property {(expectedStep: string) => void} assertDone - Reports an error if
* expectedStep was not already reported.
* @property {(step: string) => void} report - Moves progress forward if step
* passed is the next step. no-op if current step (reporting progress is
* idempotent)
* @property {(errors: Error[]) => void} reportErrorsTo - Wire up the array to
* push errors to for compilation. Pass compilation.errors to it as soon as
* possible.
*/

/**
Expand All @@ -21,6 +24,14 @@ const diag = require('./diagnostics')
* @returns {ProgressAPI}
*/
module.exports = function progress({ steps }) {
/** @type {Error[]} */
let compilationErrors = []
/**
* @param {Error} e
*/
const reportError = (e) => {
compilationErrors.push(e)
}
const canRepeat = new Set()

steps = steps.map((step) => {
Expand All @@ -47,10 +58,12 @@ module.exports = function progress({ steps }) {
}
done.add(step)
if (steps[currentStep + 1] !== step) {
throw Error(
`LavaMoatPlugin Plugin: Progress reported '${step}' but the next step was expected to be '${
steps[currentStep + 1]
}'`
reportError(
Error(
`LavaMoatPlugin Plugin: Progress reported '${step}' but the next step was expected to be '${
steps[currentStep + 1]
}'`
)
)
} else {
diag.rawDebug(1, `\n> progress ${steps[currentStep]}->${step}`)
Expand Down Expand Up @@ -81,9 +94,18 @@ module.exports = function progress({ steps }) {
if (done.has(query)) {
return
}
throw Error(
`LavaMoatPlugin Plugin: Expected '${query}' to be done, but we're at '${steps[currentStep]}'`
reportError(
Error(
`LavaMoatPlugin Plugin: Expected '${query}' to be done, but we're at '${steps[currentStep]}'`
)
)
}
/**
* @param {Error[]} errors
*/
API.reportErrorsTo = (errors) => {
errors.push(...compilationErrors)
compilationErrors = errors
}
return API
}
1 change: 1 addition & 0 deletions packages/webpack/src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class LavaMoatPlugin {
compiler.hooks.thisCompilation.tap(
PLUGIN_NAME,
(compilation, { normalModuleFactory }) => {
PROGRESS.reportErrorsTo(compilation.errors)
compilation.hooks.optimizeAssets.tap(PLUGIN_NAME, () => {
// By the time assets are being optimized we should have finished.
// This will ensure all previous steps have been done.
Expand Down
1 change: 1 addition & 0 deletions packages/webpack/src/runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ const lavamoatRuntimeWrapper = (resourceId, runtimeKit) => {
policyRequire.nmd = (moduleReference) => {
if (moduleReference === module) {
module = __webpack_require__.nmd(module)
return module
}
}

Expand Down

0 comments on commit fe86454

Please sign in to comment.