From 53161b46382f9b0e57124c08eea4b526c6f1e995 Mon Sep 17 00:00:00 2001 From: Marcin Szczepanski Date: Tue, 18 Jun 2024 10:55:57 +1000 Subject: [PATCH] Improve constant module bundling error message (#9803) * Improve constant module bundling error message * Avoid work when not throwing error --- .../packagers/js/src/ScopeHoistingPackager.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/packagers/js/src/ScopeHoistingPackager.js b/packages/packagers/js/src/ScopeHoistingPackager.js index 980b9683072..4f3157b7849 100644 --- a/packages/packagers/js/src/ScopeHoistingPackager.js +++ b/packages/packagers/js/src/ScopeHoistingPackager.js @@ -17,7 +17,7 @@ import { } from '@parcel/utils'; import SourceMap from '@parcel/source-map'; import nullthrows from 'nullthrows'; -import invariant from 'assert'; +import invariant, {AssertionError} from 'assert'; import ThrowableDiagnostic, { convertSourceLocationToHighlight, } from '@parcel/diagnostic'; @@ -922,10 +922,17 @@ ${code} isWrapped(resolved: Asset, parentAsset: Asset): boolean { if (resolved.meta.isConstantModule) { - invariant( - this.bundle.hasAsset(resolved), - 'Constant module not found in bundle', - ); + if (!this.bundle.hasAsset(resolved)) { + throw new AssertionError({ + message: `Constant module ${path.relative( + this.options.projectRoot, + resolved.filePath, + )} referenced from ${path.relative( + this.options.projectRoot, + parentAsset.filePath, + )} not found in bundle ${this.bundle.name}`, + }); + } return false; } return (