Description
I'm currently working on a new ionic project and trying out TypeORM. While trying to do my first production build for testing out typeorm/typeorm#2419 I found that its currently not possible to build ionic (angular) projects with AoT compiler (ahead of time) turned on when typeORM is included, including this example project.
When using the term "production build" I mean this command: "ionic cordova build ios --prod --release" (the --prod flag is the problematic one).
There are three problems:
- In its current state this example project is using angular 4.x with typescript 2.9 ("^2.7.1") despite that angular 4 only works with TS 2.3 but it was not specified in its package.json so you get no warning when installing it. The dev and prod builds both finish without any error however as the angular compiler doesn't (yet) complain about the wrong TS version., but I can't open the prod version of the app after a prod build. I only get this error:
vendor.js:1 Uncaught Error: Cannot find module "."
at vendor.js:1
at vendor.js:1
at Object.<anonymous> (vendor.js:1)
at e (vendor.js:1)
at Object.241 (main.js:1)
at e (vendor.js:1)
at window.webpackJsonp (vendor.js:1)
at main.js:1
- As ionic 3.x doesn't support the current angular version of 6.0.7 I next updated to the last angular5 version of 5.2.10. After I did this, every production build failed, because the angular AOT compiler will now fail the build because of the wrong TS version. After downgrading TS to 2.6.2 (the last supported TS version in angular5), I got this error message during production build:
TypeError: Cannot read property 'kind' of undefined
at nodeCanBeDecorated (/Users/mzrinck/projects/ionic-example/node_modules/typescript/lib/typescript.js:8376:36)
at nodeIsDecorated (/Users/mzrinck/projects/ionic-example/node_modules/typescript/lib/typescript.js:8396:16)
at Object.nodeOrChildIsDecorated (/Users/mzrinck/projects/ionic-example/node_modules/typescript/lib/typescript.js:8400:16)
at isDecoratedClassElement (/Users/mzrinck/projects/ionic-example/node_modules/typescript/lib/typescript.js:53700:23)
The cause of this is a typescript bug which is only fixed in TS 2.7.x, see angular/angular-cli#8434 The problem seems to be a bug when arrow functions are used in decorators (which typeorm uses frequently) and the angular AoT compiler triggers this.
That means this can't be fixed in a current ionic 3.x project as only ionic 4.x will officially support angular6 and TS >= 2.7.
Oh, and btw, the same happens when doing this in angular4 and TS 2.4.
- Even when upgrading to angular and rxjs to 6.0.7 (and including rxjs-compat package and ignoring ionic dependency warning) will not work. Development builds are working at first glance and prod builds work without any error butthe resulting application will not run as I only got this error:
vendor.js:1 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'Database' of undefined
TypeError: Cannot read property 'Database' of undefined
at n.<anonymous> (vendor.js:1)
at vendor.js:1
at Object.next (vendor.js:1)
at vendor.js:1
at new t (polyfills.js:3)
at l (vendor.js:1)
at n.createDatabaseConnectionWithImport (vendor.js:1)
at n.load (vendor.js:1)
at n.createDatabaseConnection (vendor.js:1)
at n.<anonymous> (vendor.js:1)
at n.<anonymous> (vendor.js:1)
This is a problem with the SqlJs driver (in its createDatabaseConnectionWithImport method). I don't know if its caused by angular6 but I doubt it. I guess this error would be thrown even in the other versions when they would work and it seems to be caused by optimizations maybe? I don't know, I'm not an expert enough.
In the end, there is currently no way to do ANY builds with --prod flag when I include typeORM to my project. As ionic 4.x seems to be released in the coming months, problem 1 and 2 will be fixed as TS 2.7 can be used then, but I'm not so sure about 3.