javascript - 让 TypeScript 为闭包编译器导出正确的函数名称?

标签 javascript typescript minify google-closure-compiler

我有以下 typescript :

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 "/";
    }


}

现在我希望能够像这样使用此 typescript 的最小化和优化的 JavaScript:

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

但是,当我通过 google closure-compiler 运行它时(仅在高级模式下),漂亮的函数名称消失了,我有 CompanyName.HtmlTools.Cookie.f,这会破坏使用此脚本的所有其他脚本。

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

Cookie.createCookie = createCookie;

如果是

Cookie["createCookie"] = createCookie;

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

有什么方法可以让 TypeScript 将函数导出为

Cookie["createCookie"] = createCookie;

代替

Cookie.createCookie = createCookie;

?

命名空间也一样,应该是

})(window["CompanyName"]|| (window["CompanyName"] = {}));

代替

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

最佳答案

使用公共(public)父类(super class)来存储静态方法:

In TypeScript, static properties of classes are inherited, in Closure they are not. To fix this, we create a common superclass.

These fake superclasses don't actually exist and are present solely to simulate the behavior of goog.inherits(SubClass, SuperClass) in TypeScript definition files. Calling new SuperClass__Class or extends SuperClass__Class in non-declaration TypeScript code will generate a runtime error. You should use new SuperClass and extends SuperClass in non-declaration TypeScript code.

引用资料

关于javascript - 让 TypeScript 为闭包编译器导出正确的函数名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32697916/

相关文章:

javascript - 如何选择在 jQuery 中使用 .html() 添加的 HTML 元素

angularjs - 使用 TypeScript 创建自定义 Angular 过滤器

angular - 基于有效载荷而不是效果取消可观察

javascript - 输入范围拇指 css 在 chrome 中不起作用

javascript - 移动 Google 广告时,Firefox 中的 JavaScript 出现问题

javascript - 从开发者控制台填写 Angular 表单

angular - Typescript 下划线大小写翻译

AngularJS 服务配置值在缩小时被破坏

css - 缩小媒体查询?

javascript - 为什么我们使用缩小版的 Angularjs(即使我们添加了 angular.min.js.map,也要优先使用 angular.min.js 而不是 angular.js)