Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gulp使用3.使用livereload实现页面自动刷新(续二) #13

Open
damoclesX opened this issue Aug 13, 2015 · 0 comments
Open

gulp使用3.使用livereload实现页面自动刷新(续二) #13

damoclesX opened this issue Aug 13, 2015 · 0 comments

Comments

@damoclesX
Copy link
Owner

使用gulp-livereload自动刷新页面的时候,默认是刷新index.html。想刷新其他页面时,可以在命令行传入参数

比如在命令行中传入参数

gulp --page app.html

目前(2015-08-13 16:44:17)可以使用gulp.env.page获取到app.html。但是会提示

gulp.env has been deprecated. Use your own CLI parser instead. We recommend usin
g yargs or minimist.

官方推荐的方法是使用yargs或者minimist等来实现。

如:yargs

npm install yargs -g

安装成功后,在gulpfile.js里面:

var argv = require('yargs').argv;

console.log(argv.page)

便可读取到传入的app.html

这时在之前的gulpfile.js里面便可以这样

var gulp = require('gulp'),
    livereload = require('gulp-livereload'),
    argv = require('yargs').argv;

var path = {
    js:'./js/*.js',
    css:'./css/*.css',
    html:'*.html'
};
function obj2array(obj){
    var arr = [];
    for(var i in obj){
        arr.push(obj[i])
    }
    return arr;
}
gulp.task('watch',function(){
    gulp.watch(obj2array(path),['livereload']);
})

gulp.task('livereload',function(){
    livereload.listen();
    livereload.reload(argv.page || 'index.html');
})
gulp.task('default',['watch'])
@damoclesX damoclesX changed the title gulp使用1.使用livereload实现页面自动刷新(续二) gulp使用3.使用livereload实现页面自动刷新(续二) Aug 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant