-
Notifications
You must be signed in to change notification settings - Fork 489
/
gulpfile.js
113 lines (102 loc) · 3.81 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
/**
* @fileoverview 自动构建程序
* @author Jin Yufeng
*/
// 载入 gulp 插件
const gulp = require('gulp')
const htmlmin = require('gulp-htmlmin')
const babel = require('gulp-babel')
const uglify = require('gulp-uglify')
const cleanCss = require('gulp-clean-css')
const clean = require('gulp-clean')
const gulpif = require('gulp-if')
const plumber = require('gulp-plumber')
const size = require('gulp-size')
// 载入构建工具
const config = require('./tools/config')
const converter = require('./tools/converter')
const ifdef = require('./tools/ifdef')
const minifier = require('./tools/minifier')
const plugin = require('./tools/plugin')
// 载入环境信息
const isDev = process.argv.includes('--dev')
let platform = process.argv[3]
if (!platform) {
throw Error('缺少平台信息')
}
platform = platform.substr(2)
/**
* @description 清理文件夹
*/
gulp.task('clean', () => {
return gulp.src(`${isDev ? 'dev' : 'dist'}/${platform === 'all' ? '' : platform + '/'}*`, {
read: false,
allowEmpty: true
})
.pipe(clean())
})
/**
* @description 生成原生组件包(含插件)
* @returns {NodeJS.ReadWriteStream}
*/
function packComp () {
return gulp.src(['plugins/**/*', 'src/**/*'], {
nodir: true
})
// 公共处理
.pipe(plumber()) // 错误处理
.pipe(plugin.build(platform)) // 构建插件
.pipe(ifdef(platform)) // 条件编译
// wxml 处理
.pipe(gulpif(file => file.extname === '.wxml', minifier.wxs())) // 压缩内联 wxs
.pipe(gulpif(file => file.extname === '.wxml', htmlmin(config.htmlmin))) // 压缩 wxml
.pipe(gulpif(file => file.extname === '.html', htmlmin(Object.assign({}, config.htmlmin, { // 压缩 html
minifyCSS: true
}))))
// js 处理
.pipe(gulpif(file => file.extname === '.js' && !file.stem.includes('.min') && (platform !== 'uni-app' || file.relative.includes('static')), babel(config.babel))) // es6 转 es5
.pipe(gulpif(file => file.extname === '.js' && !file.stem.includes('.min') && !isDev && (platform !== 'uni-app' || file.relative.includes('static')), uglify(config.uglify))) // 压缩 js
// wxss(css)处理
.pipe(gulpif(file => file.extname.includes('ss'), cleanCss(config.cleanCss))) // 压缩 wxss
.pipe(plugin.importCss()) // 引入插件中的 css 文件
// json 处理
.pipe(gulpif(file => file.extname === '.json', minifier.json())) // 压缩 json
// 公共处理
.pipe(converter(platform)) // 将微信端的代码转换到各个平台
.pipe(gulpif(!isDev, size({
title: `${platform} 包生成完毕`
})))
.pipe(gulp.dest(file => {
return `${isDev ? 'dev' : 'dist'}/${platform}/${(platform === 'uni-app' && !file.relative.includes('components') && !file.relative.includes('static')) || (platform !== 'uni-app' && isDev) ? 'components/mp-html/' : ''}`
}))
}
gulp.task('build', gulp.series('clean', packComp))
/**
* @description 生成原生示例项目
* @returns {NodeJS.ReadWriteStream}
*/
function packDemo () {
return gulp.src(['tools/demo/**/*', 'test/content.js'], {
nodir: true
})
.pipe(ifdef(platform))
.pipe(gulpif(platform !== 'uni-app', converter(platform)))
.pipe(gulp.dest(`dev/${platform}/`))
}
gulp.task('dev', gulp.series('clean', gulp.parallel(packComp, packDemo)))
/**
* @description 监听文件变化
*/
gulp.task('watch-demo', () => {
gulp.watch(['tools/demo/**/*', 'test/content.js']).on('all', (type, file) => {
console.log(type + ':' + file)
packDemo()
})
})
gulp.task('watch-comp', () => {
gulp.watch(['src/**/*', 'src/common/**/*', 'plugins/**/*']).on('all', (type, file) => {
console.log(type + ':' + file)
packComp()
})
})
gulp.task('watch', gulp.parallel('watch-demo', 'watch-comp'))