Skip to content

Commit 1d52b95

Browse files
fix(runtime): always throw if component can not be loaded (#5762)
1 parent 3cfbb8d commit 1d52b95

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

src/client/client-load-module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const loadModule = (
2727
cmpMeta: d.ComponentRuntimeMeta,
2828
hostRef: d.HostRef,
2929
hmrVersionId?: string,
30-
): Promise<d.ComponentConstructor> | d.ComponentConstructor => {
30+
): Promise<d.ComponentConstructor | undefined> | d.ComponentConstructor => {
3131
// loadModuleImport
3232
const exportName = cmpMeta.$tagName$.replace(/-/g, '_');
3333
const bundleId = cmpMeta.$lazyBundleId$;

src/hydrate/platform/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let customError: d.ErrorHandler;
66

77
export const cmpModules = new Map<string, { [exportName: string]: d.ComponentConstructor }>();
88

9-
const getModule = (tagName: string): d.ComponentConstructor => {
9+
const getModule = (tagName: string): d.ComponentConstructor | null => {
1010
if (typeof tagName === 'string') {
1111
tagName = tagName.toLowerCase();
1212
const cmpModule = cmpModules.get(tagName);
@@ -17,7 +17,11 @@ const getModule = (tagName: string): d.ComponentConstructor => {
1717
return null;
1818
};
1919

20-
export const loadModule = (cmpMeta: d.ComponentRuntimeMeta, _hostRef: d.HostRef, _hmrVersionId?: string): any => {
20+
export const loadModule = (
21+
cmpMeta: d.ComponentRuntimeMeta,
22+
_hostRef: d.HostRef,
23+
_hmrVersionId?: string,
24+
): d.ComponentConstructor | null => {
2125
return getModule(cmpMeta.$tagName$);
2226
};
2327

src/runtime/initialize-component.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const initializeComponent = async (
2626
cmpMeta: d.ComponentRuntimeMeta,
2727
hmrVersionId?: string,
2828
) => {
29-
let Cstr: any;
29+
let Cstr: d.ComponentConstructor | undefined;
3030
// initializeComponent
3131
if ((hostRef.$flags$ & HOST_FLAGS.hasInitializedComponent) === 0) {
3232
// Let the runtime know that the component has been initialized
@@ -37,17 +37,19 @@ export const initializeComponent = async (
3737
// lazy loaded components
3838
// request the component's implementation to be
3939
// wired up with the host element
40-
Cstr = loadModule(cmpMeta, hostRef, hmrVersionId);
41-
if (Cstr.then) {
40+
const CstrImport = loadModule(cmpMeta, hostRef, hmrVersionId);
41+
if (CstrImport && 'then' in CstrImport) {
4242
// Await creates a micro-task avoid if possible
4343
const endLoad = uniqueTime(
4444
`st:load:${cmpMeta.$tagName$}:${hostRef.$modeName$}`,
4545
`[Stencil] Load module for <${cmpMeta.$tagName$}>`,
4646
);
47-
Cstr = await Cstr;
47+
Cstr = await CstrImport;
4848
endLoad();
49+
} else {
50+
Cstr = CstrImport as d.ComponentConstructor | undefined;
4951
}
50-
if ((BUILD.isDev || BUILD.isDebug) && !Cstr) {
52+
if (!Cstr) {
5153
throw new Error(`Constructor for "${cmpMeta.$tagName$}#${hostRef.$modeName$}" was not found`);
5254
}
5355
if (BUILD.member && !Cstr.isProxied) {
@@ -96,12 +98,16 @@ export const initializeComponent = async (
9698
customElements.whenDefined(cmpMeta.$tagName$).then(() => (hostRef.$flags$ |= HOST_FLAGS.isWatchReady));
9799
}
98100

99-
if (BUILD.style && Cstr.style) {
101+
if (BUILD.style && Cstr && Cstr.style) {
100102
// this component has styles but we haven't registered them yet
101103
let style = Cstr.style;
102104

103105
if (BUILD.mode && typeof style !== 'string') {
104-
style = style[(hostRef.$modeName$ = computeMode(elm))];
106+
hostRef.$modeName$ = computeMode(elm) as string | undefined;
107+
if (hostRef.$modeName$) {
108+
style = style[hostRef.$modeName$];
109+
}
110+
105111
if (BUILD.hydrateServerSide && hostRef.$modeName$) {
106112
elm.setAttribute('s-mode', hostRef.$modeName$);
107113
}

0 commit comments

Comments
 (0)