gulp

news/2024/7/3 3:51:38

详情请参见github:

  https://github.com/Sunnshino/gulpjs-plugs

  1 /*
  2     目录结构:
  3         test(主目录)
  4             src(输入路径)
  5                 index.html(主页面)
  6                 js(文件夹)
  7                 less(文件夹)
  8                 images(文件夹)
  9                 html(文件夹)
 10             dest(输出路径)
 11                 js(文件夹)
 12                 less(文件夹)
 13                 img(文件夹)
 14                 html(文件夹)
 15                 
 16         说明:
 17         paths里面有:
 18             script:
 19                 src:来自路径
 20                 dest:目的路径
 21                 watch:监听路径
 22 */
 23 let gulp = require('gulp'),
 24     del = require('del'),
 25     browserSync = require("browser-sync").create();
 26 let $ = require('gulp-load-plugins')();
 27 
 28 
 29 // 路径选项
 30 var paths = {
 31     script: {
 32         src: 'src/js/*.js',
 33         dest: 'dest/js/',
 34         watch: 'src/js/*.js'
 35     },
 36     less: {
 37         src: 'src/less/*.less',
 38         dest: 'dest/less/',
 39         watch: 'src/less'
 40     },
 41     images: {
 42         src: 'src/images/*.*',
 43         dest: 'dest/img/',
 44         watch: 'src/images/*.*'
 45     },
 46     html: {
 47         src: 'src/html/*.html',
 48         dest: 'dest/html',
 49         watch: 'src/*.html'
 50     }
 51 };
 52 
 53 gulp.task('delete', function(cb) {
 54     return del(['dest/*', '!dest/images'], cb);
 55 });
 56 
 57 
 58 // 文件热更新
 59 // gulp.task('reload', function() {
 60 //     gulp.src('/')
 61 //         .pipe($.connect.reload())
 62 //     console.log('html change!')
 63 // });
 64 
 65 //压缩html  
 66 gulp.task('html', function() {
 67     var options = {
 68         removeComments: true,
 69         collapseWhitespace: true,
 70         removeScriptTypeAttributes: true,
 71         removeStyleLinkTypeAttributes: true,
 72         minifyJS: true,
 73         minifyCSS: true
 74     };
 75     // 来自
 76     gulp.src('src/index.html')
 77         .pipe($.changed('dest', { hasChanged: $.changed.compareSha1Digest }))
 78         .pipe($.htmlmin(options))
 79         .pipe(gulp.dest('dest'))
 80         .pipe(browserSync.reload({ stream: true }));
 81 });
 82 
 83 
 84 //压缩js以及重命名 
 85 gulp.task("script", function() {
 86     // 来自路径
 87     gulp.src(['src/js/jquery.js', 'src/js/index.js'])
 88         .pipe($.changed(paths.script.dest, { hasChanged: $.changed.compareSha1Digest }))
 89         .pipe($.jshint())
 90         .pipe($.babel())
 91         .pipe($.jshint.reporter())
 92         .pipe($.concat('index.js'))
 93         .pipe($.uglify())
 94         .pipe($.rename('index.min.js'))
 95         .pipe($.obfuscate())
 96         .pipe(gulp.dest(paths.script.dest))
 97         .pipe(browserSync.reload({ stream: true }));
 98 });
 99 
100 //实时编译less  
101 gulp.task('less', function() {
102     gulp.src([paths.less.src])
103         .pipe($.changed(paths.less.src, { hasChanged: $.changed.compareSha1Digest }))
104         .pipe($.less())
105         .pipe($.tobase64({
106             maxsize: 8
107         }))
108         .pipe($.concat('main.css'))
109         .pipe($.cleanCss())
110         .pipe(gulp.dest(paths.less.dest))
111         .pipe(browserSync.reload({ stream: true }));
112 });
113 
114 
115 // 压缩图片  
116 gulp.task('images', function() {
117     gulp.src(paths.images.src)
118         .pipe($.changed(paths.images.dest, { hasChanged: $.changed.compareSha1Digest }))
119         .pipe($.imagemin({
120             progressive: true,
121             svgoPlugins: [{ removeViewBox: false }],
122             // use: [$.imageminPngquant()]
123         }))
124         .pipe(gulp.dest(paths.images.dest))
125         .pipe(browserSync.reload({ stream: true }));
126 });
127 
128 //启动服务器热加载  
129 gulp.task('server', ['delete'], function() {
130     gulp.start('html', 'less', 'script', 'images');
131     browserSync.init({
132         port: 8088,
133         server: {
134             baseDir: ['dest']
135         },
136         livereload: true
137     });
138 
139     //监控文件变化,自动更新(自添加规则) 
140     gulp.watch(paths.script.watch, ['script']);
141     gulp.watch(paths.less.watch, ['less']);
142     gulp.watch(paths.html.watch, ['html']);
143     gulp.watch(paths.images.watch, ['images']);
144 });
145 
146 gulp.task('default', ['server']);

http://lihuaxi.xjx100.cn/news/240542.html

相关文章

图的连通性和连通分量_英语,人口,连通性和露营地

图的连通性和连通分量by Evaristo Caraballo通过Evaristo Caraballo 英语,人口,连通性和露营地 (English, Population, Connectivity and Campsites) 在世界范围内推动使用Free Code Camp的因素 (Factors driving the use of Free Code Camp worldwide)…

aligned_storage简单学习

#include <iostream> #include <type_traits> #include <string>/* template< std::size_t Len, std::size_t Align default-alignment >struct::type aligned_storage; 相当于一个内建的POD类型他的大小是Size他的对齐方式是Align */ template<c…

gitlab安装各种坑

架构&#xff1a;源码安装, 数据库用mysql,网站用nginx 坑一.nginx报错 122016/07/19 09:26:11 [crit] 3881#0: *10 connect() to unix:/home/git/gitlab/tmp/sockets/gitlab-workhorse.socket failed (13: Permission denied) while connecting to upstream, client: 192.168.…

1.6 INSERT语句

1.6 INSERT语句正在更新内容&#xff0c;请稍后

把握机缘_机缘巧合,蒙太奇训练以及我的朋友如何使自己失业

把握机缘by Wiley Jones通过威利琼斯 机缘巧合&#xff0c;蒙太奇训练以及我的朋友如何使自己失业 (Serendipity, training montages, and how my friend automated himself out of a job) “No one person’s Hollywood success story has anything in common with anybody e…

form表单的action提交写到js中来,同时onclick事件也写在js中来。其action也可以通过ajax来提交的。...

假如你现在还在为自己的技术担忧&#xff0c;假如你现在想提升自己的工资&#xff0c;假如你想在职场上获得更多的话语权&#xff0c;假如你想顺利的度过35岁这个魔咒&#xff0c;假如你想体验BAT的工作环境&#xff0c;那么现在请我们一起开启提升技术之旅吧&#xff0c;详情请…

51单片机编码自学_这是9个月的自学式编码看起来像什么

51单片机编码自学by Stephen Mayeux斯蒂芬马约(Stephen Mayeux) 这是9个月的自学式编码看起来像什么 (Here’s What 9 Months of Self-Taught Coding Looks Like) 只是划伤表面 (Just Scratching the Surface) Today marks 9 months since I embarked on my journey as a self…

IE8下不识别indexOf的问题

1、为Array原型添加indexOf方法(如果学过面向对象,相当于给Array类添加实例方法),方法体如下: //添加数组IndexOf方法 if (!Array.prototype.indexOf){Array.prototype.indexOf function(elt /*, from*/){var len this.length >>> 0;var from Number(arguments[1])…