Typescript TSC CommandLine - 防止 TSC 观看
Typescript TSC CommandLine - Prevent TSC from watching
起初:我知道命令行中tsc
和tsc -w
的区别。这不是问题,而是比较特殊的情况:
在 VS Code 的 tsconfig 中,我通过设置 "watch":true
启用了观察器。这是绝对想要的。但现在的问题是:
我有一些脚本应该安装整个 VS Code 项目环境(因此是特定项目本身)并希望项目最初由 tsc 编译。但现在它以监视模式编译它,因为 tsconfig 要求这样做。我可以在这里破例吗?最初在 bat 文件中编译时,我希望 tsc 不会自动将 watch 选项设置为 true,而是在忽略 tsconfig 中的 watching 选项打开或关闭时编译它。
有办法实现吗?
谢谢!
终于用windows的重命名命令解决了:
rem Rename the tsconfig to prevent the tsc-task from watching
rename "tsconfig.json" "temp-hide.json"
rename "setup-config.json" "tsconfig.json"
tsc
rem Undo renaming since the task finished
rename "tsconfig.json" "setup-config.json"
rename "temp-hide.json" "tsconfig.json"
在我的安装过程中,我将 tsconfig.json 重命名为 temp-hide.json (因为 tsconfig 包含选项 watch)
然后将一个setup-config.json重命名为tsconfig.json,这样这个文件
改为使用。
当所有文件都重命名后,我终于执行了命令 tsc,并且
执行该命令后,我将所有内容重命名为原来的名称
文件名。
您可以比基于重命名的解决方案做得更好。现在,问题是当您调用 tsc
时,-w
(--watch
) 标志被您的 tsconfig.json
中指定的 compilerOptions.watch
值覆盖(位于与调用 tsc
脚本的目录相同)。我们将使用 TypeScript 2.1 中引入的功能:configuration inheritance.
基本配置,tsconfig.json
(一次性模式)
在这里写下典型的行为。这将是我们的基础(默认)tsconfig
文件。
{
"compilerOptions": {
"watch": false,
"module": "commonjs",
"target": "es6",
"sourceMap": true,
"inlineSources": true,
"strictNullChecks": true
}
}
扩展配置tsconfig_w.json
(观看模式)
现在我们为它写一个扩展,其中单个属性是变化:compilerOptions.watch
被覆盖为true
。
{
"extends": "./tsconfig.json",
"compilerOptions": {
"watch": true
}
}
tsc
的命令行调用
之前,您在调用 tsc
时没有提供任何进一步的参数。现在,您应该明确指定它应该从哪个 tsconfig
读取。
一次性:tsc --project tsconfig.json
(这是它将使用的默认文件,但让我们明确一点)。
观看:tsc --project tsconfig_w.json
注意:--project
标志是在 TypeScript 1.8 中引入的。
为方便起见的可选额外内容:package.json
我倾向于将这些作为方便的脚本写入我的 package.json
:
"scripts": {
"lib": "./node_modules/.bin/tsc --project tsconfig.json",
"libw": "./node_modules/.bin/tsc --project tsconfig_w.json"
}
然后您只需 运行 npm run lib
或 npm run libw
取决于您需要的脚本。
注意:我在本地而不是全局指定了 TypeScript 的路径(因为我没有在全局安装它)。
起初:我知道命令行中tsc
和tsc -w
的区别。这不是问题,而是比较特殊的情况:
在 VS Code 的 tsconfig 中,我通过设置 "watch":true
启用了观察器。这是绝对想要的。但现在的问题是:
我有一些脚本应该安装整个 VS Code 项目环境(因此是特定项目本身)并希望项目最初由 tsc 编译。但现在它以监视模式编译它,因为 tsconfig 要求这样做。我可以在这里破例吗?最初在 bat 文件中编译时,我希望 tsc 不会自动将 watch 选项设置为 true,而是在忽略 tsconfig 中的 watching 选项打开或关闭时编译它。
有办法实现吗?
谢谢!
终于用windows的重命名命令解决了:
rem Rename the tsconfig to prevent the tsc-task from watching
rename "tsconfig.json" "temp-hide.json"
rename "setup-config.json" "tsconfig.json"
tsc
rem Undo renaming since the task finished
rename "tsconfig.json" "setup-config.json"
rename "temp-hide.json" "tsconfig.json"
在我的安装过程中,我将 tsconfig.json 重命名为 temp-hide.json (因为 tsconfig 包含选项 watch) 然后将一个setup-config.json重命名为tsconfig.json,这样这个文件 改为使用。
当所有文件都重命名后,我终于执行了命令 tsc,并且 执行该命令后,我将所有内容重命名为原来的名称 文件名。
您可以比基于重命名的解决方案做得更好。现在,问题是当您调用 tsc
时,-w
(--watch
) 标志被您的 tsconfig.json
中指定的 compilerOptions.watch
值覆盖(位于与调用 tsc
脚本的目录相同)。我们将使用 TypeScript 2.1 中引入的功能:configuration inheritance.
基本配置,tsconfig.json
(一次性模式)
在这里写下典型的行为。这将是我们的基础(默认)tsconfig
文件。
{
"compilerOptions": {
"watch": false,
"module": "commonjs",
"target": "es6",
"sourceMap": true,
"inlineSources": true,
"strictNullChecks": true
}
}
扩展配置tsconfig_w.json
(观看模式)
现在我们为它写一个扩展,其中单个属性是变化:compilerOptions.watch
被覆盖为true
。
{
"extends": "./tsconfig.json",
"compilerOptions": {
"watch": true
}
}
tsc
的命令行调用
之前,您在调用 tsc
时没有提供任何进一步的参数。现在,您应该明确指定它应该从哪个 tsconfig
读取。
一次性:
tsc --project tsconfig.json
(这是它将使用的默认文件,但让我们明确一点)。观看:
tsc --project tsconfig_w.json
注意:--project
标志是在 TypeScript 1.8 中引入的。
为方便起见的可选额外内容:package.json
我倾向于将这些作为方便的脚本写入我的 package.json
:
"scripts": {
"lib": "./node_modules/.bin/tsc --project tsconfig.json",
"libw": "./node_modules/.bin/tsc --project tsconfig_w.json"
}
然后您只需 运行 npm run lib
或 npm run libw
取决于您需要的脚本。
注意:我在本地而不是全局指定了 TypeScript 的路径(因为我没有在全局安装它)。