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

不羁的心 提交于 2019-12-21 03:11:23

问题


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:

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

is it possible?


回答1:


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

Highly recommend you use tsconfig.json and then the -p compiler option to build your code. Look at: Compilation-Context

Setup

Here is the setup for using tsc with NPM scripts

init

npm init
npm install typescript --save

And then in your package.json add some scripts:

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

Use

  • For build only: npm run build
  • For building + live watching : npm start

Enjoy 🌹




回答2:


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

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.



来源:https://stackoverflow.com/questions/31749952/how-to-run-typescript-compiler-as-a-package-json-script-without-grunt-or-gulp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!