layui/gulpfile.js

173 lines
4.6 KiB
JavaScript
Raw Normal View History

2022-06-29 08:56:33 +08:00
'use strict';
2017-08-21 08:51:13 +08:00
2022-06-08 01:21:33 +08:00
const pkg = require('./package.json');
const gulp = require('gulp');
const uglify = require('gulp-uglify');
const cleanCSS = require('gulp-clean-css');
const concat = require('gulp-concat');
const rename = require('gulp-rename');
const replace = require('gulp-replace');
const header = require('gulp-header');
const footer = require('gulp-footer');
const del = require('del');
const minimist = require('minimist');
const yargs = require('yargs');
2022-05-18 22:35:13 +08:00
2022-06-05 23:55:31 +08:00
// 基础配置
2022-06-08 01:21:33 +08:00
const config = {
2022-05-18 22:35:13 +08:00
//注释
comment: [
2022-08-31 01:47:42 +08:00
'/** v<%= pkg.version %> | <%= pkg.license %> Licensed */<%= js %>'
2022-05-18 22:35:13 +08:00
,{pkg: pkg, js: ';'}
]
//模块
2022-08-31 01:47:42 +08:00
,modules: 'lay,laytpl,laypage,laydate,jquery,layer,util,dropdown,slider,colorpicker,element,upload,form,table,tree,transfer,carousel,rate,flow,code'
2022-05-18 22:35:13 +08:00
};
2017-08-21 08:51:13 +08:00
2022-06-05 23:55:31 +08:00
// 获取参数
2022-06-08 01:21:33 +08:00
const argv = require('minimist')(process.argv.slice(2), {
2017-08-21 08:51:13 +08:00
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
});
2022-06-05 23:55:31 +08:00
// 前置目录
2022-06-08 01:21:33 +08:00
const dir = {
2022-05-18 22:35:13 +08:00
rls: './release/zip/layui-v' + pkg.version
2017-08-21 08:51:13 +08:00
};
2022-06-05 23:55:31 +08:00
// 输出目录
2022-06-08 01:21:33 +08:00
const dest = ({
2022-05-18 22:35:13 +08:00
dist: './dist'
,rls: dir.rls + '/layui'
}[argv.dest || 'dist'] || argv.dest) + (argv.vs ? '/'+ pkg.version : '');
2017-08-21 08:51:13 +08:00
2022-06-05 23:55:31 +08:00
// js
2022-06-08 01:21:33 +08:00
const js = () => {
let src = [
2022-05-18 22:35:13 +08:00
'./src/**/{layui,layui.all,'+ config.modules +'}.js'
];
return gulp.src(src).pipe(uglify({
output: {
ascii_only: true //escape Unicode characters in strings and regexps
2022-06-23 00:03:41 +08:00
},
ie: true
2022-05-18 22:35:13 +08:00
})).pipe(concat('layui.js', {newLine: ''}))
.pipe(header.apply(null, config.comment))
.pipe(gulp.dest(dest));
};
2022-06-05 23:55:31 +08:00
// css
2022-06-08 01:21:33 +08:00
const css = () => {
let src = [
2022-08-31 01:47:42 +08:00
'./src/css/layui.css',
'./src/css/modules/**/*.css',
'!./src/css/**/font.css'
2022-06-08 01:21:33 +08:00
];
2022-05-18 22:35:13 +08:00
return gulp.src(src).pipe(cleanCSS({
compatibility: 'ie8'
}))
2022-08-31 01:47:42 +08:00
.pipe(concat('layui.css', {newLine: ''}))
2022-05-18 22:35:13 +08:00
.pipe(gulp.dest(dest +'/css'));
};
2022-06-05 23:55:31 +08:00
// files
2022-06-08 01:21:33 +08:00
const files = () => {
let src = ['./src/**/*.{eot,svg,ttf,woff,woff2,html,json,png,jpg,gif}'];
2022-05-18 22:35:13 +08:00
return gulp.src(src)
.pipe(gulp.dest(dest));
};
2017-08-21 08:51:13 +08:00
2022-06-05 23:55:31 +08:00
// cp
2022-06-08 01:21:33 +08:00
const cp = () => {
2022-05-18 22:35:13 +08:00
return gulp.src('./dist/**/*')
.pipe(gulp.dest(dest));
};
2022-06-05 23:55:31 +08:00
// release
2022-06-08 01:21:33 +08:00
const rls = () => {
2022-05-18 22:35:13 +08:00
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-06-05 23:55:31 +08:00
// clean
2022-06-08 01:21:33 +08:00
const clean = cb => {
2022-05-18 22:35:13 +08:00
return del([dest], {
force: true
});
};
2022-06-08 01:21:33 +08:00
const cleanRLS = cb => {
2022-05-18 22:35:13 +08:00
return del([dir.rls]);
};
2017-09-13 16:06:55 +08:00
2022-06-05 23:55:31 +08:00
// Define all task
2022-05-18 22:35:13 +08:00
exports.js = js;
exports.css = css;
exports.files = files;
exports.default = gulp.series(clean, gulp.parallel(js, css, files)); //default task
2022-06-05 23:55:31 +08:00
exports.cp = gulp.series(clean, cp);
2022-05-18 22:35:13 +08:00
exports.rls = gulp.series(cleanRLS, rls); //release task
2022-06-05 23:55:31 +08:00
// layer task
2022-06-08 01:21:33 +08:00
exports.layer = () => { // gulp layer
let dest = './release/layer';
2017-09-13 16:06:55 +08:00
2022-08-31 01:47:42 +08:00
gulp.src('./src/css/modules/layer.css')
.pipe(gulp.dest(dest + '/src'));
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
2022-06-05 23:55:31 +08:00
// laydate task
2022-06-08 01:21:33 +08:00
exports.laydate = () => { // gulp laydate
let dest = './release/laydate/'; // 发行目录
let 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-06-05 23:55:31 +08:00
// css
2022-05-18 22:35:13 +08:00
gulp.src('./src/css/modules/laydate.css')
2022-08-31 01:47:42 +08:00
.pipe(gulp.dest(dest + 'src'));
2017-08-24 20:26:48 +08:00
2022-06-05 23:55:31 +08:00
// js
2021-05-18 02:42:31 +08:00
return gulp.src(['./src/layui.js', './src/modules/{lay,laydate}.js'])
2022-06-05 23:55:31 +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(replace('}(window, window.document); //gulp build: lay-footer', '')) // 替换 lay.js 的落脚
2022-05-18 22:35:13 +08:00
.pipe(concat('laydate.js', {newLine: ''}))
.pipe(replace(';!function(window, document){ //gulp build: laydate-header', '')) // 替换 laydate.js 的头部
2022-05-18 22:35:13 +08:00
.pipe(header.apply(null, comment)) //追加头部
.pipe(gulp.dest(dest + 'src'));
};
2017-08-21 08:51:13 +08:00
2022-06-05 23:55:31 +08:00
// helper
2022-06-08 01:21:33 +08:00
exports.help = () => {
let usage = '\nUsage: gulp [options] tasks';
let parser = yargs.usage(usage, {
2022-05-18 22:35:13 +08:00
dest: {
type: 'string'
,desc: '定义输出目录可选项dist默认、rls、任意路径'
}
,vs: {
type: 'boolean'
,desc: '生成一个带版本号的文件夹'
}
});
parser.showHelp(console.log);
console.log([
'Tasks:'
,' default 默认任务'
,' rls 发行任务'
2022-06-05 23:55:31 +08:00
,' cp 将 dist 目录复制一份到参数 --dest 指向的目录'
].join('\n'), '\n\nExamples:\n gulp cp --dest ./v --vs', '\n');
2022-05-18 22:35:13 +08:00
return gulp.src('./');
};
2017-08-21 08:51:13 +08:00