简体   繁体   English

如何验证AoT是否正常工作[Webpack 2,Angular 2]?

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

In my sample Angular 2 SPA , I have used Webpack 2 in order to 在我的示例Angular 2 SPA中,我使用了Webpack 2

  1. Bundle all my js files 捆绑我所有的js文件
  2. Implement "Tree Shaking" to remove dead code and reduce bundle js file size 实现“Tree Shaking”以删除死代码并减少bundle js文件大小
  3. and to implement Ahead-of-time compilation to reduce the bundle js file size further. 并实现Ahead-of-time编译以进一步减少bundle js文件的大小。

I was able to achive "1" and "2" by creating a webpack.config.js file , and below are the contents of this file 通过创建webpack.config.js文件,我能够获得“1”和“2”,以下是此文件的内容

'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. “webpack.optimize.UglifyJsPlugin”插件执行Tree Shaking和minfication,将我的bundle.js文件大小从3 mb减少到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. 接下来为了实现AoT编译,我使用了@ ngtools / webpack ,以下是带有AoT相关代码的修改过的webpack.config.js文件。

'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). 在AoT之后,bundle.js文件的大小仍然相同(1 mb)。

Now my question is how can I check/know whether AoT compilation is working or not ? 现在我的问题是如何检查/知道AoT编译是否有效?

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. 确保使用AOT构建Angular项目的最佳方法是弄脏手,并查看已编译的源代码。

What does AOT really do behind the scenes? AOT在幕后真的做了什么? AOT is compiling your HTML into JS functions which can be statically analysed (and later tree shaked). AOT正在将您的HTML编译成JS函数,这些函数可以进行静态分析(以及后来的树形抖动)。

So just take a part of your HTML template and look for it inside your compiled JS. 因此,只需获取HTML模板的一部分,然后在已编译的JS中查找它。 For example, here's my login.component.html in one of my projects: 例如,这是我的一个项目中的login.component.html

<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 . 例如,这里是md-icon vpn_key When I search in dist folder once built with AOT, I can find that my view has been compiled to: 当我在使用AOT构建的dist文件夹中搜索时,我发现我的视图已被编译为:

在此输入图像描述

And how would it be without AOT ? 没有AOT会怎么样?

Like that: 像那样: 在此输入图像描述

We can see that the whole template hasn't been compiled into JS without AOT ! 我们可以看到,没有AOT,整个模板还没有被编译成JS!

Potential problem with your build system 构建系统的潜在问题
When you say: 当你说:

1) Bundle all my js files 1)捆绑我所有的js文件
2) Implement "Tree Shaking" to remove dead code and reduce bundle js file size 2)实现“Tree Shaking”以删除死代码并减少bundle js文件大小
3) and to implement Ahead-of-time compilation to reduce the bundle js file size further. 3)并实现Ahead-of-time编译以进一步减少bundle js文件的大小。

If you bundle and implement Tree Shaking before the AOT compilation it won't work of course. 如果在AOT编译之前捆绑并实现Tree Shaking 它当然不会起作用。

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. 捆绑大小似乎对你来说很重要,如果你还没有使用Angular v4,我建议你试一试。 Few/no breaking changes yet (4.0.0-rc.2 as I'm writing) and the template compiler has been rewritten. 很少/没有重大变化(我正在编写4.0.0-rc.2)并且模板编译器已被重写。 It now generate less code for the views (~40 to ~50% less than Angular v2.x) 它现在为视图生成更少的代码(比Angular v2.x少40%到约50%)

在AOT编译之后,treeshaking应该删除@ angular / compiler(在JIT期间分别使用。如果你做简单的大小分析,你会发现几乎40%的Angular 2是编译器所以sazie

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. 您可以使用source-map-explorer查看main.bundle.js的内容,并且可以将AOT包与非AOT包进行比较。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM