获取TypeScript为closure-compiler导出正确的函数名?

时间:2015-09-21 14:33:50

标签: javascript typescript minify google-closure-compiler

我有以下TypeScript:

http://example.com/chat/test.htm

现在我希望能够使用最小化的& amp;优化了这个打字稿的JavaScript,如下所示:

namespace CompanyName.HtmlTools.Cookie
{

    export function eraseCookie(name:string, path:string)
    {
        createCookie(name, "", path, -1);
    }


    export function readCookie(name:string) 
    {
        var nameEQ:string = name + "=";
        var ca:string[] = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) 
        {
            var c:string = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) 
                return c.substring(nameEQ.length, c.length);
        }
        return null;
    }


    // http://stackoverflow.com/questions/14573223/set-cookie-and-get-cookie-with-javascript
    // http://blog.codinghorror.com/protecting-your-cookies-httponly/
    export function createCookie(name:string, value:string, path:string, days:number) 
    {
        var expires:string = "";

        if(!path)
            path = "/";

        if (days) 
        {
            var date:Date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            expires = "expires=" + date.toUTCString() + "; ";
        }

        var allValues = name + "=" + encodeURIComponent(value) + "; " + expires + "path=" + path + ";";
        if(window.location.protocol === 'https:')
            allValues += " secure;"

        document.cookie = allValues;
    }


    export function getDirName(path:string)
    {
        if(path === null) return '/';

        if(path.indexOf("/") !== -1)
        {
            var subs:string[] = path.split('/') //break the string into an array
            subs.pop() //remove its last element
            path = subs.join('/')  //join the array back into a string
            if(path === '')
                return '/';
            return path;
        }

        return "/";
    }


}

然而,当我通过谷歌闭包编译器(仅在高级模式下)运行它时,那么漂亮的函数名称消失了,我有了CompanyName.HtmlTools.Cookie.f,它打破了使用这个脚本的所有其他脚本。

现在,问题似乎是,在TypeScript转换器创建的未压缩的javaScript中,有这样的:

CompanyName.HtmlTools.Cookie.createCookie("name","value", "path", 2);

如果是

Cookie.createCookie = createCookie;

然后它将在高级模式下在Closure-Compiler中工作。

有没有办法让TypeScript将函数导出为

Cookie["createCookie"] = createCookie;

而不是

Cookie["createCookie"] = createCookie;

命名空间也一样,应该是

Cookie.createCookie = createCookie;

而不是

  

})(CompanyName ||(CompanyName = {}));

2 个答案:

答案 0 :(得分:0)

使用公共超类来存储静态方法:

  

在TypeScript中,类的静态属性是继承的,在Closure中它们不是。为了解决这个问题,我们创建了一个共同的超类。

     

这些伪超类实际上并不存在,仅用于模拟TypeScript定义文件中goog.inherits(SubClass, SuperClass)的行为。在非声明TypeScript代码中调用new SuperClass__Classextends SuperClass__Class将生成运行时错误。您应该在非声明TypeScript代码中使用new SuperClassextends SuperClass

<强>参考

答案 1 :(得分:-1)

  

Cookie [“createCookie”] = createCookie;

在闭包编译器上使用此^变通方法或名称变换。