本地安装bower 建议先看我的博客– angularjs && bower安装和使用 入门级安装 直接上手
项目配置文件
1、在文件本地初始化 npm init
2、全局安装gulp 执行命令:cnpm i -g gulp
3、本地安装gulp 执行命令:cnpm i –save-dev gulp
4、依次本地安装上图(配置图片)执行命令:cnpm i –save-dev gulp-clean …………
依据项目需求总计9个模块的安装
5、模块安装完以后,在本地目录新建gulpfile.js
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var open = require('open');
var app = {
srcPath: 'src/',
devPath: 'build/',
prdPath: 'dist/'
};
gulp.task('html', function() {
gulp.src(app.srcPath + '**/*.html')
.pipe(gulp.dest(app.devPath))
.pipe(gulp.dest(app.prdPath))
.pipe($.connect.reload());
})
gulp.task('less', function() {
gulp.src(app.srcPath + 'less/*.less')
.pipe($.plumber())
.pipe($.less())
.pipe(gulp.dest(app.devPath + 'css'))
.pipe($.cssmin())
.pipe(gulp.dest(app.prdPath + 'css'))
.pipe($.connect.reload());
});
gulp.task('js', function() {
gulp.src(app.srcPath + 'js/*.js')
.pipe($.plumber())
.pipe(gulp.dest(app.devPath + 'js'))
.pipe($.uglify())
.pipe(gulp.dest(app.prdPath + 'js'))
.pipe($.connect.reload());
});
gulp.task('image', function() {
gulp.src(app.srcPath + 'img/**/*')
.pipe($.plumber())
.pipe(gulp.dest(app.devPath + 'img'))
.pipe($.imagemin())
.pipe(gulp.dest(app.prdPath + 'img'))
.pipe($.connect.reload());
});
gulp.task('build', ['image', 'js', 'less', 'html']);
gulp.task('clean', function() {
gulp.src([app.devPath, app.prdPath])
.pipe($.clean());
});
gulp.task('serve', ['build'], function() {
$.connect.server({
root: [app.devPath],
livereload: true,
port: 3000
});
open('http://localhost:3000');
gulp.watch(app.srcPath + '**/*.html', ['html']);
gulp.watch(app.srcPath + 'less/*.less', ['less']);
gulp.watch(app.srcPath + 'js/*.js', ['js']);
gulp.watch(app.srcPath + 'img/**/*', ['image']);
});
gulp.task('default', ['serve']);
6、依据图片用gulp创建任务
7、项目介绍:
7.1、src 为项目源代码 –需要手动创建目录
7.2、build 为开发模式
7.3、dist 为生产模式
7.4、例如gulp.src(‘bower_components/*/.js’)为获取项目路径
7.5、例如 .pipe(gulp.dest(app.devPath + ‘vendor’)) 通过gulp流拷贝到开发模式下的vendor文件夹下
7.6、.pipe($.connect.reload()) 通过gulp流 监控页面的变化
8、项目自定义浏览器热加载–nb
gulp.task('build', ['image', 'js', 'less', 'lib', 'html', 'json']);
gulp.task('serve', ['build'], function() {
$.connect.server({
root: [app.devPath],
livereload: true,
port: 3000
});
open('http://localhost:3000');
gulp.watch('bower_components/**/*', ['lib']);
gulp.watch(app.srcPath + '**/*.html', ['html']);
gulp.watch(app.srcPath + 'data/**/*.json', ['json']);
gulp.watch(app.srcPath + 'style/**/*.less', ['less']);
gulp.watch(app.srcPath + 'script/**/*.js', ['js']);
gulp.watch(app.srcPath + 'image/**/*', ['image']);
});
gulp.task('default', ['serve']);
从打包到新建服务再到监听—-通过默认任务简化了命令行:gulp serve
直接执行 gulp 即可。