layui/gulpfile.js

172 lines
4.3 KiB
JavaScript
Raw Normal View History

2022-05-18 22:35:13 +08:00
/**
* Layui Build
*/
2017-08-21 08:51:13 +08:00
var pkg = require('./package.json');
var gulp = require('gulp');
var uglify = require('gulp-uglify');
2022-05-18 22:35:13 +08:00
var cleanCSS = require('gulp-clean-css');
2017-08-21 08:51:13 +08:00
var concat = require('gulp-concat');
var rename = require('gulp-rename');
2021-03-31 14:07:23 +08:00
var replace = require('gulp-replace');
2017-08-21 08:51:13 +08:00
var header = require('gulp-header');
2021-05-18 02:42:31 +08:00
var footer = require('gulp-footer');
2017-08-21 08:51:13 +08:00
var del = require('del');
var minimist = require('minimist');
2022-05-18 22:35:13 +08:00
var yargs = require('yargs');
//基础配置
var config = {
//注释
comment: [
'/** <%= pkg.version %> | <%= pkg.license %> Licensed */<%= js %>'
,{pkg: pkg, js: ';'}
]
//模块
2022-05-22 08:17:44 +08:00
,modules: 'lay,laytpl,laypage,laydate,jquery,layer,util,dropdown,slider,colorpicker,element,upload,form,table,tree,transfer,carousel,rate,flow,layedit,code'
2022-05-18 22:35:13 +08:00
};
2017-08-21 08:51:13 +08:00
//获取参数
var argv = require('minimist')(process.argv.slice(2), {
default: {
2022-05-18 22:35:13 +08:00
version: pkg.version
2017-08-24 20:26:48 +08:00
}
2022-05-18 22:35:13 +08:00
});
//前置目录
var dir = {
rls: './release/zip/layui-v' + pkg.version
2017-08-21 08:51:13 +08:00
};
2022-05-18 22:35:13 +08:00
//输出目录
var dest = ({
dist: './dist'
,rls: dir.rls + '/layui'
}[argv.dest || 'dist'] || argv.dest) + (argv.vs ? '/'+ pkg.version : '');
2017-08-21 08:51:13 +08:00
2022-05-18 22:35:13 +08:00
//js
var js = function(){
var src = [
'./src/**/{layui,layui.all,'+ config.modules +'}.js'
];
return gulp.src(src).pipe(uglify({
output: {
ascii_only: true //escape Unicode characters in strings and regexps
}
})).pipe(concat('layui.js', {newLine: ''}))
.pipe(header.apply(null, config.comment))
.pipe(gulp.dest(dest));
};
//css
var css = function(){
var src = [
'./src/css/**/*.css'
,'!./src/css/**/font.css'
]
return gulp.src(src).pipe(cleanCSS({
compatibility: 'ie8'
}))
//.pipe(concat('layui.css', {newLine: ''}))
.pipe(gulp.dest(dest +'/css'));
};
//files
var files = function(){
var src = ['./src/**/*.{eot,svg,ttf,woff,woff2,html,json,png,jpg,gif}'];
return gulp.src(src)
.pipe(gulp.dest(dest));
};
2017-08-21 08:51:13 +08:00
2022-05-18 22:35:13 +08:00
//mv
var mv = function(){
return gulp.src('./dist/**/*')
.pipe(gulp.dest(dest));
};
//release
var rls = function(){
return gulp.src('./release/doc/**/*')
.pipe(replace(/[^'"]+(\/layui\.css)/, 'layui/css$1')) //替换 css 引入路径中的本地 path
.pipe(replace(/[^'"]+(\/layui\.js)/, 'layui$1')) //替换 js 引入路径中的本地 path
.pipe(gulp.dest(dir.rls));
};
2017-08-21 08:51:13 +08:00
2022-05-18 22:35:13 +08:00
//clean
var clean = function(cb) {
return del([dest], {
force: true
});
};
var cleanRLS = function(cb) {
return del([dir.rls]);
};
2017-09-13 16:06:55 +08:00
2022-05-18 22:35:13 +08:00
//Define all task
exports.js = js;
exports.css = css;
exports.files = files;
exports.default = gulp.series(clean, gulp.parallel(js, css, files)); //default task
exports.mv = gulp.series(clean, mv);
exports.rls = gulp.series(cleanRLS, rls); //release task
//layer task
exports.layer = function(){ // gulp layer
var dest = './release/layer';
2017-09-13 16:06:55 +08:00
gulp.src('./src/css/modules/layer/default/*')
2022-05-18 22:35:13 +08:00
.pipe(gulp.dest(dest + '/src/theme/default'));
2017-09-13 16:06:55 +08:00
2021-03-31 14:07:23 +08:00
return gulp.src('./src/modules/layer.js')
2022-05-18 22:35:13 +08:00
.pipe(gulp.dest(dest + '/src'));
};
2017-09-13 16:06:55 +08:00
2021-03-31 14:07:23 +08:00
2022-05-18 22:35:13 +08:00
//laydate task
exports.laydate = function(){ // gulp laydate
var dest = './release/laydate/' //发行目录
,comment = [ //注释
2021-05-18 02:42:31 +08:00
'\n/*! \n * <%= title %> \n * <%= license %> Licensed \n */ \n\n'
,{title: 'layDate 日期与时间组件(单独版)', license: 'MIT'}
2021-03-31 14:07:23 +08:00
];
2017-09-13 16:06:55 +08:00
2022-05-18 22:35:13 +08:00
//css
gulp.src('./src/css/modules/laydate.css')
.pipe(gulp.dest(dest + 'src/'));
2017-08-24 20:26:48 +08:00
2022-05-18 22:35:13 +08:00
//js
2021-05-18 02:42:31 +08:00
return gulp.src(['./src/layui.js', './src/modules/{lay,laydate}.js'])
2022-05-18 22:35:13 +08:00
.pipe(replace('win.layui =', 'var layui =')) //将 layui 替换为局部变量
.pipe(replace('})(window); //gulp build: layui-footer', '')) //替换 layui.js 的落脚
.pipe(replace('(function(window){ //gulp build: lay-header', '')) //替换 lay.js 的头部
.pipe(concat('laydate.js', {newLine: ''}))
.pipe(header.apply(null, comment)) //追加头部
.pipe(gulp.dest(dest + 'src'));
};
2017-08-21 08:51:13 +08:00
2022-05-18 22:35:13 +08:00
//help
exports.help = function(){
var usage = '\nUsage: gulp [options] tasks'
,parser = yargs.usage(usage, {
dest: {
type: 'string'
,desc: '定义输出目录可选项dist默认、rls、任意路径'
}
,vs: {
type: 'boolean'
,desc: '生成一个带版本号的文件夹'
}
});
parser.showHelp(console.log);
console.log([
'Tasks:'
,' default 默认任务'
,' rls 发行任务'
,' mv 将 dist 目录复制并拷贝一份到参数 --dest 指向的目录'
].join('\n'), '\n\nExamples:\n gulp mv --dest ./v --vs', '\n');
return gulp.src('./');
};
2017-08-21 08:51:13 +08:00