打字稿和谷歌关闭 [英] Typescript and Google Closure

查看:69
本文介绍了打字稿和谷歌关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Typescript命令(tsc)创建一个包含所有平台类的Javascript文件。

I use Typescript command (tsc) to create a single Javascript file containing all plateform classes.

tsc "./Main.ts" -out "./script/myProject_debug.js" --declarations

然后,我想用Google Closure(compiler.jar)对此文件进行模糊处理:

Then, I want to obfuscate this file with Google Closure (compiler.jar) like this :

java -jar ./compiler/compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --js "./script/myProject_debug.js" > "./script/myProject.js".

但是当我执行生成的混淆/优化代码时,我得到以下错误:Uncaught TypeError:不能读取未定义的属性'prototype'

But when I execute the resulting obfuscated/optimized code, I got this following error : Uncaught TypeError: Cannot read property 'prototype' of undefined

哪个匹配以下非混淆的JS代码(由tsc命令生成):

Which matches the following non-obfuscated JS code (generated by tsc command) :

var __extends = this.__extends || function (d, b) {
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
}

此部分用于翻译extendsTypescript关键字,相当于b未定义。

This part is used to translate the "extends" Typescript keyword and the equivalent of b is undefined.

是否有人遇到类似的错误或/并获得一个解决方案,可以使用Typescript编译的文件与Google Closure混淆?

Is anyone got similar error or/and get a solution to be able to obfuscate with Google Closure with a Typescript compiled file?

我尝试使用uglifyjs命令并且输出文件工作正常,但我想要完全混淆(类,参数,变量,方法等)。此外,欢迎Google Closure提供的额外优化。

I tried with uglifyjs command and the output file works perfectly, but I want total obfuscation (classes, args, variables, methods, etc). Also, the extra optimization provided by Google Closure would be welcome.

谢谢!

推荐答案

好的我发现了问题。

如前所述,b未定义:

var __extends = this.__extends || function (d, b) {
   function __() { this.constructor = d; }
   __.prototype = b.prototype;
   d.prototype = new __();
}

当打字稿编译成javascript时,如果你有一个项目名称空间但是在分离的文件中编写与此命名空间相关的所有类,Typescript在最终生成的js文件中执行以下操作:

When typescript "compile" into javascript, if you got one namespace by project but that you write all classes related to this namespace in separated files, Typescript do the following in the final generated js file :

var namespace;
(function (namespace) {

    var Class1 = (function (dependency) {
        [...]
        return Class1;
    })(namespace.dependency);

    namespace.Class1 = Class1;
})(namespace || (namespace= {}));

var namespace;
(function (namespace) {

    var Class2 = (function (dependency) {
        [...]
        return Class2;
    })(namespace.dependency);

    namespace.Class2 = Class2;
})(namespace || (namespace= {}));

var namespace;
(function (namespace) {

    var Main = (function (dependency) {
        [...]
        return Main;
    })(namespace.Class2);

    namespace.Main = Main;
})(namespace || (namespace= {}));

我不确切知道它是如何工作的,但某些地方google-closure-compiler删除了一些类,即使这段代码没有问题,JS可以处理它。因此,一些依赖项缺失,b未定义。

I don't know exactly how its works but somewhere google-closure-compiler removed some classes even if there is no problem with this code and JS can handle it. So some dependencies was missing and b was undefined.

所以我发现如果你声明你的命名空间如下,你将不再遇到错误(关闭将保留所有只要使用Main类或者在全局窗口对象中保留命名空间的引用,就可以在最终混淆的js文件中使用类:

So I found that if you declare your namespace like the following, you will not encounter the error anymore (closure will keep all your classes in the final obfuscated js file as long as the "Main" class is used or that you keep a reference of your namespace in the global window object) :

var namespace;
(function (namespace) {

    var Class1 = (function (dependency) {
        [...]
        return Class1;
    })(namespace.dependency);

    namespace.Class1= Class1;

    var Class2 = (function (dependency) {
        [...]
        return Class2;
    })(namespace.dependency);

    namespace.Class2= Class2;

    var Main = (function (dependency) {
        [...]
        return Main;
    })(namespace.Class2);

    namespace.Main = Main;
})(namespace || (namespace= {}));

我想我会在typescriptlang.org上打开一个问题。它顺便优化了生成的文件大小。

I think I will open an issue on typescriptlang.org. It's optimizing the generated file size by the way.

感谢您的回答!

这篇关于打字稿和谷歌关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆