Skip to content

Instantly share code, notes, and snippets.

@Lightnet
Created October 20, 2021 16:58
Show Gist options
  • Save Lightnet/8db067ca54850e71147f6f16fa534110 to your computer and use it in GitHub Desktop.
Save Lightnet/8db067ca54850e71147f6f16fa534110 to your computer and use it in GitHub Desktop.
gulp 4.0.2 auto reload server watch files
// base on this links
// https://gist.github.com/webdesserts/5632955
// need this for "gulp default" to run command line
// @babel/register
import { src, dest, watch, parallel, series } from "gulp";
import {spawn} from "child_process";
import config from'./config';
//import babel from "gulp-babel";
//import uglify from 'gulp-uglify';
//import buffer from 'vinyl-buffer';
//import size from 'gulp-size';
let node;
//BASIC ACCESS
const src_client_files=[
'./src/client.js'
];
//web public host
const output_dest='public';
// CLIENT BUILD
function client_build(callback){
// task
return src(src_client_files)
//.pipe(buffer())
//.pipe(uglify())
//.pipe(size())
.pipe(dest(output_dest));
//cb();
}
exports.client_build = client_build;
//WATCH FILES
function watchFiles(callback) {
//file change update
watch(src_client_files, client_build);
//watch all file changes to restart server
watch(['./src/**/*.js'], function() {
reload_server();
});
callback();
}
exports.watchFiles = watchFiles;
// https://gist.github.com/webdesserts/5632955
//nodejs functions
function reload_server(callback) {
if (node) node.kill();
let serverfile='./appserver.js';
node = spawn('node', [serverfile], {stdio: 'inherit'});
node.on('close', function (code) {
if (code === 8) {
gulp.log('Error detected, waiting for changes...');
}
});
//return callback();
}
exports.reload_server = reload_server;
//EXPORT DEFAULT
exports.default = series(
client_build
, watchFiles
, reload_server
);
// clean up if an error goes unhandled.
process.on('exit', function() {
if (node) node.kill()
});
{
"name": "gulpreload",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@babel/core": "^7.15.8",
"@babel/node": "^7.15.8",
"@babel/preset-env": "^7.15.8",
"@babel/register": "^7.15.3",
"fastify": "^3.22.1",
"fastify-static": "^4.4.2",
"gulp": "^4.0.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment