Typescript Setup

Typescript:

1. Install typescript:

npm install typescript

2. Command to convert a typescript file(*.ts) to a javascript file (*.js)

tsc file_name.ts

3. Initialize npm

npm init

4. Install lite-server

npm install lite-server --save-dev

5. Add a script command to package.json to start the lite-server

"start": "lite-server"

5. Initialize tsc

tsc --init

Add the following to the tsconfig.json if it doesn't exist already:

"sourceMap": true,
"exclude": [
"node_modules"
]

6. To compile all typescript files to javascript simply run:

tsc

7. For Modules

npm install --save-dev ts-loader webpack

webpackconfig.js:

const path = require('path');

module.exports = {
  entry: './index.ts',
  devtool: 'inline-source-map',
  module: {
rules: [
  {
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
  }
]
  },
  resolve: {
extensions: [ ".tsx", ".ts", ".js" ]
  },
  output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
  }
};

Add script to package.json:

"build": "webpack"

You can now be able to run the new build command

npm run build


Comments

Popular posts from this blog

Set Up Babel and Webpack

Typescript + React Setup