0

我正在尝试使用 google 闭包编译一个相对复杂的 angular5 项目。

首先我使用tsickle将代码编译成对 google 闭包友好的语法,然后我尝试使用 google 闭包来创建最终包。

不幸的是,tsickle 似乎创建了一个与 google 闭包不兼容的模块格式,对于我拥有的所有模块,我都收到以下错误:

./path/to/my.component.js:8: ERROR - The body of a goog.module cannot reference this.
var __metadata = (this && this.__metadata) || function (k, v) {

但是,考虑到最近 ngc -> tsickle 使用 angular5 切换以帮助构建闭包的意图,我认为它应该可以工作。

检查一点 my path/to/component.js,我在一开始就发现了这个:

goog.module('target.path.to.MyComponent');var module = module || {id: 'target/path/to/MyComponent.js'};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};

看来,打字稿编译器(由 tsickle 内部调用)将此代码放在所有模块之前,它在 tsickle 不知情的情况下执行此操作,并且使模块格式与 google 闭包不兼容!

该怎么办?如何解决它?

4

1 回答 1

1

经过几个小时的谷歌搜索,包括对 tsc 源代码的一些挖掘,我找到了解决方案。

Typescript 创建此标头本质上是作为缺少某些反射支持的解决方法。它将这个东西包含到所有 .js 文件中,它仍然未使用,只会增加最终包的大小。

在“旧” ngc时代(角度 2.x - 4),此代码片段没有引起问题。但它与 google 闭包不兼容,因为它的模块格式不允许this直接从模块中使用。

TypeScript 可以选择使用

--noEmitHelpers=true

旗帜。由于tsickle参数处理不是很强,最好插入一个

"noEmitHelpers": true,

进入你的tsconfig.json.

于 2017-11-23T17:51:23.577 回答