You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
layui/gulpfile.js

156 lines
4.1 KiB

const path = require('path');
3 years ago
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');
const sourcemaps = require('gulp-sourcemaps');
const zip = require('gulp-zip');
3 years ago
const del = require('del');
const minimist = require('minimist');
const yargs = require('yargs');
const pkg = require('./package.json');
3 years ago
3 years ago
// 基础配置
3 years ago
const config = {
// 头部注释
3 years ago
comment: [
'/** v<%= pkg.version %> | <%= pkg.license %> Licensed */<%= js %>',
{pkg: pkg, js: ';'}
],
// 全部模块
modules: 'lay,laytpl,laypage,laydate,jquery,layer,util,dropdown,slider,colorpicker,element,upload,form,table,treeTable,tree,transfer,carousel,rate,flow,code'
3 years ago
};
7 years ago
3 years ago
// 获取参数
const argv = minimist(process.argv.slice(2), {
7 years ago
default: {
version: pkg.version
7 years ago
}
3 years ago
});
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;
7 years ago
// 打包目标路径
const dest = './dist';
7 years ago
3 years ago
// js
3 years ago
const js = () => {
let src = [
3 years ago
'./src/**/{layui,layui.all,'+ config.modules +'}.js'
];
return gulp.src(src)
.pipe(sourcemaps.init())
.pipe(uglify({
3 years ago
output: {
ascii_only: true // escape Unicode characters in strings and regexps
2 years ago
},
ie: true
}))
.pipe(concat('layui.js', {newLine: ''}))
3 years ago
.pipe(header.apply(null, config.comment))
.pipe(sourcemaps.write('.'))
3 years ago
.pipe(gulp.dest(dest));
};
3 years ago
// css
3 years ago
const css = () => {
let src = [
'./src/css/**/{layui,*}.css'
3 years ago
];
return gulp.src(src)
.pipe(sourcemaps.init())
.pipe(cleanCSS({
3 years ago
compatibility: 'ie8'
}))
.pipe(concat('layui.css', {newLine: ''}))
.pipe(sourcemaps.write('.'))
3 years ago
.pipe(gulp.dest(dest +'/css'));
};
3 years ago
// files
3 years ago
const files = () => {
let src = ['./src/**/*.{eot,svg,ttf,woff,woff2,html,json,png,jpg,gif}'];
3 years ago
return gulp.src(src)
.pipe(gulp.dest(dest));
};
7 years ago
// 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}/**/*`;
// 复制 css js
gulp.src(`${src}.{css,js}`)
12 months ago
.pipe(replace(/\n\/(\*|\/)\#[\s\S]+$/, '')) // 过滤 css,js 的 map 特定注释
.pipe(gulp.dest(copyDest));
// 复制其他文件
return gulp.src([
src,
`!${src}.{css,js,map}` // 过滤 map 文件
])
12 months ago
.pipe(replace(/\n\/(\*|\/)\#[\s\S]+$/, '')) // 过滤 css,js 的 map 特定注释
.pipe(gulp.dest(copyDest));
});
// 发行
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))
}
);
7 years ago
3 years ago
// helper
3 years ago
exports.help = () => {
let usage = '\nUsage: gulp [options] tasks';
let parser = yargs.usage(usage, {
3 years ago
dest: {
type: 'string',
desc: '自定义输出路径'
},
vs: {
type: 'boolean',
desc: '生成一个带版本号的文件夹'
3 years ago
}
});
3 years ago
parser.showHelp(console.log);
console.log([
'Tasks:',
' default 默认任务',
' release 发行任务',
' cp 将 dist 目录复制一份到参数 --dest 指向的目录'
3 years ago
].join('\n'), '\n\nExamples:\n gulp cp --dest ./v --vs', '\n');
3 years ago
return gulp.src('./');
};