-
Notifications
You must be signed in to change notification settings - Fork 98
/
gulpfile.js
288 lines (262 loc) · 7.04 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const gulp = require('gulp');
const gzip = require('gulp-gzip');
const path = require('path');
const fs = require('fs');
const clean = require('gulp-clean');
const exec = require('child_process').exec;
const yaml = require('yamljs');
const inquirer = require('inquirer');
const resolve = (name) => path.resolve(__dirname, name);
gulp.task('clean', () => {
return gulp.src([
'./templates/',
'./theme-butterfly-dist.zip',
], {
read: false,
allowEmpty: true,
}).pipe(
clean({
force: true,
}),
);
});
const sass = require('gulp-sass')(require('sass'));
const rename = require('gulp-rename');
const autoPrefix = require('gulp-autoprefixer');
const postcss = require('gulp-postcss');
gulp.task('css', function() {
return gulp.src('./src/scss/page/*.scss').
pipe(sass({}, {})).
pipe(postcss([
require('postcss-import')({
path: ['node_modules'],
}),
require('css-mqpacker')(),
])).
pipe(require('gulp-clean-css')({
level: 2,
})).
pipe(require('gulp-csso')()).
pipe(require('gulp-minify-css')({
compatibility: 'ie8',
keepSpecialComments: false,
restructuring: true,
})).
pipe(autoPrefix({
overrideBrowserslist: [
'> 5%',
'last 2 versions',
'last 3 Safari versions',
'Firefox >= 20',
],
cascade: false,
})).
pipe(rename({suffix: '.min'})).
pipe(gulp.dest('./templates/assets/css'));
});
const uglify = require('gulp-uglify');
const TerserPlugin = require('terser-webpack-plugin');
const webpack = require('webpack-stream');
const zip = require('gulp-zip');
gulp.task('js', function() {
const getEntryData = () => {
try {
let files = fs.readdirSync('./src/js/page', 'utf-8');
const result = {};
for (let i = 0; i < files.length; i++) {
const fileName = files[i].replace(/.js$/, '');
result[fileName] = resolve(`./src/js/page/${files[i]}`);
}
return result;
} catch (error) {
throw new Error(error);
}
};
return webpack({
mode: 'production',
entry: getEntryData(),
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: resolve('src'),
exclude: resolve('node_modules'),
options: {
presets: ['@babel/preset-env'],
plugins: [
['@babel/plugin-proposal-decorators', {'legacy': true}],
'@babel/plugin-transform-runtime',
],
},
},
{
test: /\.css$/,
use: ['css-loader'],
},
],
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
format: {
comments: false,
},
},
extractComments: false,
}),
],
},
stats: 'errors-only',
output: {
filename: '[name].min.js',
},
})
// .pipe(uglify())
.pipe(gulp.dest('./templates/assets/js')).pipe(
gzip({
threshold: '10kb',
}),
).pipe(gulp.dest('./templates/assets/js'));
});
gulp.task('js-plugins-loading', function() {
const entryRoot = './src/plugins/loading/';
const outputRoot = './templates/assets/plugins/loading/';
const getEntryData = () => {
try {
let files = fs.readdirSync(entryRoot, 'utf-8');
const result = {};
for (let i = 0; i < files.length; i++) {
const fileName = files[i].replace(/.js$/, '');
result[fileName] = resolve(`${entryRoot}${files[i]}`);
}
return result;
} catch (error) {
throw new Error(error);
}
};
return webpack({
mode: 'production',
entry: getEntryData(),
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: resolve('src'),
exclude: resolve('node_modules'),
options: {
presets: ['@babel/preset-env'],
},
},
{
test: /\.css$/,
use: ['css-loader'],
},
],
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
format: {
comments: false,
},
},
extractComments: false,
}),
],
},
stats: 'errors-only',
output: {
filename: '[name].min.js',
},
})
// .pipe(uglify())
.pipe(gulp.dest(outputRoot)).pipe(
gzip({
threshold: '10kb',
}),
).pipe(gulp.dest(outputRoot));
});
const htmlMin = require('gulp-htmlmin');
gulp.task('html', function() {
return gulp.src('./src/html/**/*.html').pipe(htmlMin({
removeComments: true,//清除HTML注释
collapseWhitespace: true,//压缩HTML
collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
minifyJS: {
ignoreCustomComments: [
/^\s*\/\*!\[\[.*?]]\*\/\s*$/, // 匹配 /*[[ ... ]]*/ 形式的注释
],
},//压缩页面JS
minifyCSS: false,
ignoreCustomFragments: [/<th:block[^>]*\/>/],
})).pipe(gulp.dest('./templates'));
});
gulp.task('zip', done => {
gulp.src([
'./templates/**/*.*',
'./settings.yaml',
'./annotation-setting.yaml',
'./theme.yaml',
], {base: '.'}).pipe(zip('theme-butterfly-dist.zip')).pipe(gulp.dest('./'));
done();
console.log(
'\x1B[32m打包完成,生产文件在根目录下的 theme-butterfly-dist.zip\x1B[0m');
});
gulp.task('release', async done => {
const {value} = await inquirer.prompt([
{
type: 'list', // 交互类型 -- 单选(无序)
message: '请选择发布版:', // 引导词
name: 'value', // 自定义的字段名
choices: [
{name: '小版本', value: 'patch'},
{name: '中版本', value: 'minor'},
{name: '大版本', value: 'major'},
], // 选项列表
},
]);
await exec(`npm version ${value} --no-git-tag-version`,
async (error, stdout, stderr) => {
if(error) {
console.error(`执行出错: ${error}`);
return;
}
const version = stdout.replace('v', '').replace('\n', '');
const themeYaml = yaml.load('./theme.yaml');
themeYaml.spec.version = version;
await fs.writeFileSync('./theme.yaml',
yaml.dump(themeYaml, './theme.yaml'), 'utf8');
});
done();
});
// 复制文件夹 font plugins
gulp.task('copy-folder', function() {
return gulp.src([
'./src/font/**/*',
'./src/plugins/**/*',
], {base: 'src'}) // 设置基本路径为 'src'
.pipe(gulp.dest('./templates/assets/'));
});
gulp.task(
'watch',
function() {
gulp.watch(['./src/scss/**/*.scss'], gulp.series('css'));
gulp.watch(['./src/js/**/**/*.js'], gulp.series('js'));
gulp.watch(['./src/html/**/**/*.html'], gulp.series('html'));
},
);
gulp.task(
'default',
gulp.series(
gulp.parallel('copy-folder', 'css', 'js', 'html'),
),
);