Last active
July 5, 2017 10:40
-
-
Save romaricpascal/8559206 to your computer and use it in GitHub Desktop.
This file contains 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
function startExpress() { | |
var express = require('express'); | |
var app = express(); | |
app.use(require('connect-livereload')()); | |
app.use(express.static(__dirname)); | |
app.listen(4000); | |
} |
This file contains 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
// Don't forget to install `gulp-liverload` beforehand: | |
// `npm install gulp-livereload --save-dev` | |
function notifyLivereload(event) { | |
gulp.src(event.path, {read: false}) | |
.pipe(require('gulp-livereload')(lr)); | |
} |
This file contains 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
var gulp = require('gulp'); | |
// Let's make things more readable by | |
// encapsulating each part's setup | |
// in its own method | |
function startExpress() { | |
var express = require('express'); | |
var app = express(); | |
app.use(express.static(__dirname)); | |
app.listen(4000); | |
} | |
gulp.task('default', function () { | |
startExpress(); | |
}); |
This file contains 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
var gulp = require('gulp'); | |
var EXPRESS_PORT = 4000; | |
var EXPRESS_ROOT = __dirname; | |
var LIVERELOAD_PORT = 35729; | |
// Let's make things more readable by | |
// encapsulating each part's setup | |
// in its own method | |
function startExpress() { | |
var express = require('express'); | |
var app = express(); | |
app.use(require('connect-livereload')()); | |
app.use(express.static(EXPRESS_ROOT)); | |
app.listen(EXPRESS_PORT); | |
} | |
// We'll need a reference to the tinylr | |
// object to send notifications of file changes | |
// further down | |
var lr; | |
function startLivereload() { | |
lr = require('tiny-lr')(); | |
lr.listen(LIVERELOAD_PORT); | |
} | |
// Notifies livereload of changes detected | |
// by `gulp.watch()` | |
function notifyLivereload(event) { | |
// `gulp.watch()` events provide an absolute path | |
// so we need to make it relative to the server root | |
var fileName = require('path').relative(EXPRESS_ROOT, event.path); | |
lr.changed({ | |
body: { | |
files: [fileName] | |
} | |
}); | |
} | |
// Default task that will be run | |
// when no parameter is provided | |
// to gulp | |
gulp.task('default', function () { | |
startExpress(); | |
startLivereload(); | |
gulp.watch('*.html', notifyLivereload); | |
}); |
This file contains 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
var gulp = require('gulp'); | |
// `gulp.task()` defines task that can be run calling `gulp xyz` from the command line | |
// The `default` task gets called when no task name is provided to Gulp | |
gulp.task('default', function () { | |
console.log('Gulp and running!') | |
}); |
This file contains 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
// https://github.com/sindresorhus/jshint-stylish example | |
gulp.task('default', function () { | |
gulp.src(['file.js']) | |
.pipe(jshint('.jshintrc')) | |
.pipe(jshint.reporter('jshint-stylish')); | |
}); |
This file contains 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
// ... | |
// We'll need a reference to the tinylr | |
// object to send notifications of file changes | |
var lr; | |
function startLivereload() { | |
lr = require('tiny-lr')(); | |
lr.listen(35729); | |
} | |
// Default task that will be run | |
// when no parameter is provided | |
// to gulp | |
gulp.task('default', function () { | |
startExpress(); | |
startLivereload(); | |
}); |
Pretty useful, thanks!
I was having some issues with live reload, it kept returning undefined TypeError: undefined is not a function.
Changing line 9 to lr().listen(35729)
seemed to solve that issue.
For more reference: [https://github.com/mklabs/tiny-lr]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Typo here. It says gulp-liverload in
gulp-livereload.snippet.js
.