Yet another external Javascript question


#1

Hello folks,

Pardon the newbie question. I am attempting to build an ios app using ionic. It requires a specialized javascript library not available as an npm/angular package. So I assume I have to include it as a plain javascript file.

As a first step, in my index.html, I include it like so

...
  <script src="assets/js/mealTimes.js"></script>
</head>

I’ve put the file inside assets/js directory. The library itself has two functions and its meant to be used like so:

function mealTimes(method) { ... }
var DMath = { ... }

// to use:
var meals = new mealTimes();

After I include the file in index.html, I use the library in home.ts like so:

export class HomePage {
    @ViewChild(Content) content: Content;
    theTimes: any;

   constructor(...) {
      this.getUpdatedTimes();
   }

   getUpdatedTimes() {
        this.theTimes = new mealTimes();
   }

}

Next, I run the following and everything seems to work fine in the simulator :+1:

ionic cordova emulate ios --livereload --c

Finally, I try to run prod and I get failures: :-1:

ionic cordova build ios --prod

Failures are:

[INFO] Running app-scripts build: --prod --platform ios --target cordova
       
[09:09:45]  build prod started ... 
[09:09:45]  clean started ... 
[09:09:45]  clean finished in 3 ms 
[09:09:45]  copy started ... 
[09:09:45]  deeplinks started ... 
[09:09:45]  deeplinks finished in 24 ms 
[09:09:45]  ngc started ... 
[09:09:47]  copy finished in 1.99 s 
[09:09:49]  typescript: src/pages/home/home.ts, line: 111 
            Cannot find name 'mealTimes'. 

Error: Failed to transpile TypeScript

The failure at line 111 it refers to is:

        this.theTimes = new mealTimes();

Any assistance much appreciated. Why would it work in simulator by fail to build in prod?


#2

You can frig it a little. On your home.ts or wherever you want to use it, just beneath your imports

declare var mealTimes: any;

That’ll give the transpiler something to latch on to.


#3

Hey! That actually worked! Thanks much @Judgewest2000!!

Now on to trying to figure out why simulator running through xocde gives white screen of death. :slight_smile: