'use strict'; const Promise = require("bluebird"); const path = require("path"); const gulp = require("gulp"); const webpackStream = require("webpack-stream"); const webpack = require("webpack"); const gulpCached = require("gulp-cached"); const gulpRename = require("gulp-rename"); const gulpNodemon = require("gulp-nodemon"); const gulpLivereload = require("gulp-livereload"); const gulpNamedLog = require("gulp-named-log"); const presetSCSS = require("@joepie91/gulp-preset-scss"); const partialPatchLivereload = require("@joepie91/gulp-partial-patch-livereload-logger"); const serverWaiter = require("./lib/build/wait-for-server")(3000); const nodemonLogger = gulpNamedLog("nodemon"); const livereloadLogger = gulpNamedLog("livereload"); partialPatchLivereload(gulpLivereload); let nodemon; let sources = { scss: ["./scss/**/*.scss"] } /* The following resolves JacksonGariety/gulp-nodemon#33 */ process.once("SIGINT", function() { process.exit(0); }); function tryReload(target) { if (!serverWaiter.isWaiting()) { if (typeof target === "string") { gulpLivereload.changed(target); } else if (target.path != null) { if (Array.isArray(target.path)) { gulpLivereload.changed(target.path[0]); } else { gulpLivereload.changed(target.path); } } } } function queueFullReload() { return Promise.try(() => { nodemonLogger.log("Waiting for server to start listening...") return serverWaiter.wait(); }).then(() => { nodemonLogger.log("Server started!") tryReload("*"); }); } gulp.task("scss", () => { return gulp.src(sources.scss) .pipe(presetSCSS({ basePath: __dirname })) .pipe(gulp.dest("./public/css")); }); gulp.task("webpack", () => { return gulp.src("./frontend/index.js") .pipe(webpackStream({ watch: true, module: { preLoaders: [{ test: /\.tag$/, loader: "riotjs-loader", exclude: /node_modules/, query: { type: "babel", template: "pug", parserOptions: { js: { presets: ["es2015-riot"] } } } }], loaders: [ { test: /\.js$/, loader: "babel-loader" }, { test: /\.json$/, loader: "json-loader" } ] }, plugins: [ new webpack.ProvidePlugin({riot: "riot"}) ], resolveLoader: { root: path.join(__dirname, "node_modules") }, resolve: { extensions: [ "", ".tag", ".web.js", ".js", ".web.json", ".json" ] }, debug: false })) .pipe(gulpRename("bundle.js")) .pipe(gulp.dest("./public")); }); gulp.task("nodemon", ["scss"], () => { nodemon = gulpNodemon({ script: "./app.js", delay: 500, ignore: ["node_modules", "public", "gulpfile.js"], quiet: true }).on("restart", (cause) => { nodemonLogger.log("Restarting... caused by:", cause.join(" / ")); serverWaiter.reportRestart(); }).on("start", () => { return queueFullReload() }); }); gulp.task("watch-server", ["nodemon"], () => { gulpLivereload.listen({ quiet: true }); gulp.watch(sources.scss, ["scss"]); gulp.watch("./public/**/*", tryReload); }); gulp.task("watch", ["watch-server", "webpack"]); gulp.task("default", ["watch"]);