"Ahead Of Time" Compilation using Webpack

You can compile the app in the browser, at runtime, as the application loads, using the Just-in-Time (JiT) compiler as we did until now. But there is another way called "AHEAD-OF-TIME" COMPILATION which as said in Angular2 official site: "radically improves performance". Here we convert the components and templates to executable JavaScript files before runtime.


Steps:

  1. Create your project folder structure.
  2. Install and configurate all required modules.
  3. Build and run the app

Step 1 - Create your root project folder

The root folder contains our index.html, a few configuration files, and the app folder which holds the main content of the application.


 /root
    /app
        /app.component.ts
        /app.module.ts
        /main.ts
        /other source files
    /index.html
    /package.json
    /tsconfig.json
    /tsconfig.aot.json
    /webpack.config.js
                        

Note: Structure may vary based on your application needs.

Step 2 - Install and configurate all required modules

Apart from the package.json and tsconfig.json you will need tsconfig.aot.json and webpack.config.js files.
The tsconfig.aot.json is a normal tsconfig.json file, but with "AoT"-oriented settings.
Let's take a look at the config files:


package.json

When we type npm start in our CLI it executes both the commands. We just need to include the bundled file in or HTML and run it.


tsconfig.json


tsconfig.aot.json

What's really new is the ngc section called angularCompilerOptions. Here we say to the compiler to skip generating metadata files with the compiled application. We have also set "rootDir" which means all of our source files must be inside the folder named "app". For more information about АОТ please refer to the official "AHEAD-OF-TIME" COMPILATION cookbook.


webpack.config.js

For more information about Webpack please refer to the official guide.

Step 3 - Build and run the app

I. Create index.html

Add the needed references:

Add the <my-app></my-app> tag to the body of index.html. That's the tag where we initialize the main component.
Then import the bundled file we received after running the npm start command.

II. Create app.module.ts

III. Create main.ts

IV. Create app.component.ts

Make a reference to jqwidgets.d.ts. It contains all the TypeScript definitions for the widgets.

Import the Angular2 key components - Component, ViewChild, AfterViewInit. Then import the needed jQWidgets components.

@Component decorator:

Now it's time to create the AppComponent class.

1) @ViewChild: It holds the reference to the widget.

2) ngAfterViewInit: All widgets have a method called createWidget that accepts either no arguments or just one - an object containing the desired settings for the widget. If there is no argument then the widget is created through attributes.
For more information about creating jQWidgets through attributes please refer to this guide.

Events Methods & Properties

I. Events

We need to set an additional attribute to the angularWidget tag in order to bind to the event - onDrawEnd

Note: Event Names in the Angular 2 Components are the same as the Event Names in the Javascript Widgets.
The only thing you need to do is to put "on" before the Javascript widget event name and upperCase it's first letter.

II. Methods & Properties


Every widget have a method setOptions which accepts a object as an argument. This object contains widget settings.

Example Files

I. index.html

II. app.component.ts