How to verify if AoT is working or not [ Webpack 2 , Angular 2 ]?
Asked Answered
T

3

18

In my sample Angular 2 SPA , I have used Webpack 2 in order to

  1. Bundle all my js files
  2. Implement "Tree Shaking" to remove dead code and reduce bundle js file size
  3. and to implement Ahead-of-time compilation to reduce the bundle js file size further.

I was able to achive "1" and "2" by creating a webpack.config.js file , and below are the contents of this file

'use strict';
const webpack = require('webpack');

module.exports = {
    devtool: 'source-map',
    entry: './src/main.js',       
    plugins: [

    new webpack.optimize.UglifyJsPlugin({
        minimize: true,
        compress: false
    })
    ],
    output: {
        filename:'./src/bundle.js'
    }
}

"webpack.optimize.UglifyJsPlugin" plugin which does the Tree Shaking and minfication , reduced my bundle.js file size from 3 mb to 1 mb.

Next in order to implement AoT compilation , I have used @ngtools/webpack , and below is the modified webpack.config.js file with AoT related code.

'use strict';
const webpack = require('webpack');
const AotPlugin = require('@ngtools/webpack').AotPlugin;

module.exports = {
    devtool: 'source-map',
    entry: './src/main.js',
    module: {
        rules: [
            {
                test: /\.ts$/,
                loader: '@ngtools/webpack'
            }
        ]
    },
    plugins: [

    new webpack.optimize.UglifyJsPlugin({
        minimize: true,
        compress: false
    }),
     new AotPlugin({         
          tsConfigPath: 'src\\tsconfig.json',
          mainPath: 'main.ts',         
          "skipCodeGeneration": true
      }), 
    ],
    output: {
        filename:'./src/bundle.js'
    }
}

After AoT the size of bundle.js file is still same(1 mb).

Now my question is how can I check/know whether AoT compilation is working or not ?

Trimurti answered 21/2, 2017 at 5:48 Comment(1)
None of the answers explain what mistake I have done if there is any in the 3 steps mentioned , or what needs to be modified ...Trimurti
J
18

The best way to make sure that your Angular project is built using AOT is to get your hands dirty and take a look into the compiled source code.

What does AOT really do behind the scenes? AOT is compiling your HTML into JS functions which can be statically analysed (and later tree shaked).

So just take a part of your HTML template and look for it inside your compiled JS. For example, here's my login.component.html in one of my projects:

<md-card>
  <form [formGroup]="loginForm" (ngSubmit)="onSubmit(loginForm)" fxLayout="column">
    <md-input-container class="margin-top-x1">
      <span mdPrefix>
          <md-icon color="primary">person</md-icon>
        </span>
      <input mdInput type="text" placeholder="Username" formControlName="username" required>
    </md-input-container>

    <md-input-container class="margin-top-x1">
      <span mdPrefix>
          <md-icon color="primary">vpn_key</md-icon>
        </span>
      <input mdInput type="password" placeholder="Password" formControlName="password" required>
    </md-input-container>

    <div fxLayout="row" fxFlex="100%" fxLayoutAlign="start center" class="form-error" *ngIf="users.connectionFailed">
      <md-icon color="warn">error</md-icon>
      <p>Username and password do not match</p>
    </div>

    <button md-raised-button color="primary" class="margin-top-x1" [disabled]="loginForm.invalid || users.isConnecting || users.isConnected">
      <span *ngIf="!users.isConnecting && !users.isConnected">
        Log in
      </span>

      <span *ngIf="users.isConnecting || users.isConnected" fxLayout="row" fxLayoutAlign="center center">
        Logging in <md-spinner></md-spinner>
      </span>
    </button>
  </form>
</md-card>

Grab something easy to search, that will probably have few occurrences. For example here, the md-icon vpn_key. When I search in dist folder once built with AOT, I can find that my view has been compiled to:

enter image description here

And how would it be without AOT?

Like that: enter image description here

We can see that the whole template hasn't been compiled into JS without AOT !

Potential problem with your build system
When you say:

1) Bundle all my js files
2) Implement "Tree Shaking" to remove dead code and reduce bundle js file size
3) and to implement Ahead-of-time compilation to reduce the bundle js file size further.

If you bundle and implement Tree Shaking before the AOT compilation it won't work of course.

OFF TOPIC:
Bundle size seems to mater for you and if you're not already using Angular v4 I'd advise you to give a try. Few/no breaking changes yet (4.0.0-rc.2 as I'm writing) and the template compiler has been rewritten. It now generate less code for the views (~40 to ~50% less than Angular v2.x)

Jackhammer answered 10/3, 2017 at 10:7 Comment(3)
Somehow confusing: Angular 2 vs. Angular v4. But I found a blogpost that explains it quite well.Paisley
@JanHommes Angular v4.x won't probably break your code ;) Just make it smaller at compilation :)Jackhammer
yes, that one I understand from your post. But I did not know that there is already a v4. But its due the fact they changed to semantic versioning. Also my understanding is that v2.4.3 is the same as v4.0.0-beta3.Paisley
M
4

After AOT compilation, treeshaking should remove @angular/compiler (which is respectively used during JIT. And if u do simple size analysis you will learn that almost 40% of Angular 2 is Compiler so sazie

Mcgann answered 22/2, 2017 at 18:57 Comment(0)
H
4

You can use source-map-explorer to view the content of your main.bundle.js and you can compare the AOT bundle with the non-AOT bundle. Hope it helps.

Hubsher answered 8/3, 2017 at 11:59 Comment(1)
I use source-map-explorer and also webpack-bundle-analyzer, the latter is able to show multiple bundles at the same time.Disassembly

© 2022 - 2024 — McMap. All rights reserved.