-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
215 lines (188 loc) · 6.98 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
const gulp = require('gulp');
const path = require('path');
const plugins = require('gulp-load-plugins')();
const del = require('del');
const through = require('through2');
const colors = require('ansi-colors');
const log = require('fancy-log');
const argv = require('minimist')(process.argv.slice(2));
const runSequence = require('run-sequence');
const pxtorpx = require('postcss-px2rpx');
const base64 = require('postcss-font-base64');
const combiner = require('stream-combiner2');
const isProd = argv.type === 'prod';
const exclude = {
dir: 'components',
type: ['wxss', 'json', 'scss']
};
const paths = {
src: {
baseDir: 'client',
imgSrc: 'client/images',
imgFiles: 'client/images/**',
excludeDir: `client/${exclude.dir}`
},
cloud: {
baseDir: 'server/cloud-functions',
destDir: 'dist/cloud-functions'
},
dist: {
baseDir: 'dist',
imgDest: 'dist/images',
excludeDir: `dist/${exclude.dir}`
}
};
const handleError = (err) => {
console.log('\n');
log(colors.red('Error: ' + err.name));
log('fileName: ' + colors.red(err.fileName));
log('lineNumber: ' + colors.red(err.lineNumber || JSON.stringify(err.loc)));
log('message: ' + err.message);
log('plugin: ' + colors.yellow(err.plugin));
};
gulp.task('wxml', () => {
return gulp
.src([`${paths.src.baseDir}/**/*.wxml`, `!${paths.src.excludeDir}/**/*.{${exclude.type.join(',')}}`])
// .pipe(isProd ? plugins.htmlmin({
// collapseWhitespace: true,
// removeComments: true,
// keepClosingSlash: true
// }) : through.obj())
.pipe(gulp.dest(paths.dist.baseDir));
});
// 关闭自动补全,启用微信小程序开发者工具自带补全功能
gulp.task('wxss', () => {
const combined = combiner.obj([
gulp.src([`${paths.src.baseDir}/**/*.{wxss,scss}`, `!${paths.src.excludeDir}/**/*.{${exclude.type.join(',')}}`]),
plugins.sass().on('error', plugins.sass.logError),
plugins.postcss([pxtorpx(), base64()]),
isProd ? plugins.cssnano({
autoprefixer: false,
discardComments: { removeAll: true }
}) : through.obj(),
plugins.rename((path) => (path.extname = '.wxss')),
gulp.dest(paths.dist.baseDir)
]);
combined.on('error', handleError);
return combined;
});
gulp.task('js', () => {
const combined = combiner.obj([
gulp.src([`${paths.src.baseDir}/**/*.js`, `!${paths.src.excludeDir}/**/*.{${exclude.type.join(',')}}`]),
isProd ? plugins.jdists({
trigger: 'prod'
}) : plugins.jdists({
trigger: 'dev'
}),
isProd ? through.obj() : plugins.sourcemaps.init(),
plugins.babel({
presets: ['@babel/env']
}),
isProd ? plugins.uglify({
compress: true
}) : through.obj(),
isProd ? through.obj() : plugins.sourcemaps.write('./'),
gulp.dest(paths.dist.baseDir)
]);
combined.on('error', handleError);
return combined;
});
gulp.task('json', () => {
return gulp.src([`${paths.src.baseDir}/**/*.json`, `!${paths.src.excludeDir}/**/*.{${exclude.type.join(',')}}`, `!${paths.src.baseDir}/project.config.json`])
.pipe(isProd ? plugins.jsonminify() : through.obj())
.pipe(gulp.dest(paths.dist.baseDir));
});
gulp.task('generateProjectConfigJson', () => {
return gulp.src('${paths.src.baseDir}/project.config.json')
.pipe(isProd ? plugins.jsonminify() : through.obj())
.pipe(gulp.dest(paths.dist.baseDir));
})
gulp.task('images', () => {
return gulp.src([`${paths.src.baseDir}/**/*.{jpg,jpeg,png,gif,svg}`, `!${paths.src.excludeDir}/**/*.{${exclude.type.join(',')}}`])
.pipe(plugins.newer(paths.dist.imgDest))
.pipe(plugins.imagemin({
progressive: true,
svgoPlugins: [{ removeViewBox: false }]
}))
.pipe(gulp.dest(paths.dist.baseDir));
});
gulp.task('wxs', () => {
return gulp.src([`${paths.src.baseDir}/**/*.wxs`, `!${paths.src.excludeDir}/**/*.{${exclude.type.join(',')}}`]).pipe(gulp.dest(paths.dist.baseDir));
});
gulp.task('extras', () => {
return gulp.src([
`${paths.src.baseDir}/**/*.*`,
`!${paths.src.baseDir}/**/*.{js,wxml,wxss,scss,wxs,json,jpg,jpeg,png,gif,svg}`,
`!${paths.src.excludeDir}/**/*.{${exclude.type.join(',')}}`
]).pipe(gulp.dest(paths.dist.baseDir));
});
gulp.task('excludes', () => {
return gulp.src(`${paths.src.excludeDir}/**/*.{${exclude.type.join(',')}}`).pipe(gulp.dest(paths.dist.excludeDir));
});
gulp.task('watch', () => {
const handleWatch = (filePath) => {
const extname = path.extname(filePath).slice(1);
if (['wxml', 'wxss', 'js', 'json', 'wxs'].includes(extname)) {
if (filePath.includes('project.config.json')) {
runSequence('generateProjectConfigJson');
} else {
runSequence(extname);
}
} else if (extname === 'scss') {
runSequence('wxss');
} else if (['jpg', 'jpeg', 'png', 'gif', 'svg'].includes(extname)) {
runSequence('images');
} else {
runSequence('extras');
}
};
plugins.watch(`${paths.src.baseDir}/**`)
.on('add', (file) => {
log(colors.green(file) + ' is added');
handleWatch(file);
}).on('change', (file) => {
log(colors.yellow(file) + ' is changed');
handleWatch(file);
}).on('unlink', (file) => {
log(colors.red(file) + ' is deleted');
path.extname(file) === '.scss'
? del([file.replace(paths.src.baseDir, paths.dist.baseDir).replace('.scss', '.wxss')])
: del([file.replace(paths.src.baseDir, paths.dist.baseDir)]);
});
});
// 云开发相关输出流
gulp.task('cloud', () => {
return gulp
.src(`${paths.cloud.baseDir}/**`)
.pipe(isProd ? plugins.jdists({
trigger: 'prod'
}) : plugins.jdists({
trigger: 'dev'
}))
.pipe(gulp.dest(paths.cloud.destDir));
});
gulp.task('watch:cloud', () => {
plugins.watch(`${paths.cloud.baseDir}/**`, () => {
runSequence('cloud');
}).on('add', (file) => {
log(colors.green(file) + ' is added');
}).on('change', (file) => {
log(colors.yellow(file) + ' is changed');
}).on('unlink', (file) => {
log(colors.red(file) + ' is deleted');
del([file.replace('server/', 'dist/')]);
});
});
// 提供package.json scripts调用
gulp.task('clean', () => {
return del([`${paths.dist.baseDir}/**`]);
});
gulp.task('dev', ['clean'], () => {
runSequence('json', 'generateProjectConfigJson', 'images', 'wxml', 'wxss', 'js', 'wxs', 'extras', 'excludes', 'cloud', 'watch');
});
gulp.task('build', ['clean'], () => {
runSequence('json', 'generateProjectConfigJson', 'images', 'wxml', 'wxss', 'js', 'wxs', 'extras', 'excludes', 'cloud');
});
gulp.task('cloud:dev', () => {
runSequence('cloud', 'watch:cloud');
});