ducky_L

导航

gulp 使用笔记

1、安装nodejs -> 全局安装gulp -> 项目安装gulp以及gulp插件 -> 配置gulpfile.js -> 运行任务

2、常用gulp插件:

  ① gulp-sass(gulp-less):用于将sass(less)转换为css

  ②gulp-usemin:用于压缩页面中的js、css块文件

  ③gulp-cssmin、gulp-uglify:压缩css、js文件

  ④gulp-htmlmin:压缩html文件

  ⑤gulp-concat:拼接文件

  ⑥gulp-rename:重命名文件

  ⑦gulp-jshint:js语法检查

  ⑧gulp-connect: 启服务

  ⑨gulp-livereload:自动刷新页面

  ⑩gulp-notify:信息提示

  gulp-autoprefixer:根据设置浏览器版本自动处理浏览器前缀

  gulp-file-include: 文件引入 (https://www.npmjs.com/package/gulp-file-include/)

  gulp-webserver

browsersync工具: http://www.browsersync.cn/docs/gulp/

  ...(待补充)

3、使用例子:

①  压缩html、sass并重命名

 1  'use strict';
 2     var gulp = require("gulp"),
 3         cssmin = require('gulp-cssmin'),
 4         concat = require('gulp-concat'),
 5         sass = require('gulp-sass'),
 6         autoprefixer = require('gulp-autoprefixer'),
 7         rename = require('gulp-rename'),
 8         uglify = require('gulp-uglify'),
 9         jshint = require('gulp-jshint'),
10         usemin = require('gulp-usemin'),
11         htmlmin = require('gulp-htmlmin');
12 
13     var sassFiles = 'public/sass/*.scss';
14 
15 
16     gulp.task('usemin', function() {     
17 
18         return gulp.src(['views/*/*.html','views/*.html']) 
19             .pipe(htmlmin({
20 
21       removeComments: true,  //清除HTML注释
22             collapseWhitespace: true,  //压缩HTML
23             collapseBooleanAttributes: true,  //省略布尔属性的值 <input checked="true"/> ==> <input />
24             removeEmptyAttributes: true,  //删除所有空格作属性值 <input id="" /> ==> <input />
25             removeScriptTypeAttributes: true,  //删除<script>的type="text/javascript"
26             removeStyleLinkTypeAttributes: true,/  /删除<style>和<link>的type="text/css"
27             minifyJS: true,  //压缩页面JS
28             minifyCSS: true  //压缩页面CSS
29 
30       })) 
31             .pipe(usemin({
32                 outputRelativePath: '../',
33                 enableHtmlComment: true,
34                 js: [uglify, 'concat'],
35                 css: [cssmin, 'concat']
36             }))
37             .pipe(gulp.dest('dist/views'))     //页面输出地址
38     });
39 
40   //处理sass文件
41     gulp.task('pageStyle', function() {  
42         return gulp.src(sassFiles)
43             .pipe(sass.sync().on('error', sass.logError))
44             .pipe(cssmin())
45             .pipe(gulp.dest('public/css/'));
46     });
47 
48   //监控sass文件
49     gulp.task('watch', function() {
50         gulp.watch(sassFiles, ['pageStyle']);
51     });
52 
53  
54 
55 //(页面中使用在引用js、css文件时将引入的文件放入这个块中
56 
57 //  <!-- build:js(public/) /javascript/test.js -->
58 
59 //           <script>tes1t.js</script>
60 
61 //           <script>test2.js</script>
62  //      <!-- endbuild -->
63 
64 //
View Code

 

②  压缩sass并重命名

 1 var gulp = require('gulp'),
 2     sass = require('gulp-sass'),
 3     autoprefixer = require('gulp-autoprefixer'),
 4     minifycss = require('gulp-minify-css'),
 5     uglify = require('gulp-uglify'),
 6     rename = require('gulp-rename'),
 7     concat = require('gulp-concat'),
 8     notify = require('gulp-notify'),
 9     cache = require('gulp-cache'),
10     livereload = require('gulp-livereload');
11 
12 var sassFiles = 'public/css/all.scss';
13 var jsFiles = 'public/js/*.js';
14 // Styles
15 gulp.task('styles', function () {
16     return gulp.src(sassFiles)
17         .pipe(sass())
18         .pipe(autoprefixer({
19             browsers: ['last 5 versions'],
20             cascade: false
21         }))
22         .pipe(gulp.dest('dist/css'))
23         .pipe(rename({
24             suffix: '.min'
25         }))
26         .pipe(minifycss())
27         .pipe(gulp.dest('dist/css'))
28         .pipe(notify({
29             message: 'Styles task complete'
30         }));
31 });
32 // Scripts
33 gulp.task('scripts', function () {
34     gulp.src('public/js/*.js')
35         .pipe(concat('all.js'))
36         .pipe(gulp.dest('dist/js'))
37         .pipe(rename({
38             suffix: '.min'
39         }))
40         .pipe(uglify())
41         .pipe(gulp.dest('dist/js'))
42         .pipe(notify({
43             message: 'Scripts task complete'
44         }));
45 });
46 // Default task
47 gulp.task('default', function () {
48     gulp.start('styles', 'scripts');
49 });
50 // Watch
51 gulp.task('watch', function () {
52     gulp.watch(sassFiles, ['styles']);
53     gulp.watch(jsFiles, ['scripts']);
54 });
View Code

 

③  合并压缩js并重命名

 1 var gulp = require('gulp');
 2 var cleanCSS = require('gulp-clean-css');
 3 var uglify = require('gulp-uglify');
 4 var rename = require('gulp-rename');
 5 var concat = require('gulp-concat');
 6 
 7 
 8 gulp.task('scripts', function () {
 9     gulp.src(['public/js/sm.js', 'public/js/sm-extend.js', 'public/js/template.js'])
10         .pipe(concat('all.js'))
11         .pipe(gulp.dest('public/build/js'))
12         .pipe(rename({
13             suffix: '.min'
14         }))
15         .pipe(uglify())
16         .pipe(gulp.dest('public/build/js'));
17       gulp.src(['public/js/zepto.js', 'public/js/touch.js', 'public/js/config.js', 'public/js/fx.js'])
18         .pipe(concat('main.js'))
19         .pipe(gulp.dest('public/build/js'))
20         .pipe(rename({
21             suffix: '.min'
22         }))
23         .pipe(uglify())
24         .pipe(gulp.dest('public/build/js'))
25 });
26 
27 gulp.task('css', function () {
28     return gulp.src(['public/css/weui.css', 'public/css/animate.css', 'public/css/bootstrap.css', 'public/css/sm.css', 'public/css/sm-extend.css', 'public/css/iconfont.css', 'public/css/custom.css'])
29         .pipe(concat('all.css'))
30         .pipe(gulp.dest('public/build/css'))
31         .pipe(rename({
32             suffix: '.min'
33         }))
34         .pipe(cleanCSS())
35         .pipe(gulp.dest('public/build/css'));
36 });
37 
38 gulp.task('default', ['scripts', 'css']);
39 
40 gulp.task('watch', function () {
41     gulp.watch('public/js/*.js', ['scripts']);
42     gulp.watch('public/css/custom.css', ['css']);
43 });
View Code

 

④  启用服务,实现自动刷新(实现scss编译)

注意:1、浏览器需要安装拓展LiveReload(自行百度安装)。

      2、在gulp监听任务中执行livereload();

 1 var gulp = require('gulp'),
 2     sass = require('gulp-sass'),
 3     autoprefixer = require('gulp-autoprefixer'),
 4     minifycss = require('gulp-minify-css'),
 5     rename = require('gulp-rename'),
 6     concat = require('gulp-concat'),
 7     notify = require('gulp-notify'),
 8     livereload = require('gulp-livereload'),
 9     connect = require('gulp-connect');
10 
11 var sassFiles = 'public/css/all.scss';
12 var jsFiles = 'public/js/*.js';
13 // Styles
14 gulp.task('styles', function () {
15     return gulp.src(sassFiles)
16         .pipe(sass())
17         .pipe(autoprefixer({
18             browsers: ['last 5 versions'],
19             cascade: false
20         }))
21         .pipe(gulp.dest('dist/css'))
22         .pipe(rename({
23             suffix: '.min'
24         }))
25         .pipe(minifycss())
26         .pipe(gulp.dest('dist/css'))
27         .pipe(notify({
28             message: 'Styles task complete'
29         })).pipe(livereload());
30 });
31 // Default task
32 gulp.task('default', ['connect', 'watch']);
33 // Watch
34 gulp.task('watch', function () {
35     livereload.listen();
36     gulp.watch(sassFiles, ['styles']);
37     gulp.watch(jsFiles, ['scripts']);
38 });
39 gulp.task('connect', function() {
40     connect.server({
41       root: '',
42       port: 8000,
43       livereload: true
44     });
45   });
View Code

 

⑤ include 文件:

 1 var gulp = require('gulp'),
 2 fileinclude = require('gulp-file-include');
 3 
 4 gulp.task('fileinclude', function() {
 5     gulp.src(['index.html'])
 6       .pipe(fileinclude({
 7         prefix: '@@',
 8         basepath: './'
 9       }))
10       .pipe(gulp.dest('./'));
11   });
12 
13 <html>
14     @@include('test.html')
15 </html>
View Code

 

 

 

 

posted on 2023-04-26 09:37  ducky_L  阅读(18)  评论(0编辑  收藏  举报