ionic-team / ionic-app-scripts

App Build Scripts for Ionic Projects
http://ionicframework.com/
MIT License
608 stars 303 forks source link

Cannot read property 'create' of undefined when I build with --prod #1115

Open squallliu opened 7 years ago

squallliu commented 7 years ago

Steps to reproduce:

  1. The service references AlertController in the custom module
  2. Create ionic project
  3. Use the service in the custom module
  4. build with --prod

set "ionic_manual_treeshaking" to false it work.

danbucholtz commented 7 years ago

@squallliu,

Can you upload a sample repo that I can work off of?

Thanks, Dan

squallliu commented 7 years ago

@danbucholtz

  1. clone https://github.com/squallliu/app-scripts-test1
  2. npm i
  3. build with --prod
MarkChrisLevy commented 7 years ago

@danbucholtz I can confirm that issue as well - my app has a dependency to a library with custom ionic components. Those components extends ionic basic components. When ionic_manual_treeshaking is true, classes of ionic components that are not used directly within the app, are not accessible for components from the library... When ionic_manual_treeshaking is set to false it works, but css files is not generated properly (reported in another issue).

JoeMeeks commented 7 years ago

Similar situation, but in my case ModalController and ToastController are undefined in my UIService provider with a --prod build but they exist without the prod flag (AlertController exists in both cases). If I attempt a build with --manualTreeshaking false then the app errors entirely with Uncaught SyntaxError: Unexpected token var from main.js .

"ionic-angular": "3.6.1",
"@ionic/app-scripts": "2.1.4",
JoeMeeks commented 7 years ago

@squallliu @mmlleevvyy @danbucholtz AlertController and LoadingController were working in my app because they were imported and injected into at least one "page" component in the app. My workaround was to import & inject the following in app.component.ts and now they are no longer undefined in my UIService provider with --prod builds where --manualTreeshaking false is not specified (default true).

import { Platform, AlertController, LoadingController, ModalController, ToastController } from 'ionic-angular';  //import & inject these to prevent manualTreeshaking removal in --prod builds

    constructor(
        private platform: Platform,
        private alert: AlertController,
        private loading: LoadingController,
        private modal: ModalController,
        private toast: ToastController,
        public translate: TranslateService,
        public ui: UIService,
        private splashscreen: SplashScreen,
        private statusbar: StatusBar
    ) { ... }
;