gulp + node + express 㧠BrowserSync 使ãã
以åã gulpï¼nodeï¼expressã§livereload使ããã ã£ã¦è¨äºãæ¸ããã®ã§ããããã® BrowserSync çã§ãã
gulp + node + express 㧠BrowserSync 使ããã
ãµã¼ããµã¤ãã node + express ã§å®è£ ãã¤ã¤ãã¯ã©ã¤ã¢ã³ããåãããã¸ã§ã¯ãã§æ±ããããªããã¸ã§ã¯ãåãã®ã©ã¤ããªãã¼ãã£ã³ã°è¨å®æ¹æ³ã§ãã
æå¤ã¨æ¥æ¬èªã§èª¬æãã¦ãããè¨äºãå°ãªãã£ãã®ã¨ãæ¤ç´¢ã®çµæâã®è¨äºè¦ã¦ããã¦ãæ¹ãå¤ãã£ãã®ã§ã
å½æã¯ãBrowserSync ããã©ããã£ã¦ express ã«ã¤ãªãã ããããã ããç¡çãªãã ãã¨æã£ã¦ãã®ã§ãããBrowserSync ã« Proxy ã®è¨å®ããã¦ãããã°å¯è½ã§ãã
æ¹æ³
ã¾ããexpress å´ã®åèµ·åãå¿ è¦ã«ãªããããªãªã½ã¼ã¹ã¯ãnodemon ã®æ©è½ãå©ç¨ãã¦ç£è¦ãã¾ãã
ããã¦ãBrowserSync ã 4000 çªãã¼ãã§èµ·åãã¦ã 3000 çªãã¼ãã§åä½ãã nodemon ã«æ¥ç¶ã
ãã©ã¦ã¶ãªãã¼ãã ãã§ãããããªãã¯ã©ã¤ã¢ã³ãå´ã®ãã¡ã¤ã«ã¯ BrowserSync ã¾ããã§ãªãã¼ãã
ãµã¼ãåèµ·åãå¿
è¦ãªãªã½ã¼ã¹ã¯ãnodemon ã§å¤æ´ãæ¤ç¥ãã¦ããµã¼ãåèµ·åãã¦ããããåèµ·åå®äºå¾ã« BrowserSync ã«éç¥ãã¦ãããã
- gulpfile.js
var gulp = require('gulp'); var nodemon = require('gulp-nodemon'); var browserSync = require('browser-sync'); function reload() { browserSync.reload({ stream: false }); }; gulp.task('browsersync', function() { browserSync.init({ files: ['public/**/*.*', 'views/**/*.*'], // BrowserSyncã«ã¾ããããã¡ã¤ã«ç¾¤ proxy: 'http://localhost:3000', // express ã®åä½ãããã¼ãã«ãããã· port: 4000, // BrowserSync 㯠4000 çªãã¼ãã§èµ·å open: false // ãã©ã¦ã¶ open ããªã }); }); gulp.task('serve', ['browsersync'], function () { nodemon({ script: './bin/www', ext: 'js html css', ignore: [ // nodemon ã§ç£è¦ããªããã£ã¬ã¯ã㪠'node_modules', 'bin', 'views', 'public', 'test' ], env: { 'NODE_ENV': 'development' }, stdout: false // Express ã®åèµ·åæã®ãã°ãç£è¦ãããã }).on('readable', function() { this.stdout.on('data', function(chunk) { if (/^Express\ server\ listening/.test(chunk)) { // Express ã®åèµ·åãå®äºããããreload() ã§BrowserSync ã«éç¥ã // â»Express ã§åºåããèµ·åæã®ã¡ãã»ã¼ã¸ã«åããã¦æ¯è¼æååã¯ä¿®æ£ reload(); } process.stdout.write(chunk); }); this.stderr.on('data', function(chunk) { process.stderr.write(chunk); }); }); }); gulp.task('default', ['serve']);
ãã©ã¦ã¶ã§ http://localhost:4000 ã«æ¥ç¶ããã¨ããµã¼ãå´ä¿®æ£ãã¦ãã¯ã©ã¤ã¢ã³ãå´ä¿®æ£ãã¦ãåæã«èªåã§ãã©ã¦ã¶ããªãã¼ãããã¦ãããããã³ã¼ãã£ã³ã°ãã§ãã¾ãã