How to compile typescript using npm command?

前端 未结 3 938
北海茫月
北海茫月 2021-01-31 10:13

Well I just wanted to know is there any command which will directly compile the typescript code and get the output. Right now, what i am doing is,every time when i make changes

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-31 10:38

    You can launch the tsc command (typescript compiler) with --watch argument.

    Here is an idea :

    • Configure typescript using tsconfig.json file
    • Run tsc --watch, so every time you change a .ts file, tsc will compile it and produce the output (let say you configured typescript to put the output in ./dist folder)
    • Use nodemon to watch if files in ./dist have changed and if needed to relaunch the server.

    Here are some scripts (to put in package.json) that can help you to do it (you will need to install the following modules npm install --save typescript nodemon npm-run-all rimraf)

    "scripts": {
        "clean": "rimraf dist",
        "start": "npm-run-all clean --parallel watch:build watch:server --print-label",
        "watch:build": "tsc --watch",
        "watch:server": "nodemon './dist/index.js' --watch './dist'"
    }
    

    Then you just need to run npm start in a terminal

提交回复
热议问题