Last active
April 3, 2020 05:31
-
-
Save keeyanajones/353b0f442064c44b264a407e098bd76a to your computer and use it in GitHub Desktop.
Gulp and TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Default | |
var gulp = require('gulp'); | |
var ts = require('gulp-typescript'); | |
var tsProject = ts.createProject('tsconfig.json'); | |
gulp.task('default', function () { | |
return tsProject.src() | |
.pipe(tsProject()) | |
.js.pipe(gulp.dest('dist')); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Babel | |
var gulp = require('gulp'); | |
var browserify = require('browserify'); | |
var source = require('vinyl-source-stream'); | |
var tsify = require('tsify'); | |
var sourcemaps = require('gulp-sourcemaps'); | |
var buffer = require('vinyl-buffer'); | |
var paths = { | |
pages: ['src/*.html'] | |
}; | |
gulp.task('copy-html', function () { | |
return gulp.src(paths.pages) | |
.pipe(gulp.dest('dist')); | |
}); | |
gulp.task('default', gulp.series(gulp.parallel('copy-html'), function () { | |
return browserify({ | |
basedir: '.', | |
debug: true, | |
entries: ['src/main.ts'], | |
cache: {}, | |
packageCache: {} | |
}) | |
.plugin(tsify) | |
.transform('babelify', { | |
presets: ['es2015'], | |
extensions: ['.ts'] | |
}) | |
.bundle() | |
.pipe(source('bundle.js')) | |
.pipe(buffer()) | |
.pipe(sourcemaps.init({loadMaps: true})) | |
.pipe(sourcemaps.write('./')) | |
.pipe(gulp.dest('dist')); | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Uglify | |
var gulp = require('gulp'); | |
var browserify = require('browserify'); | |
var source = require('vinyl-source-stream'); | |
var tsify = require('tsify'); | |
var uglify = require('gulp-uglify'); | |
var sourcemaps = require('gulp-sourcemaps'); | |
var buffer = require('vinyl-buffer'); | |
var paths = { | |
pages: ['src/*.html'] | |
}; | |
gulp.task('copy-html', function () { | |
return gulp.src(paths.pages) | |
.pipe(gulp.dest('dist')); | |
}); | |
gulp.task('default', gulp.series(gulp.parallel('copy-html'), function () { | |
return browserify({ | |
basedir: '.', | |
debug: true, | |
entries: ['src/main.ts'], | |
cache: {}, | |
packageCache: {} | |
}) | |
.plugin(tsify) | |
.bundle() | |
.pipe(source('bundle.js')) | |
.pipe(buffer()) | |
.pipe(sourcemaps.init({loadMaps: true})) | |
.pipe(uglify()) | |
.pipe(sourcemaps.write('./')) | |
.pipe(gulp.dest('dist')); | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Watchify | |
var gulp = require('gulp'); | |
var browserify = require('browserify'); | |
var source = require('vinyl-source-stream'); | |
var watchify = require('watchify'); | |
var tsify = require('tsify'); | |
var fancy_log = require('fancy-log'); | |
var paths = { | |
pages: ['src/*.html'] | |
}; | |
var watchedBrowserify = watchify(browserify({ | |
basedir: '.', | |
debug: true, | |
entries: ['src/main.ts'], | |
cache: {}, | |
packageCache: {} | |
}).plugin(tsify)); | |
gulp.task('copy-html', function () { | |
return gulp.src(paths.pages) | |
.pipe(gulp.dest('dist')); | |
}); | |
function bundle() { | |
return watchedBrowserify | |
.bundle() | |
.on('error', fancy_log) | |
.pipe(source('bundle.js')) | |
.pipe(gulp.dest('dist')); | |
} | |
gulp.task('default', gulp.series(gulp.parallel('copy-html'), bundle)); | |
watchedBrowserify.on('update', bundle); | |
watchedBrowserify.on('log', fancy_log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Browserify and tsify, we can add various features to our build with browserify plugins.