Last active
April 12, 2018 03:22
-
-
Save byverdu/1db3eb56b4d6530900ed92c73fb30f73 to your computer and use it in GitHub Desktop.
es6 + gulp + expressjs + nodemon + browser-sync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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'tcompilers/bundlers/transpilers
my code, I came with this solution by checkinggulp-nodemon
andbrowser-sync
repos.package.json and server.js are just for demo purposes... 🚀
If someone sees any error or craziness just let me know!