-
Notifications
You must be signed in to change notification settings - Fork 12k
Tree shaking removes dynamically created NgModule #5275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Please, anyone? |
+1 |
+1 Have the same issue when build in prod mode, whereas dev mode works fine |
The issue arises when I'm using main.bundle.js when ...
var AppModule = (function () {
function AppModule() {
}
return AppModule;
}());
AppModule = __decorate([
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__angular_core__["c" /* NgModule */])({
imports: [
__WEBPACK_IMPORTED_MODULE_1__angular_platform_browser__["a" /* BrowserModule */],
__WEBPACK_IMPORTED_MODULE_3__angular_http__["a" /* HttpModule */],
__WEBPACK_IMPORTED_MODULE_2__angular_router__["a" /* RouterModule */],
__WEBPACK_IMPORTED_MODULE_5__routing_module__["a" /* AppRoutingModule */]
],
declarations: [__WEBPACK_IMPORTED_MODULE_4__app_component__["a" /* AppComponent */]],
exports: [],
bootstrap: [__WEBPACK_IMPORTED_MODULE_4__app_component__["a" /* AppComponent */]],
})
], AppModule);
... main.bundle.js when ...
var AppModule = (function () {
function AppModule() {
}
return AppModule;
}());
... |
AFAIK this will never work on AOT, because it requires code to be statically analyzable. You can still build for production without AOT by doing |
But why should we want that? I've got a quite large application and aot is ten times faster (initial load only has a performance boost of 6 to 8 seconds). Are you saying we need to stick to JIT compiler if we need to dynamically add component template html and no AOT will be possible....ever? |
Some people have managed to use AOT and have the JitCompiler be shipped with it to be able to compile components at runtime, see angular/angular#15510 (comment) I think this issue (or a new one more specifically on making it possible to create an app supporting AOT + JIT) should be reopened. |
To be fair the user who came up with the solution over there did not use the CLI but handcrafted it using Webpack. @maxbellec Your second link is not about AOT compilation, please remove it. |
I managed to workthrough this by using the AOT compiled componentNgFactory instead of the https://github.com/KingMario/leaflet-popup, a demo of interactive dynamic Angular component content for Leaflet popup created by componentFactory. Here is the demo. When you click the marker, the popup content will change randomly. The git diff for the corresponding file: KingMario/leaflet-popup@1fc6793#diff-5c26d2c8f8838f32ace87fdecd6344ea Other changes in this commit are for my own AOT compilation case study only. You may run Hope it will help you out. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I am taking the below information from the following StackOverflow question: http://stackoverflow.com/questions/42537138/angular2-cli-tree-shaking-removing-dynamically-created-ngmodule (I am the author)
OS?
any
Versions.
Repro steps.
Compiling my app with a non-production environment works just fine. However, when I run
ng build --prod -e prod
the transpiling/compiling process removes all dynamically created modules.The log given by the failure.
Immediately when trying to load a dynamically created component:
Mention any other details that might be useful.
My dynamic component builder (the problem is in the
createComponentModule
function which dynamically creates a new module):And its transpiled/compiled result:
The 'e' in the error message is the function
e()
fromcreateComponentModule
which, as you can see, is empty even though is should contain the@NgModule
content.This is the transpiled/compiled content with a non-production setting, still containing all the information about the removed NgModule:
I assume tree shaking removes this dynamically created NgModule.
How can I create a new NgModule like this and still use the production mode of Angular CLI?
The text was updated successfully, but these errors were encountered: