【发布时间】:2017-08-29 12:11:14
【问题描述】:
我刚刚使用 CLI 创建了一个新的 Angular 4 项目:ng new test
版本:
@angular/cli: 1.0.0
node: 6.10.0
os: win32 x64
@angular/common: 4.0.1
@angular/compiler: 4.0.1
@angular/core: 4.0.1
@angular/forms: 4.0.1
@angular/http: 4.0.1
@angular/platform-browser: 4.0.1
@angular/platform-browser-dynamic: 4.0.1
@angular/router: 4.0.1
@angular/cli: 1.0.0
@angular/compiler-cli: 4.0.1
但是,tree-shaking 无法正常工作,因为我未使用的类 FooBar 仍在 main.*.js 文件中。
我的示例组件 TS 文件(FooBar 不应该在输出中):
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
}
export class FooBar {
foo = "hello";
}
我尝试使用汇总(如文档中所述),但效果不佳...
有没有一种简单的方法来启用 tree-shaking? (我希望在通过 CLI 创建项目时默认启用它)。
更新:我正在使用ng build --prod,它仍然没有动摇..
【问题讨论】:
-
你在使用什么
ng命令? -
我正在使用
ng build --prod,它仍然没有动摇.. -
CLI 是否甚至使用摇树?从 angular.io 文档中关于树摇晃的食谱中,它使用汇总来进行树摇晃。但是查看 CLI 的源代码,package.json 中没有汇总依赖项。除非他们用别的东西来摇树。我试图弄清楚同样的事情。
-
正如 R. Richards 在他的更新中所说,转到 Angular.Io 页面进行 AoT 编译和 Tree Shaking Here。角度文档写得很好,所以你不应该有任何问题。这是我最近在 Angular 4 的项目中实现 AoT 和 Tree Shaking 时使用的。
-
如果使用
ng-cli构建项目,默认支持。
标签: angular angular4 tree-shaking