TypeScript/JavaScript压缩 [英] TypeScript/JavaScript minification

查看:880
本文介绍了TypeScript/JavaScript压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用TypeScript编写的Web应用程序,到某个时候,已编译的.ts文件的所有输出都将被压缩.

I have a web application written in TypeScript and at some point, all the output of the compiled .ts files will be minified.

现在,在我的代码中的某些时候,我会执行以下操作:

Now, at some point in my code I do something like this:

class Test {
    testProperty: any;
    constructor() {
        this.getMethods();
    }
    private getMethods(){
        for (var method in this) {
            if (method.endsWith("Method")) {
                alert(method);
            }
        }
    }

    doSomethingMethod(){}
}

var x = new Test();

虽然我自己还没有做任何测试,但是从我读过的书中来看(此答案的第一条评论),即在最小化时,所有函数名称,属性等都将更改.

While I haven't done any test myself, but from what I've read (this blog post, the first comment of this SO answer ) that when minifying, all names of functions, properties and so on are altered.

.ts文件输出的缩小会改变这种获取方法名称的方式吗?是否取决于缩小的类型?

Will the minification of the output of the .ts files alter this way of getting the method names? Does it depend on the type of minification?

我在一个缩小器( jscompress.com )中引入了.ts文件的输出,结果如下:

I introduced the output of the .ts file above in a minifier (jscompress.com) and the result is the following:

var Test=function(){function t(){this.getMethods()}return t.prototype.getMethods=function(){for(var t in this)t.endsWith("Method")&&alert(t)},t.prototype.doSomethingMethod=function(){},t}(),x=new Test;

如您所见,此缩小版本不会影响变量名称,并且代码保留了其预期的功能.

As you can see, this minified version doesn't affect the variable names and the code kept its intended functionality.

我想知道的是:是否存在用于更改对象命名的缩小工具?

What I want to know is : Are there minifying tools that alter the naming of your objects?

推荐答案

正如其他答案中已经提到的那样-大多数JavaScript简化程序不会更改公开可用符号的名称,因为它可能会破坏外部代码.

As already stated in other answers - most JavaScript minifiers won't change names of the publicly available symbols as it may break external code.

但是,某些压缩程序提供了更激进的压缩模式,这些压缩模式实际上可以重命名外部范围可能可见的符号,除非您使用特殊注释或其他技术来保护它们.例如,以高级编译模式查看Google Closure编译器- https://developers.google.com/closure/compiler/docs/api-tutorial3

However, some minifiers offer more aggressive compression modes that may actually rename symbols that are potentially visible to the outer scope unless you protect them using special comments or some other technics. For example take a look at the Google Closure compiler in advanced compilation mode - https://developers.google.com/closure/compiler/docs/api-tutorial3

这篇关于TypeScript/JavaScript压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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