Skip to content

Instantly share code, notes, and snippets.

@ivasilov
Created August 14, 2015 13:51
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ivasilov/723100aa667a264f21d8 to your computer and use it in GitHub Desktop.
Save ivasilov/723100aa667a264f21d8 to your computer and use it in GitHub Desktop.
Gulpfile with webpack, typescript and nodemon
var gulp = require("gulp");
var gutil = require("gulp-util");
var rename = require('gulp-rename');
var webpack = require("webpack");
var nodemon = require('nodemon');
var typescript = require('gulp-typescript');
// These tasks setup nodemon.
gulp.task("start", function(cb) {
var options = {
watch: ["build/"],
script: "build/server.js",
ignore: ["build/public/**/*", "build/client.js"]
}
nodemon(options);
nodemon.on("start", cb);
});
// main task for development. Depending on what has changed,
// it does the appropriate tasks.
gulp.task("watch", ["start"], function() {
gulp.watch(["src/client.tsx", "src/routes.tsx", "src/components/**/*"], ["build:frontend"]);
gulp.watch(["./src/config/*", "./src/public/**/*", "./src/templates/**/*"], ["copy"]);
var backendFolders = [
"./src/api/**/*",
"./src/components/**/*",
"./src/lib/**/*",
"./src/models/**/*",
"./src/routes.tsx",
"./src/server.ts"
]
gulp.watch(backendFolders, ["build:backend"]);
});
// build the whole server
gulp.task("build", ["build:frontend", "copy", "build:backend"]);
var tsProject = typescript.createProject("tsconfig.json", { typescript: require('typescript') });
gulp.task("build:backend", function() {
var tsResult = tsProject.src().pipe(typescript(tsProject));
return tsResult.js
.pipe(rename(function (path) { path.dirname = path.dirname.replace("src", ""); }))
.pipe(gulp.dest("build/"));
});
gulp.task("build:frontend", function(cb) {
webpackConfig = {
// Source maps support (or 'inline-source-map' also works)
devtool: 'source-map',
context: __dirname + "/src",
entry: "./client.tsx",
output: {
filename: "app.js",
path: __dirname + "/build/public/javascripts"
},
// Add `.ts` and `.tsx` as a resolvable extension.
resolve: {
extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js']
},
module: {
loaders: [
{ test: /\.ts(x?)$/, loader: 'ts-loader'}
]
}
};
webpack(webpackConfig, function(err, stats) {
if(err) throw new gutil.PluginError("webpack:build", err);
cb();
});
});
gulp.task("copy", ["copy:config", "copy:public", "copy:templates"]);
gulp.task("copy:config", function() { return gulp.src('./src/config/*').pipe(gulp.dest('./build/config')); });
gulp.task("copy:public", function() { return gulp.src('./src/public/**/*').pipe(gulp.dest('./build/public')); });
gulp.task("copy:templates", function() { return gulp.src('./src/templates/**/*').pipe(gulp.dest('./build/templates')); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment