forked from layui/layui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
155 lines (139 loc) · 4.08 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const path = require('path');
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');
const del = require('del');
const minimist = require('minimist');
const yargs = require('yargs');
const pkg = require('./package.json');
// 基础配置
const config = {
// 头部注释
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'
};
// 获取参数
const argv = minimist(process.argv.slice(2), {
default: {
version: pkg.version
}
});
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;
// 打包目标路径
const dest = './dist';
// js
const js = () => {
let src = [
'./src/**/{layui,layui.all,'+ config.modules +'}.js'
];
return gulp.src(src)
.pipe(sourcemaps.init())
.pipe(uglify({
output: {
ascii_only: true // escape Unicode characters in strings and regexps
},
ie: true
}))
.pipe(concat('layui.js', {newLine: ''}))
.pipe(header.apply(null, config.comment))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(dest));
};
// css
const css = () => {
let src = [
'./src/css/**/{layui,*}.css'
];
return gulp.src(src)
.pipe(sourcemaps.init())
.pipe(cleanCSS({
compatibility: 'ie8'
}))
.pipe(concat('layui.css', {newLine: ''}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(dest +'/css'));
};
// files
const files = () => {
let src = ['./src/**/*.{eot,svg,ttf,woff,woff2,html,json,png,jpg,gif}'];
return gulp.src(src)
.pipe(gulp.dest(dest));
};
// 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}`)
.pipe(replace(/\n\/(\*|\/)\#[\s\S]+$/, '')) // 过滤 css,js 的 map 特定注释
.pipe(gulp.dest(copyDest));
// 复制其他文件
return gulp.src([
src,
`!${src}.{css,js,map}` // 过滤 map 文件
])
.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))
}
);
// helper
exports.help = () => {
let usage = '\nUsage: gulp [options] tasks';
let parser = yargs.usage(usage, {
dest: {
type: 'string',
desc: '自定义输出路径'
},
vs: {
type: 'boolean',
desc: '生成一个带版本号的文件夹'
}
});
parser.showHelp(console.log);
console.log([
'Tasks:',
' default 默认任务',
' release 发行任务',
' cp 将 dist 目录复制一份到参数 --dest 指向的目录'
].join('\n'), '\n\nExamples:\n gulp cp --dest ./v --vs', '\n');
return gulp.src('./');
};