TypeScript 中的构建时/编译时装饰器

GtE*_*tEx 6 decorator typescript

我认为这不符合decoratorsTypeScript 的目的;但是,是否可以使用装饰器来更改输出js根据情况更改输出文件?

用例1:

export class Foo {
  @debugMode
  private log() { /* implementation */ }
  public doSomething() {
    this.log(arguments);
  }
}
Run Code Online (Sandbox Code Playgroud)

@debugMode装饰器在构建时(gulp 或 ..)在构建生产时删除该方法及其调用者,这样输出的 js 文件中就不会出现该方法的实现迹象。

或者

@debugMode
@Route('UC2')
export class UC1Page{ }
Run Code Online (Sandbox Code Playgroud)

@debugMode在生产中构建时,将从输出 js 中完全删除类定义及其装饰器。

或者

// this is not a valid syntax, only for the purpose of the usecase
// can achieve the same behavior using the first example
@debugMode doSomethingWeird(blah, blah);
@releaseMode sendGoogleAnalyticsInfo(blah, blah);
Run Code Online (Sandbox Code Playgroud)

第一行将在生产环境中删除,而第二行将在开发环境中删除。

用例 2(不一定在 AngularJS 中):

假设我们有以下文件夹结构:

[uc2]
-- uc2-component.ts
-- uc2-component.html
-- uc2-component.css
Run Code Online (Sandbox Code Playgroud)

以及以下代码app-component.ts

@debugMode
@Route('UC2')
export class UC1Page{ }
Run Code Online (Sandbox Code Playgroud)

无论天气与否,这都是一个不好的做法,是否可以实用地提供templateUrlHTMLstyleUrl和 CSS 文件的正确路径 - 因为它们共享目录?这是为了避免对这些文件的路径进行硬编码,从而在需要时可以自由更改这 3 个文件的位置(这样我们只需更改文件导入的路径ts)。