Skip to content

legoheld/lernetz-typescript-gulp-task

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 

Repository files navigation

lernetz-typescript-gulp-task

This node module wraps several gulp plugins executions into one reusable gulp task. The main goal of the task is to compile a set of typescript files into a single js file. It automatically creates a minified version and injects the sourcemaps. It internally uses rollup with treeshaking for a smallest as possible filesize. Rollup is setup to bundle also npm packages. This allows to use npm packages in our source typescript. You need to install the required types for typescript see: https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/ And also install the library itself with npm so rollup can find and bundle it.

In the 0.4.x version with gulp 4.0 this module will automatically register two gulp tasks: ln:bundle, ln:minify. You can use them directly in your gulp file or from outside. See npx gulp --tasks.

Usage

The following example will compile the file Main.ts and bundle all its dependencies into to the output folder public. It creates the following js files:

  • main.js - the uncompressed js source
  • main.js.map - the sourcemap file
  • main.min.js - the minified js file The resulting js file exposes the exported functions/variables of Main.ts under the globalName: main.
var gulp = require('gulp');

// this will automatically register to gulp tasks: ln:bundle, ln:minify and returns them as gulp.series( 'ln:bundle', 'ln:minify' )
var bundle = require( 'lernetz-typescript-gulp-task' )( { dest:'public', src:'Main.ts', name:'main' } );

// either define your own task
gulp.task( 'bundle', bundle );

// or use it directly in your watch statement
gulp.task('default', function() {
    gulp.watch( 'typescript/**/*.ts', bundle );
    // or 
    gulp.watch( 'typescript/**/*.ts', gulp.series( 'ln:bundle', 'ln:minify' ) );
});

// or directly call:
// npx gulp ln:bundle
// npx gulp ln:minify

Options

The task accepts an parameter object with the following attributes:

{
    name: 'main', // the name of the js file to create and also the globalname 
	dest: 'public', // the destination used in gulp.dest( .. )
    src: './src/main.ts', // the source the the main typescript file
    rollup: {
        // the default input options used for rollup. see: https://rollupjs.org/guide/en#javascript-api
        inputOptions: {
            input: options.src, // autogenerated if not defined
            plugins: [
                typescript( { check:false } ),
                resolve( { jsnext: true, main: true, browser:true } ),
                commonjs(),
            ]
        },
        // the default output options used for rollup. see: https://rollupjs.org/guide/en#javascript-api
        outputOptions: { 
            file: options.dest + '/' + options.name + '.js', // autogenerated if not defined
            name: options.name,  // autogenerated if not defined
            format: 'iife',
            sourcemap: true
        }
    },
    uglify: {} // the default options for the ugify task: https://www.npmjs.com/package/gulp-uglify-es
}

About

This node module that wraps several gulp plugins into a reusable glup task with options

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published