AOT Production build causes incorrect code path
Bug Report or Feature Request (mark with an x)
- [X] bug report -> please search issues before submitting
- [ ] feature request
Versions.
@angular/cli: 1.2.1 node: 7.10.0 os: win32 x64 @angular/animations: 4.3.1 @angular/common: 4.3.1 @angular/compiler: 4.3.1 @angular/core: 4.3.1 @angular/forms: 4.3.1 @angular/http: 4.3.1 @angular/platform-browser: 4.3.1 @angular/platform-browser-dynamic: 4.3.1 @angular/router: 4.3.1 @angular/cli: 1.2.1 @angular/compiler-cli: 4.3.1 @angular/language-service: 4.3.1
Windows 10
The following Typescript code:
private createActiveEnrollment$(trackerFactory: PathParameterTrackerFactory):
Observable<Enrollment> {
this.enrollmentIdTracker = trackerFactory.track(routeTree.paramNames.enrollmentId);
this.trainingProgramIdTracker = trackerFactory.track(routeTree.paramNames.trainingProgramId);
return Observable
.merge(
this.enrollments$,
this.enrollmentIdTracker.paramValue$.filter(p => !!p),
this.trainingProgramIdTracker.paramValue$.filter(p => !!p))
.map(() => this.findActiveEnrollment());
}
Gets compiled into the following Javascript code when using AOT and Production build:
function(t) {
var n = this;
return this.enrollmentIdTracker = t.track(o.a.paramNames.enrollmentId), this.trainingProgramIdTracker = t.track(o.a.paramNames.trainingProgramId), f.Observable.merge(this.enrollments$, this.enrollmentIdTracker.paramValue$.filter(function(t) {
return !!t
}), this.trainingProgramIdTracker.paramValue$.filter(function(t) {
return !!t
})).map(function() {
return n.findActiveEnrollment()
})
The problem is, the variable this.enrollmentIdTracker, upon entry to the function is undefined. The TS code sets that variable to a value. Then that value is used in the Observable.merge statement. When the code is build (with AOT under production), the three TS lines are merged into one line, and the this.enrollmentIdTracker is undefined, so trying to retrieve this.enrollmentIdTracker.paramValue$ throws an exception.
The log given by the failure.
TypeError: undefined is not a function (near '...f.Observable.merge...')
Desired functionality.
NO errors/exceptions
Further problems - it seems when building with AOT for Production, the webpack builder decided it doesn't need the Observable.merge function, or imports it and uses it incorrectly:
Without AOT:
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_4_rxjs_add_operator_distinctUntilChanged__ = __webpack_require__("../../../../rxjs/add/operator/distinctUntilChanged.js");
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_4_rxjs_add_operator_distinctUntilChanged___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_4_rxjs_add_operator_distinctUntilChanged__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_5_rxjs_add_operator_filter__ = __webpack_require__("../../../../rxjs/add/operator/filter.js");
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_5_rxjs_add_operator_filter___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_5_rxjs_add_operator_filter__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_6_rxjs_add_operator_map__ = __webpack_require__("../../../../rxjs/add/operator/map.js");
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_6_rxjs_add_operator_map___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_6_rxjs_add_operator_map__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_7_rxjs_add_operator_merge__ = __webpack_require__("../../../../rxjs/add/operator/merge.js");
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_7_rxjs_add_operator_merge___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_7_rxjs_add_operator_merge__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_8_rxjs_add_operator_switchMap__ = __webpack_require__("../../../../rxjs/add/operator/switchMap.js");
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_8_rxjs_add_operator_switchMap___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_8_rxjs_add_operator_switchMap__);
With AOT:
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_3_rxjs_add_operator_distinctUntilChanged__ = __webpack_require__("../../../../rxjs/add/operator/distinctUntilChanged.js");
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_3_rxjs_add_operator_distinctUntilChanged___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_3_rxjs_add_operator_distinctUntilChanged__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_4_rxjs_add_operator_filter__ = __webpack_require__("../../../../rxjs/add/operator/filter.js");
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_4_rxjs_add_operator_filter___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_4_rxjs_add_operator_filter__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_5_rxjs_add_operator_map__ = __webpack_require__("../../../../rxjs/add/operator/map.js");
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_5_rxjs_add_operator_map___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_5_rxjs_add_operator_map__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_6_rxjs_add_operator_merge__ = __webpack_require__("../../../../rxjs/add/operator/merge.js");
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_6_rxjs_add_operator_merge___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_6_rxjs_add_operator_merge__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_7_rxjs_add_operator_switchMap__ = __webpack_require__("../../../../rxjs/add/operator/switchMap.js");
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_7_rxjs_add_operator_switchMap___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_7_rxjs_add_operator_switchMap__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_8_rxjs_BehaviorSubject__ = __webpack_require__("../../../../rxjs/BehaviorSubject.js");
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_8_rxjs_BehaviorSubject___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_8_rxjs_BehaviorSubject__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_9_rxjs_Observable__ = __webpack_require__("../../../../rxjs/Observable.js");
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_9_rxjs_Observable___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_9_rxjs_Observable__);
Resulting in the error (for the above code, which DOES import the merge operator):
main.bundle.js:2205 Error: Uncaught (in promise): TypeError: __WEBPACK_IMPORTED_MODULE_9_rxjs_Observable__.Observable.merge is not a function
TypeError: __WEBPACK_IMPORTED_MODULE_9_rxjs_Observable__.Observable.merge is not a function
I understand you're having a problem with your project, but from the details you've shown I'm not absolutely sure of what you're suggesting (typescript compilation is incorrect).
Could you perhaps trim and isolate the problematic code into a runnable repro I could take a look at? If it's really a transpilation bug I'd like to get it to the appropriate people.
Looks like this issue is showing up again when using AngularFire2. where the AOT complied code fails to include Observable.merge() - resulting in run time error 'undefined'. See here - https://github.com/angular/angularfire2/issues/1424
UPDATE: Using es5 for 'ng serve' an es6 for 'prod -aot' resolves the issue for me.
Can you still reproduce the problem? If it's still here, would you share a minimal demo where we'll be able to reproduce it in our local environment so we can investigate?
Closing the issue due to inactivity. If the problem still exists in the latest version of the CLI, please open a new issue following the template and providing a minimal demo where we can reproduce it. Thanks! :-)
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.