Skip to content

Commit 3467d76

Browse files
JLHwungchristopherthielen
authored andcommitted
refactor(lazyload): detect AoT by module
Previously, the AoT switch `offlineMode` is detected by the presence of `JitCompiler`. However, as the `moduleToLoad` can be passed via public `lazyLoad` interface, there are cases when `moduleToLoad` returns either `NgModuleFactory` or `Type`, thus it is better to invoke `JITCompiler` only when `moduleToLoad` does not return an `NgModuleFactory`.
1 parent 3b65916 commit 3467d76

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/lazyLoad/lazyLoadNgModule.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,16 @@ export function loadModuleFactory(moduleToLoad: NgModuleToLoad, ng2Injector: Inj
115115
}
116116

117117
const compiler: Compiler = ng2Injector.get(Compiler);
118-
const offlineMode = compiler instanceof Compiler;
119118

120119
const unwrapEsModuleDefault = x => (x && x.__esModule && x['default'] ? x['default'] : x);
121-
const compileAsync = (moduleType: Type<any>) => compiler.compileModuleAsync(moduleType);
122120

123-
const loadChildrenPromise = Promise.resolve(moduleToLoad()).then(unwrapEsModuleDefault);
124-
return offlineMode ? loadChildrenPromise : loadChildrenPromise.then(compileAsync);
121+
return Promise.resolve(moduleToLoad()).then(unwrapEsModuleDefault)
122+
.then((t: NgModuleFactory<any> | Type<any>) => {
123+
if (t instanceof NgModuleFactory) {
124+
return t;
125+
}
126+
return compiler.compileModuleAsync(t);
127+
});
125128
}
126129

127130
/**

0 commit comments

Comments
 (0)