layui/gulpfile.js

156 lines
4.1 KiB
JavaScript
Raw Normal View History

const path = require('path');
2022-06-08 01:21:33 +08:00
const gulp = require('gulp');
const uglify = require('gulp-uglify');
const cleanCSS = require('gulp-clean-css');
const concat = require('gulp-concat');
const replace = require('gulp-replace');
const header = require('gulp-header');
2023-10-23 10:32:05 +08:00
const sourcemaps = require('gulp-sourcemaps');
const zip = require('gulp-zip');
2022-06-08 01:21:33 +08:00
const del = require('del');
const minimist = require('minimist');
const yargs = require('yargs');
const pkg = require('./package.json');
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: [
2023-06-20 10:31:58 +08:00
'/** v<%= pkg.version %> | <%= pkg.license %> Licensed */<%= js %>',
{pkg: pkg, js: ';'}
],
// 全部模块
2023-06-20 10:31:58 +08:00
modules: 'lay,laytpl,laypage,laydate,jquery,layer,util,dropdown,slider,colorpicker,element,upload,form,table,treeTable,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
// 获取参数
const argv = minimist(process.argv.slice(2), {
2017-08-21 08:51:13 +08:00
default: {
2023-10-23 10:32:05 +08:00
version: pkg.version
2017-08-24 20:26:48 +08:00
}
2022-05-18 22:35:13 +08:00
});
const rlsFileName = `${pkg.name}-v${pkg.version}`; // 发行文件名
const rlsDest = `./release/zip/${rlsFileName}/${pkg.name}`; // 发行目标路径
const rlsDirname = path.dirname(rlsDest); // 发行目录名
// 复制目标路径
const copyDest = argv.dest
? path.join(argv.dest, (argv.vs ? '/' + pkg.version : ''))
: rlsDest;
2017-08-21 08:51:13 +08:00
// 打包目标路径
const dest = './dist';
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'
];
2023-10-23 10:32:05 +08:00
return gulp.src(src)
.pipe(sourcemaps.init())
.pipe(uglify({
2022-05-18 22:35:13 +08:00
output: {
2023-04-21 10:49:58 +08:00
ascii_only: true // escape Unicode characters in strings and regexps
2022-06-23 00:03:41 +08:00
},
ie: true
2023-10-23 10:32:05 +08:00
}))
.pipe(concat('layui.js', {newLine: ''}))
2022-05-18 22:35:13 +08:00
.pipe(header.apply(null, config.comment))
2023-11-07 23:04:33 +08:00
.pipe(sourcemaps.write('.'))
2022-05-18 22:35:13 +08:00
.pipe(gulp.dest(dest));
};
2023-10-23 10:32:05 +08:00
2022-06-05 23:55:31 +08:00
// css
2022-06-08 01:21:33 +08:00
const css = () => {
let src = [
2023-10-25 11:18:21 +08:00
'./src/css/**/{layui,*}.css'
2022-06-08 01:21:33 +08:00
];
2023-10-23 10:32:05 +08:00
return gulp.src(src)
.pipe(sourcemaps.init())
.pipe(cleanCSS({
2022-05-18 22:35:13 +08:00
compatibility: 'ie8'
}))
2022-08-31 01:47:42 +08:00
.pipe(concat('layui.css', {newLine: ''}))
2023-11-07 23:04:33 +08:00
.pipe(sourcemaps.write('.'))
2022-05-18 22:35:13 +08:00
.pipe(gulp.dest(dest +'/css'));
};
2023-11-07 23:04:33 +08:00
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
// clean
const clean = () => {
return del([dest]);
};
// 默认任务
exports.default = gulp.series(clean, gulp.parallel(js, css, files));
// 复制 dist 目录到指定路径
exports.cp = gulp.series(() => del(copyDest), () => {
const src = `${dest}/**/*`;
2023-10-25 11:18:21 +08:00
// 复制 css js
gulp.src(`${src}.{css,js}`)
2023-12-17 16:01:33 +08:00
.pipe(replace(/\n\/(\*|\/)\#[\s\S]+$/, '')) // 过滤 css,js 的 map 特定注释
.pipe(gulp.dest(copyDest));
2023-10-25 11:18:21 +08:00
// 复制其他文件
return gulp.src([
src,
`!${src}.{css,js,map}` // 过滤 map 文件
2023-10-25 11:18:21 +08:00
])
2023-12-17 16:01:33 +08:00
.pipe(replace(/\n\/(\*|\/)\#[\s\S]+$/, '')) // 过滤 css,js 的 map 特定注释
.pipe(gulp.dest(copyDest));
});
2023-11-07 23:04:33 +08:00
// 发行
exports.release = gulp.series(
() => del([rlsDirname]), // 清理发行目录
() => { // 生成说明
return gulp.src('./release/introduce/**/*')
.pipe(replace(/[^'"]+(\/layui\.css)/, 'layui/css$1')) // 替换 css 引入路径中的本地 path
.pipe(replace(/[^'"]+(\/layui\.js)/, 'layui$1')) // 替换 js 引入路径中的本地 path
.pipe(gulp.dest(rlsDirname)) // 用于本地
.pipe(gulp.dest('./examples/introduce')); // 用于 Github actions
},
exports.cp, // 复制 dist 目录文件
() => { // 生成 ZIP 压缩包
const base = path.dirname(rlsDirname);
return gulp.src(rlsDirname + '/**/*', {
base: base
})
.pipe(zip(`${rlsFileName}.zip`))
.pipe(gulp.dest(base))
}
);
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: {
2023-06-20 10:31:58 +08:00
type: 'string',
desc: '自定义输出路径'
2023-06-20 10:31:58 +08:00
},
vs: {
type: 'boolean',
desc: '生成一个带版本号的文件夹'
2022-05-18 22:35:13 +08:00
}
});
2023-11-07 23:04:33 +08:00
2022-05-18 22:35:13 +08:00
parser.showHelp(console.log);
console.log([
2023-06-20 10:31:58 +08:00
'Tasks:',
' default 默认任务',
' release 发行任务',
2023-06-20 10:31:58 +08:00
' cp 将 dist 目录复制一份到参数 --dest 指向的目录'
2022-06-05 23:55:31 +08:00
].join('\n'), '\n\nExamples:\n gulp cp --dest ./v --vs', '\n');
2022-05-18 22:35:13 +08:00
return gulp.src('./');
};