使用打字稿在编译时插入今天的日期和版本号

Insert today's date and version number at compile time using typescript

我想要一个(自定义)版本号编译日期javascript 文件中已从 typescript.

编译

有没有办法在编译时在 TypeScript 中定义变量或计算日期函数?

@COMPANY = "myCompanyName";
@VERSION = "2.3.4.5"
// copyright © 2015 - {year(@TODAY)} @COMPANY
//
// version: {@VERSION} 
// release: {@TODAY}`

我希望 .js 文件中的输出是这样的:

// copyright © 2015 - 2019 MyCompanyName
//
// version: 2.3.4.5 
// release: 2019-09-16 10:00:00

尽管这条线没有明确定义,但 TypeScript 不是构建系统;它不支持任何类似的处理。

您更适合在构建过程中使用其他任何东西。有很多选择;最好的取决于您的构建工作流程。

如果您使用的是 Babel(common complementary build tool for TS projects), you could find that something that suit your needs like babel-plugin-add-header-comment or create one yourself

您还可以 use the compiler library 而不是命令行来创建文件,进行任何您想要的转换。

或者您可以在构建过程之后创建一个单独的步骤,对输出文件进行一些 string/regex 替换 - 通过 JS 或通过 bash 脚本。