javascript 各种hexo gulpfile.js收集
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 各种hexo gulpfile.js收集相关的知识,希望对你有一定的参考价值。
// from https://gyunzhi.github.io/2018/02/15/%E4%BD%BF%E7%94%A8gulp%E5%AF%B9%E5%8D%9A%E6%96%87%E5%8E%8B%E7%BC%A9/
var gulp = require('gulp');
var minifycss = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var htmlmin = require('gulp-htmlmin');
var htmlclean = require('gulp-htmlclean');
var imagemin = require('gulp-imagemin');
var del = require('del');
var runSequence = require('run-sequence');
var Hexo = require('hexo');
// 清除public文件夹
gulp.task('clean', function() {
return del(['public/**/*']);
});
// 利用Hexo API 来生成博客内容, 效果和在命令行运行: hexo g 一样
var hexo = new Hexo(process.cwd(), {});
gulp.task('generate', function(cb) {
hexo.init().then(function() {
return hexo.call('generate', {
watch: false
});
}).then(function() {
return hexo.exit();
}).then(function() {
return cb()
}).catch(function(err) {
console.log(err);
hexo.exit(err);
return cb(err);
})
})
// 压缩public目录下的所有css
gulp.task('minify-css', function() {
return gulp.src('./public/**/*.css')
.pipe(minifycss({
compatibility: 'ie8',
rebase: false,
}))
.pipe(gulp.dest('./public'));
});
// 压缩public目录下的所有html
gulp.task('minify-html', function() {
return gulp.src('./public/**/*.html')
.pipe(htmlclean())
.pipe(htmlmin({
removeComments: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
}))
.pipe(gulp.dest('./public'))
});
// 压缩public目录下的所有js
gulp.task('minify-js', function() {
return gulp.src('./public/**/*.js')
.pipe(uglify())
.pipe(gulp.dest('./public'));
});
// 压缩public目录下的所有img: 这个采用默认配置
gulp.task('minify-img', function() {
return gulp.src('./public/images/**/*.*')
.pipe(imagemin())
.pipe(gulp.dest('./public/images'))
})
// 同上,压缩图片,这里采用了: 最大化压缩效果。
gulp.task('minify-img-aggressive', function() {
return gulp.src('./public/images/**/*.*')
.pipe(imagemin(
[imagemin.gifsicle({'optimizationLevel': 3}),
imagemin.jpegtran({'progressive': true}),
imagemin.optipng({'optimizationLevel': 7}),
imagemin.svgo()],
{'verbose': true}))
.pipe(gulp.dest('./public/images'))
})
// 用run-sequence并发执行,同时处理html,css,js,img
gulp.task('compress', function(cb) {
runSequence(['minify-html', 'minify-css', 'minify-js', 'minify-img-aggressive'], cb);
});
// 执行顺序: 清除public目录 -> 产生原始博客内容 -> 执行压缩混淆
gulp.task('build', function(cb) {
runSequence('clean', 'generate', 'compress', cb)
});
gulp.task('default', ['build'])
// from https://www.ofind.cn/blog/HEXO/Gulp-Plgues.html
/* npm install 超时请使用:(两条命令在当前文件目录下执行)
npm config set registry https://registry.npm.taobao.org
npm install
或者:
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install
*/
var gulp = require('gulp');
var debug = require('gulp-debug');
var cleancss = require('gulp-clean-css'); //css压缩组件
var cssversion = require('gulp-make-css-url-version'); //css资源添加版本号
var uglify = require('gulp-uglify'); //js压缩组件
var htmlmin = require('gulp-htmlmin'); //html压缩组件
var htmlclean = require('gulp-htmlclean'); //html清理组件
var assetRev = require('gulp-asset-rev'); //版本控制插件
var runSequence = require('run-sequence'); //异步执行组件
var changed = require('gulp-changed'); //文件更改校验组件
var gulpif = require('gulp-if') //任务 帮助调用组件
var plumber = require('gulp-plumber'); //容错组件(发生错误不跳出任务,并报出错误内容)
var isScriptAll = true; //是否处理所有文件,(true|处理所有文件)(false|只处理有更改的文件)
var isDebug = true; //是否调试显示 编译通过的文件
// 压缩js文件
gulp.task('compressJs', function () {
var option = {
// preserveComments: 'all',//保留所有注释
mangle: true, //类型:Boolean 默认:true 是否修改变量名
compress: true //类型:Boolean 默认:true 是否完全压缩
}
return gulp.src(['./_Web/**/*.js','!./_Web/**/*.min.js']) //排除的js
.pipe(gulpif(!isScriptAll, changed('./_Web')))
.pipe(gulpif(isDebug,debug({title: 'Compress JS:'})))
.pipe(plumber())
.pipe(uglify(option)) //调用压缩组件方法uglify(),对合并的文件进行压缩
.pipe(gulp.dest('./_Web')); //输出到目标目录
});
// 压缩css文件
gulp.task('compressCss', function () {
return gulp.src('./_Web/**/*.css')
.pipe(gulpif(!isScriptAll, changed('./_Web')))
.pipe(gulpif(isDebug,debug({title: 'Compress CSS:'})))
.pipe(plumber())
.pipe(cleancss({rebase: false}))
.pipe(gulp.dest('./_Web'));
});
// 压缩html文件
gulp.task('compressHtml', function () {
var cleanOptions = {
protect: /<\!--%fooTemplate\b.*?%-->/g, //忽略处理
unprotect: /<script [^>]*\btype="text\/x-handlebars-template"[\s\S]+?<\/script>/ig //特殊处理
}
var minOption = {
collapseWhitespace: true, //压缩HTML
collapseBooleanAttributes: true, //省略布尔属性的值 <input checked="true"/> ==> <input />
removeEmptyAttributes: true, //删除所有空格作属性值 <input id="" /> ==> <input />
removeScriptTypeAttributes: true, //删除<script>的type="text/javascript"
removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
removeComments: true, //清除HTML注释
minifyJS: true, //压缩页面JS
minifyCSS: true, //压缩页面CSS
minifyURLs: true //替换页面URL
};
return gulp.src('./_Web/**/*.html')
.pipe(gulpif(isDebug,debug({title: 'Compress HTML:'})))
.pipe(plumber())
.pipe(htmlclean(cleanOptions))
.pipe(htmlmin(minOption))
.pipe(gulp.dest('./_Web'));
});
// 默认任务
gulp.task('default', function () {
runSequence.options.ignoreUndefinedTasks = true;
runSequence('compressHtml','compressCss','compressJs');
});
//from http://gtskk.github.io/2015/09/21/gulp-hexo/
var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
var pngquant = require('imagemin-pngquant');
var imageminGifsicle = require('imagemin-gifsicle');
var imageminJpegtran = require('imagemin-jpegtran');
var Hexo = require('hexo'),
hexo = new Hexo(process.cwd(), {}); // 初始化一个hexo对象
// 处理图片
gulp.task('img', function () {
return gulp.src(['src_images/*', 'src_images/*/*'])
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant(), imageminGifsicle(), imageminJpegtran()]
}))
.pipe(gulp.dest('source/images'));
});
// 下面就是hexo有关的操作,主要通过hexo.call()去执行
// 启动Hexo服务器
gulp.task('server', ['img'], function() {
hexo.init().then(function() {
return hexo.call('server', {});
}).catch(function(err) {
console.log(err);
});
});
// 部署到服务器
gulp.task('deploy', function() {
hexo.init().then(function() {
return hexo.call('deploy', {generate: true});
}).catch(function(err) {
console.log(err);
});
});
gulp.task('default', ['img', 'server']);
//from https://sungd.github.io/2016/07/14/gulp-hexo/
// npm install gulp-shell gulp-clean gulp-minify-html gulp-minify-css gulp-uglify gulp-notify --save-dev
var gulp = require('gulp'),
clean = require('gulp-clean'),
minifyCss = require('gulp-minify-css'),
minifyHtml = require('gulp-minify-html'),
uglify = require('gulp-uglify'),
notify = require('gulp-notify'),
shell = require('gulp-shell');
//清空dest文件夹
gulp.task("clean",function() {
return gulp.src("./dst/*")
.pipe(clean());
});
// 压缩css文件,已压缩文件不用再压缩
gulp.task("css",function() {
return gulp.src(["public/**/*.css","!public/**/*.min.css"])
.pipe(minifyCss({compatibility: "ie8"}))
.pipe(gulp.dest("./dst/"));
});
// 压缩js文件
gulp.task("js",function() {
return gulp.src(["public/**/*.js","!public/**/*.min.js"])
.pipe(uglify())
.pipe(gulp.dest("./dst/"));
});
// 压缩html文件
gulp.task("html",function() {
return gulp.src("public/**/*.html")
.pipe(minifyHtml())
.pipe(gulp.dest("./dst/"));
});
// 设置默认任务,command line下输入gulp执行
// clean任务执行完成了才会去运行其他的任务,在gulp.start()里的任务执行的顺序是不确定的,所以将要在它们之前执行的任务写在数组里面
gulp.task("default",['clean'],function() {
gulp.start('css', 'js', 'html');
});
// det文件复制到public
gulp.task("mv",function() {
return gulp.src("./dst/*")
.pipe(shell([
"cp -r ./dst/* ./public/"
]));
});
// from https://clearsky.me/hexo-gulp-compress.html
var gulp = require('gulp');
minifycss = require('gulp-clean-css');
uglify = require('gulp-uglify');
htmlmin = require('gulp-htmlmin');
htmlclean = require('gulp-htmlclean');
imagemin = require('gulp-imagemin');
// 压缩 public 目录内 css
gulp.task('minify-css', function() {
return gulp.src('./public/**/*.css')
.pipe(minifycss({
advanced: true,//类型:Boolean 默认:true [是否开启高级优化(合并选择器等)]
compatibility: 'ie7',//保留ie7及以下兼容写法 类型:String 默认:''or'*' [启用兼容模式; 'ie7':IE7兼容模式,'ie8':IE8兼容模式,'*':IE9+兼容模式]
keepBreaks: true,//类型:Boolean 默认:false [是否保留换行]
keepSpecialComments: '*'
//保留所有特殊前缀 当你用autoprefixer生成的浏览器前缀,如果不加这个参数,有可能将会删除你的部分前缀
}))
.pipe(gulp.dest('./public'));
});
// 压缩 public 目录内 html
gulp.task('minify-html', function() {
return gulp.src('./public/**/*.html')
.pipe(htmlclean())
.pipe(htmlmin({
removeComments: true,//清除 HTML 注释
collapseWhitespace: true,//压缩 HTML
collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
removeScriptTypeAttributes: true,//删除 <script> 的 type="text/javascript"
removeStyleLinkTypeAttributes: true,//删除 <style> 和 <link> 的 type="text/css"
minifyJS: true,//压缩页面 JS
minifyCSS: true//压缩页面 CSS
}))
.pipe(gulp.dest('./public'))
});
// 压缩 public/js 目录内 js
gulp.task('minify-js', function() {
return gulp.src('./public/**/*.js')
.pipe(uglify())
.pipe(gulp.dest('./public'));
});
// 压缩 public/uploads 目录内图片
gulp.task('minify-images', function() {
gulp.src('./public/uploads/**/*.*')
.pipe(imagemin({
optimizationLevel: 5, //类型:Number 默认:3 取值范围:0-7(优化等级)
progressive: true, //类型:Boolean 默认:false 无损压缩jpg图片
interlaced: false, //类型:Boolean 默认:false 隔行扫描gif进行渲染
multipass: false, //类型:Boolean 默认:false 多次优化svg直到完全优化
}))
.pipe(gulp.dest('./public/uploads'));
});
// 执行 gulp 命令时执行的任务
gulp.task('default', [
'minify-html','minify-css','minify-js','minify-images'
]);
//from https://gist.github.com/phawxby/9e41cb8d57ca5106046de80d8b24e308
'use strict';
// Based on https://gist.github.com/zhoujiealex/4d926889b02b85d4d8d73f036ef728eb
let Promise = require('bluebird');
let gulp = require('gulp');
let cssnano = require('gulp-cssnano');
let uglify = require('gulp-uglify');
let htmlmin = require('gulp-htmlmin');
let htmlclean = require('gulp-htmlclean');
let imagemin = require('gulp-imagemin');
let del = require('del');
let runSequence = require('run-sequence');
let Hexo = require('hexo');
let debug = require('gulp-debug');
gulp.task('clean', function() {
return del(['public/**/*']);
});
// generate html with 'hexo generate'
var hexo = new Hexo(process.cwd(), {});
hexo.extend.filter.register('after_generate', function(){
return new Promise(function(resolve, reject) {
runSequence(['compress'], resolve);
});
});
gulp.task('generate', function(cb) {
hexo.init().then(function() {
return hexo.call('generate', {
watch: false
});
}).then(function() {
return hexo.exit();
}).then(function() {
return cb()
}).catch(function(err) {
console.log(err);
hexo.exit(err);
return cb(err);
})
});
gulp.task('deploy', function(cb) {
hexo.init().then(function() {
return hexo.call('deploy', {
watch: false
});
}).then(function() {
return hexo.exit();
}).then(function() {
return cb()
}).catch(function(err) {
console.log(err);
hexo.exit(err);
return cb(err);
})
});
gulp.task('minify-css', function() {
return gulp.src(['./public/**/*.css', './.deploy_git/**/*.css'])
.pipe(debug({title: 'minify-css:', showFiles: false}))
.pipe(cssnano())
.pipe(gulp.dest('./public'));
});
gulp.task('minify-html', function() {
return gulp.src(['./public/**/*.html', './.deploy_git/**/*.html'])
.pipe(debug({title: 'minify-html:', showFiles: false}))
.pipe(htmlclean())
.pipe(htmlmin({
removeComments: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
}))
.pipe(gulp.dest('./public'))
});
gulp.task('minify-js', function() {
return gulp.src(['./public/**/*.js', './.deploy_git/**/*.js'])
.pipe(debug({title: 'minify-js:', showFiles: false}))
.pipe(uglify())
.pipe(gulp.dest('./public'));
});
gulp.task('minify-img', function() {
return gulp.src(['./public/images/*', './.deploy_git/images/*'])
.pipe(debug({title: 'minify-img:', showFiles: false}))
.pipe(imagemin())
.pipe(gulp.dest('./public/images'))
})
gulp.task('compress', function(cb) {
runSequence(['minify-html', 'minify-css', 'minify-js', 'minify-img'], cb);
});
gulp.task('build', function(cb) {
runSequence('clean', 'generate', 'compress', cb)
});
gulp.task('default', [])
以上是关于javascript 各种hexo gulpfile.js收集的主要内容,如果未能解决你的问题,请参考以下文章