Skip to content

Instantly share code, notes, and snippets.

@byverdu
Last active April 12, 2018 03:22
Show Gist options
  • Save byverdu/1db3eb56b4d6530900ed92c73fb30f73 to your computer and use it in GitHub Desktop.
Save byverdu/1db3eb56b4d6530900ed92c73fb30f73 to your computer and use it in GitHub Desktop.
es6 + gulp + expressjs + nodemon + browser-sync
import gulp from 'gulp';
import browserSync from 'browser-sync';
import nodemon from 'gulp-nodemon';
const browserOpts = {
proxy: 'http://localhost:3000',
port: 9000,
open: false
};
gulp.task( 'browserSync', () => {
let restarted = false;
// As suggested in 'gulp-nodemon'
// start browser-sync in same task
browserSync.create();
browserSync.init( browserOpts );
return nodemon({
// Rather than using the script option
// use the exec option and call babel-node
exec: 'babel-node ./server.js'
})
.on( 'start', () => {
if ( restarted ) {
setTimeout(() => {
// reload browser after nodemon has
// finished to restart
browserSync.reload();
}, 4000 );
}
})
// Just prevents to call reload
// when starting for fisrst time
.on( 'restart', () => {
restarted = true;
});
});
{
"name": "es6-gulp-expressjs-nodemon-browser-sync",
"version": "1.0.0",
"main": "./server.js",
"author": "@byverdu <[email protected]>",
"devDependencies": {
"browser-sync": "^2.18.8",
"gulp": "^4.3.1",
"gulp-nodemon": "^2.2.1",
},
"dependencies": {
"express": "^4.15.2"
}
}
// express server
import express from 'express';
import pathUtil from 'path';
const server = express();
const router = express.Router();
server.use( express.static( pathUtil.join( __dirname, '/client' )));
server.get('/', (req, res) => res.sendfile('./client/index.html'));
server.listen(3000);
@byverdu
Copy link
Author

byverdu commented May 6, 2017

I was trying to find an implementation to make work all this packages together but what I've found was somehow old (2 years heheheh 😱 ).

Those gists were working but I was getting unexpected token import because I wasn't compilers/bundlers/transpilersmy code, I came with this solution by checking gulp-nodemon and browser-sync repos.

package.json and server.js are just for demo purposes... 🚀

If someone sees any error or craziness just let me know!

@jacsamg
Copy link

jacsamg commented Apr 12, 2018

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment