UNPKG

5.15 kBJavaScriptView Raw
1/**
2 * 项目文件压缩
3 * @memberOf Watch
4 */
5module.exports = (option)=>{
6 const {fs,path,tip,api,pathInfo,getDirFilesPath,getFileInfo,getCompileFn,getDistPath} = {
7 fs:require('fs-extra'),
8 path:require('path'),
9 tip:require('./tip'),
10 api:require('../api'),
11 pathInfo:require('./getPathInfo'),
12 getDirFilesPath:require('./getDirFilesPath'), //获取目录文件数据
13 getFileInfo:require('./getFileInfo'), //获取指定文件的相关信息
14 getCompileFn:require('./getCompileFn'), //根据文件类型来获取编译方法
15 getDistPath:require('./getDistPath')
16 };
17
18 let src = option.src,
19 dist = option.dist,
20 isMobile = option.isMobile,
21 isBeautify = option.isBeautify,
22 tasks = [],
23 getCompressionFn = (type)=>{
24 let fn;
25 switch (type) {
26 //页面
27 case '.html':
28 fn = api.compileHtml;
29 break;
30
31 //样式
32 case '.css':
33 fn = api.compileCss;
34 break;
35
36 //小程序样式
37 case '.wxss':
38 fn = api.compileCss;
39 break;
40
41 //JS
42 case '.js':
43 fn = api.compileJs;
44 break;
45
46 //图片
47 case '.png':case '.jpg':case '.jpeg':case '.svg':case '.gif':
48 fn = api.compileImg;
49 break;
50
51 //其它
52 default:
53 fn = api.Copy;
54 break;
55 };
56 return fn;
57 };
58
59 //清空生产目录
60 tasks.push(()=>{
61 return new Promise((resolve,reject)=>{
62 //先尝试直接删除目录
63 fs.remove(dist,err => {
64 //如果目录删除失败即遍历目录内所有的文件删除即可
65 if(err){
66 let files = fs.readdirSync(dist);
67 files.forEach(item => {
68 let fileOrDirpath = path.join(dist,item);
69 try {
70 fs.removeSync(fileOrDirpath);
71 resolve({
72 status:'success',
73 msg:`清空 ${dist}`
74 });
75 } catch (error) {
76 reject({
77 status:'error',
78 msg:`清除 ${fileOrDirpath} 失败,请检查文件是否被其它程序占用`,
79 info:error
80 });
81 };
82 });
83 }else{
84 resolve({
85 status:'success',
86 msg:`清空 ${dist}`
87 });
88 };
89 });
90 });
91 });
92
93 //压缩项目文件
94 tasks.push(()=>{
95 return new Promise((resolve,reject)=>{
96 let taskList = [],
97 data = getDirFilesPath({
98 srcDir:src,
99 ignoreDir:[], //不排除任何目录
100 ignore_:false //不排除以"_"开始的文件
101 });
102 for(let i in data){
103 for(let ii in data[i]){
104 let fileInfo = getFileInfo(ii),
105 fileType = fileInfo.type,
106 fileName = fileInfo.name,
107 CompressionFn = getCompressionFn(fileType);
108
109 taskList.push(()=>{
110 return new CompressionFn({
111 src:ii,
112 dist:ii.replace(src,dist),
113 isMobile:isMobile,
114 isBeautify:isBeautify
115 });
116 });
117
118 };
119 };
120
121 //将编译任务异步执行
122 let f = async ()=>{
123 for(let i=0,len=taskList.length; i < len; i++){
124 let subTask = await taskList[i]();
125 if(subTask instanceof Array){
126 subTask.forEach((item,index)=>{
127 if(item.status === 'success'){
128 tip.success(item.msg);
129 };
130 });
131 };
132 if(subTask.status === 'success'){
133 tip.success(subTask.msg);
134 };
135 };
136 return {
137 status:'success',
138 msg:'目录文件压缩完成'
139 };
140 };
141
142 f().then(v => {
143 //isInitCompile = true;
144 resolve(v);
145 }).catch(e => {
146 tip.error(e.msg);
147 reject(e);
148 });
149 });
150 });
151
152
153 return tasks;
154}
\No newline at end of file