Skip to content

Instantly share code, notes, and snippets.

@thomd
Last active September 12, 2016 19:03
Show Gist options
  • Select an option

  • Save thomd/2147fd7643bb51d66119c6e473d2ea12 to your computer and use it in GitHub Desktop.

Select an option

Save thomd/2147fd7643bb51d66119c6e473d2ea12 to your computer and use it in GitHub Desktop.
typescript gulp task
var gulp = require("gulp"),
browserify = require("browserify"),
source = require("vinyl-source-stream"),
buffer = require("vinyl-buffer"),
tslint = require("gulp-tslint"),
tsc = require("gulp-typescript"),
sourcemaps = require("gulp-sourcemaps"),
uglify = require("gulp-uglify"),
runSequence = require("run-sequence"),
mocha = require("gulp-mocha"),
istanbul = require("gulp-istanbul"),
browserSync = require('browser-sync').create();
gulp.task("default", function (cb) {
runSequence("lint", "build", "test", "bundle", cb);
});
// tslint expects to find a file named tslint.json in the root folder of the project.
// The tslint.json is to configure the TypeScript linter options.
gulp.task("lint", function() {
return gulp.src([
"source/**/**.ts",
"test/**/**.test.ts"
])
.pipe(tslint({ }))
.pipe(tslint.report("verbose"));
});
// compile our application’s source code
var tsProject = tsc.createProject("tsconfig.json");
gulp.task("build-app", function() {
return gulp.src([
"source/**/**.ts",
"typings/main.d.ts/",
"source/interfaces/interfaces.d.ts"
])
.pipe(tsc(tsProject))
.js.pipe(gulp.dest("source/"));
});
// use Browserify to transform CommonJS into one single file that can be loaded by a web browser
gulp.task("bundle", function() {
var libraryName = "myapp";
var mainTsFilePath = "source/main.js";
var outputFolder = "dist/";
var outputFileName = libraryName + ".min.js";
var bundler = browserify({
debug: true,
standalone : libraryName
});
return bundler.add(mainTsFilePath)
.bundle()
.pipe(source(outputFileName))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(outputFolder));
});
// observe changes in the code, trigger the compilation process and display changes in a web browser
gulp.task("watch", ["default"], function () {
browserSync.init({
server: "."
});
gulp.watch([ "source/**/**.ts", "test/**/*.ts"], ["default"]);
gulp.watch("dist/*.js").on('change', browserSync.reload);
});
// test
gulp.task("istanbul:hook", function() {
return gulp.src(['source/**/*.js'])
// Covering files
.pipe(istanbul())
// Force `require` to return covered files
.pipe(istanbul.hookRequire());
});
gulp.task("test", ["istanbul:hook"], function() {
return gulp.src('test/**/*.test.js')
.pipe(mocha({ui: 'bdd'}))
.pipe(istanbul.writeReports());
});
{
"compilerOptions": {
"target": "es5",
"sourceMap": true,
"module": "commonjs",
"moduleResolution": "node",
"isolatedModules": false,
"jsx": "react",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true
}
}
{
"rules": {
"class-name": true,
"comment-format": [true, "check-space"],
"curly": true,
"eofline": true,
"forin": true,
"indent": [true, "spaces"],
"label-position": true,
"label-undefined": true,
"max-line-length": [true, 140],
"member-access": true,
"member-ordering": [true,
"public-before-private",
"static-before-instance",
"variables-before-functions"
],
"no-arg": true,
"no-bitwise": true,
"no-console": [true,
"debug",
"info",
"time",
"timeEnd",
"trace"
],
"no-construct": true,
"no-debugger": true,
"no-duplicate-key": true,
"no-duplicate-variable": true,
"no-empty": true,
"no-eval": true,
"no-inferrable-types": true,
"no-shadowed-variable": true,
"no-string-literal": true,
"no-switch-case-fall-through": true,
"no-trailing-whitespace": true,
"no-unused-expression": true,
"no-unused-variable": true,
"no-unreachable": true,
"no-use-before-declare": true,
"no-var-keyword": true,
"object-literal-sort-keys": true,
"one-line": [true,
"check-open-brace",
"check-catch",
"check-else",
"check-finally",
"check-whitespace"
],
"quotemark": [true, "double", "avoid-escape"],
"radix": true,
"semicolon": true,
"trailing-comma": [true, {
"singleline": "never",
"multiline": "always"
}],
"triple-equals": [true, "allow-null-check"],
"typedef-whitespace": [true, {
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}],
"variable-name": false,
"whitespace": [true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment