Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 251
SuperUberDuper Avatar asked Jul 31 '15 15:07

SuperUberDuper


People also ask

How do you run a ts index?

Use the ts-node package to run a TypeScript file from the command line, e.g. npx ts-node myDirectory/myFile. ts . The ts-node command will transpile the TypeScript file to JavaScript and run the code in a single step.

Which command is used to manually compile a TypeScript?

As you know, TypeScript files can be compiled using the tsc <file name>. ts command.

Does TypeScript require compilation?

The TypeScript compiler compiles these files and outputs the JavaScript with . js extension by keeping the same file name as the individual input file. The TypeScript compiler also preserves the original file path, hence the . js output file will be generated where the input file was in the directory structure.


2 Answers

"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 .",     "start": "npm run build -- -w" }, 

Use

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

Enjoy 🌹

like image 105
basarat Avatar answered Oct 08 '22 18:10

basarat


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.

like image 25
Zanon Avatar answered Oct 08 '22 17:10

Zanon