-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
311 lines (289 loc) · 7.02 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
const gulp = require("gulp");
const $ = require("gulp-load-plugins")();
const del = require("del");
const path = require("path");
const autoprefixer = require("autoprefixer");
const pngquant = require("imagemin-pngquant");
const uglifyjs = require("uglify-es");
const px2rpx = require("postcss-px2rpx");
const composer = require("gulp-uglify/composer");
const minifyJs = composer(uglifyjs, console);
const jsonTransform = require("gulp-json-transform");
const tsProject = $.typescript.createProject("tsconfig.json");
const pkg = require("./package.json");
/* 文件路径 */
const distPath = "dist";
const wxmlFiles = "src/**/*.wxml";
const lessFiles = ["src/**/!(_)*.less"];
const imgFiles = "src/assets/images/*.{png,jpg,jpeg,gif,ico,svg}";
const jsonFiles = "src/**/*.json";
const tsFiles = ["src/**/*.ts"];
const copyPath = ["src/**/!(_)*.*", "!src/**/*.less", "!src/**/*.ts"];
const isProd = process.env.NODE_ENV === "production";
/****************************/
/*****************************
*** TASKS ***
****************************/
/****************************/
/**
* @description 清空非npm构建包dist目录文件
*/
gulp.task("clean", () => {
return del(["dist/**", "!dist/miniprogram_npm/**"]);
});
/**
* @description 复制不包含less和图片的文件
*/
gulp.task("copy", () => {
return gulp.src(copyPath).pipe(gulp.dest(distPath));
});
/**
* @description 复制不包含less和图片的文件(只改动有变动的文件)
*/
gulp.task("copyChange", () => {
return gulp
.src(copyPath)
.pipe($.changed(distPath))
.pipe(gulp.dest(distPath));
});
/**
* @description 压缩wxml,生产环境任务
* @options 去空,去注释,补全标签
*/
gulp.task("minify-wxml", () => {
const options = {
collapseWhitespace: true,
removeComments: true,
keepClosingSlash: true
};
return gulp
.src(wxmlFiles)
.pipe($.if(isProd, $.htmlmin(options)))
.pipe(gulp.dest(distPath));
});
/**
* @description 压缩json,生产环境任务
*/
gulp.task("minify-json", function () {
return gulp
.src(jsonFiles)
.pipe($.jsonminify2())
.pipe(gulp.dest(distPath));
});
/**
* @description 编译less,补全、压缩样式文件
*/
gulp.task("compile-less", () => {
const postcssOptions = [
px2rpx({
screenWidth: 750, // 设计稿屏幕, 默认750
wxappScreenWidth: 750, // 微信小程序屏幕, 默认750
remPrecision: 6 // 小数精度, 默认6
}),
autoprefixer({
overrideBrowserslist: ["ios >= 8", "android >= 4.1"]
})
];
return gulp
.src(lessFiles)
.pipe($.plumber())
.pipe($.if(!isProd, $.sourcemaps.init()))
.pipe($.less())
.pipe($.if(isProd, $.cssnano()))
.pipe($.postcss(postcssOptions))
.pipe($.if(!isProd, $.sourcemaps.write()))
.pipe(
$.rename({
extname: ".wxss"
})
)
.pipe(gulp.dest(distPath));
});
/**
* @description 压缩图片
*/
gulp.task("minify-image", () => {
const options = {
progressive: true,
svgoPlugins: [{
removeViewBox: false
}],
use: [pngquant()]
};
return gulp
.src(imgFiles)
.pipe($.imagemin(options))
.pipe(gulp.dest("src/assets/images/"));
});
/**
* @description 编译、压缩ts
*/
gulp.task("compile-ts", () => {
const options = {
compress: {
drop_console: true,
drop_debugger: true
}
};
return tsProject
.src()
.pipe($.plumber())
.pipe($.if(!isProd, $.sourcemaps.init()))
.pipe($.eslint())
.pipe($.eslint.format())
.pipe(tsProject())
// .on("error", logError)
.js.pipe($.if(isProd, minifyJs(options)))
.pipe($.if(!isProd, $.sourcemaps.write()))
.pipe(gulp.dest(distPath));
});
/**
* @description npm支持1,复制依赖的node_modules文件
*/
gulp.task("copyNodeModules", () => {
const nodeModulesCopyPath = Object.keys(pkg.dependencies).map(
d => "node_modules/" + d + "/**/*"
);
return gulp
.src(nodeModulesCopyPath, {
base: ".",
allowEmpty: true
})
.pipe(gulp.dest(distPath));
});
/**
* @description npm支持2,根据dependencies生成package.json
*/
gulp.task("generatePackageJson", () => {
return gulp
.src("./package.json")
.pipe(
jsonTransform(() => {
return {
dependencies: pkg.dependencies
};
})
)
.pipe(gulp.dest(distPath));
});
/**
* @description 通用编译
*/
gulp.task(
"compile",
gulp.series(
"clean",
gulp.parallel(
"copyNodeModules",
"generatePackageJson",
"compile-ts",
"compile-less",
"copy"
)
)
);
/**
* @description 编译、监听
*/
gulp.task(
"watch",
gulp.series("compile", () => {
gulp.watch(tsFiles, gulp.parallel("compile-ts"));
gulp.watch(lessFiles, gulp.parallel("compile-less"));
gulp.watch(copyPath, gulp.parallel("copy"));
$.watch("src/**", e => {
console.log(`[watch]:${e.path} has ${e.event}`);
});
})
);
/**
* @description 生产环境打包
*/
gulp.task(
"build",
gulp.series(
"compile",
gulp.parallel("minify-wxml", "minify-json", "minify-image")
)
);
/**
* @description CLI快建模板
* @example
* gulp create -p mypage 创建名称为mypage的page文件
* gulp create -t mytpl 创建名称为mytpl的template文件
* gulp create -c mycomponent 创建名称为mycomponent的component文件
* gulp create -s index -p mypage 创建名称为mypage的page文件
*/
const create = done => {
const yargs = require("yargs")
.example("gulp create -p mypage", "创建名为mypage的page文件")
.example("gulp create -c mycomponent", "创建名为mycomponent的component文件")
.example(
"gulp create -s index -p mypage",
"复制pages/index中的文件创建名称为mypage的页面"
)
.option({
s: {
alias: "src",
default: "",
describe: "copy的模板",
type: "string"
},
p: {
alias: "page",
describe: "页面名称",
conflicts: ["t", "c"],
type: "string"
},
c: {
alias: "component",
describe: "组件名称",
type: "string"
},
version: {
hidden: true
},
help: {
hidden: true
}
})
.fail(msg => {
done();
console.error("创建失败!!!");
console.error(msg);
console.error("请按照如下命令执行...");
yargs.parse(["--msg"]);
return;
})
.help("msg");
const argv = yargs.argv;
const source = argv.s;
const typeEnum = {
p: "pages",
c: "components"
};
let hasParams = false;
let name, type;
for (let key in typeEnum) {
hasParams = hasParams || !!argv[key];
if (argv[key]) {
name = argv[key];
type = typeEnum[key];
}
}
if (!hasParams) {
done();
yargs.parse(["--msg"]);
}
const root = path.join(__dirname, "template", type);
return gulp
.src(path.join(root, source, "*.*"))
.pipe(
rename({
dirname: name,
basename: name
})
)
.pipe(gulp.dest(path.join("src", type)));
};
gulp.task(create);