如何在没有 grunt 或 gulp 的情况下将 typescript 编译器作为 package.json 脚本运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31749952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:44:18  来源:igfitidea点击:

How to run typescript compiler as a package.json script without grunt or gulp

typescriptpackage.json

提问by SuperUberDuper

I don't want to use grunt or gulp to compile ts files. I just want to do it in my package.json something like this:

我不想使用 grunt 或 gulp 来编译 ts 文件。我只想在我的 package.json 中做这样的事情:

  "scripts": {
    "build": "tsc main.ts dist/"
  },

is it possible?

是否可以?

回答by basarat

"build": "tsc main.ts dist/"

"build": "tsc main.ts dist/"

Highly recommend you use tsconfig.jsonand then the -pcompiler option to build your code. Look at: Compilation-Context

强烈建议您使用tsconfig.json然后-p编译器选项来构建您的代码。看:编译上下文

Setup

设置

Here is the setup for using tscwith NPM scripts

这是tsc与 NPM 脚本一起使用的设置

init

在里面

npm init
npm install typescript --save

And then in your package.json add some scripts:

然后在你的 package.json添加一些脚本

"scripts": {
    "build": "tsc -p ./src",
    "start": "npm run build -- -w"
},

Use

  • For build only: npm run build
  • For building + live watching : npm start
  • 仅用于构建: npm run build
  • 对于建筑 + 现场观看: npm start

Enjoy

享受

回答by Zanon

If you want to compile and run, you can use the ts-node module.

如果要编译运行,可以使用ts-node 模块

npm install --save-dev ts-node 
npm install --save-dev typescript

And run with:

并运行:

"scripts": {
    "start": "ts-node index.ts"
},

All other typescripts files that index.ts has imported (along with imports from index.ts dependencies) will be compiled and executed.

index.ts 导入的所有其他打字稿文件(以及从 index.ts 依赖项导入)将被编译和执行。