简体   繁体   English

如何在多个文件的输出打字稿中优化模块名称空间,使用gulp

[英]How optimize module namespace in output typescript of multiple files, use gulp

I encountered a problem, if use methodology: one file - one class in typescript project, as in the example below. 如果使用方法学,我遇到一个问题: 一个文件-打字稿项目中的一个类 ,如下例所示。

File Greeter.ts 文件Greeter.ts

module App {
    export class Greeter {
        constructor(public greeting: string) { }
        public greet() {
            return "<h1>" + this.greeting + "</h1>";
        }
    };
}

File Program.ts 文件程序

module App {
    export class Program {
        private _greeter = new Greeter("Hello, world!");
        public main() {
            document.body.innerHTML = this._greeter.greet();
        }
    };
}

Result file 结果文件

var App;
(function (App) {
    var Greeter = (function () {
        // class code
    } ());
    App.Greeter = Greeter;
})(App || (App = {}));

var App;
(function (App) {
    var Program = (function () {
        // class code
    } ());
    App.Program = Program;
})(App || (App = {}));
// point of entry
var program = new App.Program();
program.main();

As can be seen duplication declaration App . 可以看出,重复声明App I would like exclude any repetition like this: 我想排除这样的重复:

var App;
(function (App) {
    var Greeter = (function () {
        // class code
    } ());
    App.Greeter = Greeter;

    var Program = (function () {
        // class code
    } ());
    App.Program = Program;
})(App || (App = {}));
// point of entry
var program = new App.Program();
program.main();

Otherwise, when a large amount of classes, it turns out a lot of excess code in the output file. 否则,当使用大量类时,会在输出文件中产生大量多余的代码。

Maybe I'm wrong somewhere? 也许我在某个地方错了?

----Update---- ----更新----

The project is building by gulp-concat and gulp-typescript , maybe exist package allowing avoid this? 该项目是由gulp-concatgulp-typescript构建的,也许存在允许避免这种情况的软件包?

example on github github上的例子

Otherwise, when a large amount of classes, it turns out a lot of excess code in the output file. 否则,当使用大量类时,会在输出文件中产生大量多余的代码。 Maybe I'm wrong somewhere? 也许我在某个地方错了?

No. This is by design. 不,这是设计使然。 Basically typescript allows each section to run in isolation. 基本上,打字稿允许每个部分独立运行。

Warning 警告

Please try and migration to modules . 请尝试并迁移到模块。 More https://basarat.gitbooks.io/typescript/content/docs/tips/outFile.html 更多https://basarat.gitbooks.io/typescript/content/docs/tips/outFile.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM