TypeScript Build Tools

Build tools are programming utilities which help to automate the transformation and bundling of our source code into a single file. A build tool utility is used to build a new version of a program. Building means compiling, linking, and packaging the code into the executable form.

The Build tools are usually run on the command line, either in IDE or completely separate from it.

Build tools or build automation is the act of scripting or automating a variety of tasks that developers do in their day-to-day activities. These are:

  • Downloading dependencies.
  • Compiling source code into binary code.
  • Packaging that binary code.
  • Running tests.
  • Deployment to production systems.

Use of build tools

In small projects, the software developers manually invoke the build process, which is not a good practice for larger projects. It is because, in larger projects, it is very hard to keep track of what needs to be built, in what sequence and what should be the dependencies in the building process. So we use an automation tool which allows the build process to be more consistent.

Some of the standard build tools that can be integrated with TypeScript are:

  1. Browserify
  2. Duo
  3. Grunt
  4. Gulp
  5. Jspm
  6. Webpack
TypeScript Build Tools

Browserify

Use the Browserify plugin Tsify for compiling TypeScript files.

Install

Install Tsify by using the following command:

C:\. Command Prompt
$npm install Tsify

Using Command Line Interface

By using the following command, compile your code that saves the result in a file named bundle.js.

C:\. Command Prompt
browserify main.ts -p [ tsify --noImplicitAny ] > bundle.js

Using API

Example
snippet
var browserify = require("browserify");
var tsify = require("tsify");

browserify()
    .add("main.ts")
    .plugin("tsify", { noImplicitAny: true })
    .bundle()
    .pipe(process.stdout);

Duo

Install

Install the Duo plugin by using the following command:

C:\. Command Prompt
$npm install duo-typescript

Using Command Line Interface

By using the following command, compile your code that saves the result in a file named entry.ts.

C:\. Command Prompt
$duo --use duo-typescript entry.ts

Using API

Example
snippet
var Duo = require("duo");
var fs = require("fs")
var path = require("path")
var typescript = require("duo-typescript");

var out = path.join(__dirname, "output.js")

Duo(__dirname)
    .entry("entry.ts")
    .use(typescript())
    .run(function (err, results) {
        if (err) throw err;
        // Write compiled result to output file
        fs.writeFileSync(out, results.code);
    });

Grunt

Use grunt-ts plugin from Grunt for compiling TypeScript files.

Install

Install grunt-ts by using the following command:

C:\. Command Prompt
$npm install grunt-ts

Now, you need to include the Grunt config file named gruntfile.js in your project.

Example
snippet
module.exports = function(grunt) {
    grunt.initConfig({
        ts: {
            default: {
                src: ["**/*.ts", "!node_modules/**/*.ts"]
            }
        }
    });
    grunt.loadNpmTasks("grunt-ts");
    grunt.registerTask("default", ["ts"]);
};

Gulp

Use a gulp-typescript plugin for compiling TypeScript files.

Install

Install gulp-typescript by using the following command:

C:\. Command Prompt
$npm install gulp-typescript

Now, you need to include the Gulp config file named gulpfile.js in your project.

Example
snippet
var gulp = require("gulp");
var ts = require("gulp-typescript");

gulp.task("default", function () {
    var tsResult = gulp.src("src/*.ts")
        .pipe(ts({
              noImplicitAny: true,
              out: "output.js"
        }));
    return tsResult.js.pipe(gulp.dest("built/local"));
});

Jspm

Use jspm plugin for compiling TypeScript files.

Install

Install jspm by using the following command:

C:\. Command Prompt
$npm install -g jspm@beta
Note
Note: Currently TypeScript support in jspm is in 0.16beta

Webpack

Use ts-loader plugin for compiling TypeScript files.

Install

Install webpack by using the following command:

C:\. Command Prompt
$npm install ts-loader --save-dev

Now, you need to include the Webpack config file named webpack.config.js in your project.

Example
snippet
module.exports = {
    entry: "./src/index.tsx",
    output: {
        filename: "bundle.js"
    },
    resolve: {
        // Add '.ts' and '.tsx' as a resolvable extension.
        extensions: ["", ".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
    },
    module: {
        loaders: [
            // all files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'
            { test: /\.tsx?$/, loader: "ts-loader" }
        ]
    }
}
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +