在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):hiddentao/gulp-server-livereload开源软件地址(OpenSource Url):https://github.com/hiddentao/gulp-server-livereload开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):gulp-server-livereload
Serve a folder over HTTP and watch it for changes, telling the browser to reload itself when a change happens.
Installation$ npm install --save-dev gulp-server-livereload UsageThe folder supplied to var gulp = require('gulp');
var server = require('gulp-server-livereload');
gulp.task('webserver', function() {
gulp.src('app')
.pipe(server({
livereload: true,
directoryListing: true,
open: true
}));
}); If you run Command-lineInstall the package globally: $ npm install -g gulp-server-livereload Then you can run the $ livereload help
Usage: livereload [options]
Options:
-h, --help output usage information
-V, --version output the version number
-n, --no-browser do not open the localhost server in a browser
-l, --log [type] log level (default: info)
-p, --port <n> the port to run on OptionsNote: not all of these options are currently available via the CLI executable
Livereload behaviourBy default when a file changes the livereload script in the browser does the following:
To override the default behaviour define the following method in Javascript: /**
* This method gets called by the livereload script when the server notifies it
* that something has changed.
*
* @param {Object} file File which changed.
*/
window._onLiveReloadFileChanged = function(file) {
// do whatever you want here, e.g. location.reload();
} The {
"path": ...full path to file which changed...
"name": ...file name (without path)...
"ext": ...file extension name...
} FAQWhy can't I reach the server from the network?Set How can I set main.html to automatically load when I visit the URL?Set the gulp.task('webserver', function() {
gulp.src('app')
.pipe(server({
defaultFile: 'main.html'
}));
}); How can I use livereload if my HTML is already being served up by a node.js/other app?You'll have to add some Javascript to dynamically load in the browser-side scripts.
For example, if the (function() {
var lrHost = location.protocol + '//' + location.hostname + ':34322';
var s = document.createElement('script');
s.async = true;
s.setAttribute('src', lrHost + '/livereload.js');
document.body.appendChild(s);
})(); To enable console logging capture add the following query paramter: s.setAttribute('src', lrHost + '/livereload.js?extra=capture-console'); How can I pass a custom filter to livereload?In the gulp.task('webserver', function() {
gulp.src('app')
.pipe(server({
livereload: {
enable: true,
filter: function(filePath, cb) {
cb( !(/node_modules/.test(filePath)) );
}
}
}));
}); How can I use non-hash URLs for my single page app (i.e. HTML5 mode) with this plugin?When you're building a single-page app with non-hash URLs (html5 mode) then you
want the server to always serve up the same file for every URL. This is where
the gulp.task('webserver', function() {
gulp.src('app')
.pipe(server({
fallback: 'index.html'
}));
}); You can control exactly how the fallback mode works using the gulp.task('webserver', function() {
gulp.src('app')
.pipe(server({
fallback: 'index.html',
fallbackLogic: function(req, res, fallbackFile) {
if (req.url.match(/\.png$/i)) {
res.statusCode = 404;
res.end();
} else {
// default fallback config
fs.createReadStream(fallbackFile).pipe(res);
}
},
}));
}); How can I use this with CSS preprocessors such as LESS or SASS?If you use a CSS preprocessor in gulp, you'll need to run its gulp task (typically with You'll also want to configure livereload to ignore changes to the source files, and instead let it only handle changes to the compiled CSS (which will be refreshed inline). var watch = require('gulp-watch');
gulp.task('watch', function () {
watch('./styles/*.less', batch(function (events, done) {
gulp.start('default', done);
}));
});
gulp.task('webserver', ['watch'], function () {
gulp.src('.')
.pipe(server({
livereload: {
enable: true,
filter: function (filename, cb) {
cb(!/\.(sa|le)ss$|node_modules/.test(filename);
}
},
directoryListing: true,
open: true
}));
}); Note: the livereload server automatically handles generated sourcemap files properly so don't worry about them. LicenseMIT - see LICENSE.md |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论