forked from layui/layui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
172 lines (150 loc) · 4.61 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict';
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');
// 基础配置
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 = require('minimist')(process.argv.slice(2), {
default: {
version: pkg.version
}
});
// 前置目录
const dir = {
rls: './release/zip/layui-v' + pkg.version
};
// 输出目录
const dest = ({
dist: './dist'
,rls: dir.rls + '/layui'
}[argv.dest || 'dist'] || argv.dest) + (argv.vs ? '/'+ pkg.version : '');
// js
const js = () => {
let 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
},
ie: true
})).pipe(concat('layui.js', {newLine: ''}))
.pipe(header.apply(null, config.comment))
.pipe(gulp.dest(dest));
};
// css
const css = () => {
let src = [
'./src/css/layui.css',
'./src/css/modules/**/*.css',
'!./src/css/**/font.css'
];
return gulp.src(src).pipe(cleanCSS({
compatibility: 'ie8'
}))
.pipe(concat('layui.css', {newLine: ''}))
.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));
};
// cp
const cp = () => {
return gulp.src('./dist/**/*')
.pipe(gulp.dest(dest));
};
// release
const rls = () => {
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));
};
// clean
const clean = cb => {
return del([dest], {
force: true
});
};
const cleanRLS = cb => {
return del([dir.rls]);
};
// 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.cp = gulp.series(clean, cp);
exports.rls = gulp.series(cleanRLS, rls); // release task
// layer task
exports.layer = () => { // gulp layer
let dest = './release/layer';
gulp.src('./src/css/modules/layer.css')
.pipe(gulp.dest(dest + '/src'));
return gulp.src('./src/modules/layer.js')
.pipe(gulp.dest(dest + '/src'));
};
// laydate task
exports.laydate = () => { // gulp laydate
let dest = './release/laydate/'; // 发行目录
let comment = [ // 注释
'\n/** \n * <%= title %> \n * <%= license %> Licensed \n */ \n\n'
,{title: 'laydate 日期与时间组件(单独版)', license: 'MIT'}
];
// css
gulp.src('./src/css/modules/laydate.css')
.pipe(gulp.dest(dest + 'src'));
// js
return gulp.src(['./src/layui.js', './src/modules/{lay,laydate}.js'])
.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 的落脚
.pipe(concat('laydate.js', {newLine: ''}))
.pipe(replace(';!function(window, document){ // gulp build: laydate-header', '')) // 替换 laydate.js 的头部
.pipe(header.apply(null, comment)) // 追加头部
.pipe(gulp.dest(dest + 'src'));
};
// helper
exports.help = () => {
let usage = '\nUsage: gulp [options] tasks';
let parser = yargs.usage(usage, {
dest: {
type: 'string'
,desc: '定义输出目录,可选项:dist(默认)、rls、任意路径'
}
,vs: {
type: 'boolean'
,desc: '生成一个带版本号的文件夹'
}
});
parser.showHelp(console.log);
console.log([
'Tasks:'
,' default 默认任务'
,' rls 发行任务'
,' cp 将 dist 目录复制一份到参数 --dest 指向的目录'
].join('\n'), '\n\nExamples:\n gulp cp --dest ./v --vs', '\n');
return gulp.src('./');
};