gulp的配置使用
2017-06-09 本文已影响0人
守心向暖
自动构建工具,优点
- 易于使用
代码优于配置策略,复杂任务可管理
- 构建快速
快速构建,减少频繁IO
- 插件高质
严格的插件指南
- 易于学习
- 安装
// 全局安装
npm install -g gulp
// 本地安装作为项目依赖
npm install gulp --save-dev
- 执行
// 执行任务列表
gulp <task> <othertask>
// 执行注册名为default的task,不存在则会报错
gulp
- 参数标记
-v/--version: 显示gulp版本号
--require <module path>: 执行前require一个模块
--gulpfile <gulpfile path>: 手动指定gulpfile路径
--cwd <dir path>: 手动指定cwd,定义gulpfile查找的位置
-T/--tasks: 显示
- API
gulp.src(globs[, options])
gulp.dest(path[, options])
gulp.task(name[, deps], fn)
gulp.watch(glob[, opts], tasks)
gulp.watch(glob[, opts, cb])
- gulp的简单配置
// gulpfile.js
'use strict';
var gulp = require('gulp');
var less = require('gulp-less');
var minifycss = require('gulp-minify-css');
var rename = require("gulp-rename");
var browserSync = require('browser-sync');
var reload = browserSync.reload;
var gutil = require('gulp-util');
// load plugins
var $ = require('gulp-load-plugins')();
var hash = require('gulp-hash');
var inject = require('gulp-inject');
// handle *.less
gulp.task('styles', function() {
return gulp.src(['less/index.less'])
.pipe(less())
.pipe($.autoprefixer('last 1 version'))
.pipe(minifycss())
.pipe(gulp.dest('css'))
.pipe(reload({stream: true}))
.pipe($.size())
});
// handle *.js
gulp.task('scripts', function() {
return gulp.src('js/index.js')
.pipe($.jshint())
.pipe($.jshint.reporter(require('jshint-stylish')))
.pipe(gulp.dest('js'))
.pipe($.size());
});
// handle html, depend on styles and scripts
gulp.task('html', ['scripts', 'styles'], function() {
var jsFilter = $.filter('**/*.js', {restore: true}),
cssFilter = $.filter('**/*.css', {restore: true});
// 多层级目录时:gulp.src(['index.html', 'xx/**/*.html])
return gulp.src('index.html')
.pipe($.useref())
.pipe(jsFilter)
.pipe(hash({hashLength: 12}))
.pipe($.uglify())
.pipe(jsFilter.restore)
.pipe(cssFilter)
.pipe(hash({hashLength: 12}))
.pipe($.csso())
.pipe(cssFilter.restore)
.pipe($.useref())
.pipe(gulp.dest('dist'))
.pipe($.size());
});
// handle images
gulp.task('images', function() {
return gulp.src('images/*')
.pipe(gulp.dest('dist/images'))
.pipe(reload({stream: true, once: true}))
.pipe($.size());
});
// clean
gulp.task('clean', function() {
return gulp.src(['dist'], {read: false}).pipe($.clean());
});
// inject
gulp.task('inject', ['html'], function() {
var target = gulp.src('dist/*.html'),
sources = gulp.src([ 'dist/js/vendor-*.js', 'dist/js/index-*.js','dist/css/index-*.css'], {read: false});
return target.pipe(inject(sources, {relative: true}))
.pipe(gulp.dest('dist'));
});
// build
gulp.task('build', ['clean'], function() {
gulp.start(['inject', 'images']);
});
// dev
gulp.task('dev', ['watch']);
// default
gulp.task('default', ['clean'], function() {
gulp.start('build');
});
// browserSync
gulp.task('serve', ['styles'], function () {
browserSync.init({
port: 3200,
server: {
baseDir: ''
}
});
});
// watch
gulp.task('watch', ['serve'], function () {
// watch for changes
// 多层级目录时:gulp.src(['index.html', 'xx/**/*.html])
gulp.watch(['index.html'], reload);
gulp.watch('less/**/*.less', ['styles']);
gulp.watch('js/**/*.js', ['scripts']);
gulp.watch('img/**/*', ['images']);
});
// index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- inject:css -->
<!-- build:css css/index.css -->
<link rel="stylesheet" href="css/index.css">
<!-- endbuild -->
<!-- endinject -->
</head>
<body>
</body>
<!-- inject:js -->
<!-- build:js js/vendor.js -->
<script src="vendor/lib.js"></script>
<!-- endbuild -->
<!-- build:js js/index.js -->
<script src="js/index.js"></script>
<!-- endbuild -->
<!-- endinject -->
</html>
// 多层级目录时:资源使用相对路径,链接使用绝对路径