NPM/CLI

Almost all web projects use NPM. NPM is the mechanism we used to fetch TypeScript. This one creates package.json at the root of your project and can be used to launch TypeScript directly. This is possible because TypeScript has a CLI called tsc (TypeScript compiler).

NPM configuration has a section named scripts where you can add any command you want. You can create a build one that invokes tsc. Without any parameters, tsc uses tsconfig.json at the root of your project. In the following snippet, the "build" script is defined. To run the command, the use of the run command of NPM is needed, that is, npm run build:

"scripts": {
"build": "node_modules/typescript/bin/tsc"
},

With a TypeScript configuration file that specifies the source map, the rootDir, and outDir the result will be the same as Gulp and Grunt (different from webpack since it won't be bundled):

{
"compilerOptions": {
"rootDir": "src",
"outDir": "build",
"sourceMap": true
}
}

This is often not the preferred configuration because of how simplistic and limited it is. However, it's possible to have several commands executed one after the other, using the double ampersand (&&) to create a chain of commands. This option is fast, doesn't require any dependency on NPM libraries, and is often enough to get started at a basic level.

The advantage of the NPM and CLI approach is that TypeScript can be executed easily. Hence, if you have a custom build system you can easily plug TypeScript by invoking the CLI.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.120.6