forked from weimobGroup/WeConsole
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmp.ts
94 lines (91 loc) · 3.19 KB
/
mp.ts
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
import { compileFile } from './sass';
import { getFiles, readFile, writeFile } from './fs';
import { copyPromise } from './_copy';
import { renameSync } from 'fs';
import { toXhsML } from './translator/xhs';
import { toAliXml } from './translator/ali';
import { MpXmlFileSuffix } from './vars';
import { toSwanXML } from './translator/swan';
const cssFileSuffix = {
wx: 'wxss',
my: 'acss',
xhs: 'css',
qq: 'qss',
swan: 'css',
tt: 'ttss',
ks: 'css'
};
const xjsFileSuffix = {
wx: 'wxs',
my: 'sjs',
xhs: 'SJS',
qq: 'qs',
swan: 'sjs',
tt: 'sjs',
ks: 'sjs' // TODO: 待确认
};
export const compilerMpResource = (
src: string,
dist: string,
targetPlatform: 'wx' | 'my' | 'xhs' | 'qq' | 'swan' | 'tt' | 'ks'
) => {
return Promise.all([
copyPromise(`${src}/**/*.png`, dist),
copyPromise(`${src}/**/*.jpg`, dist),
copyPromise(`${src}/**/*.jpeg`, dist),
copyPromise(`${src}/**/*.gif`, dist),
copyPromise(`${src}/**/*.wxs`, dist),
copyPromise(`${src}/**/*.wxml`, dist),
copyPromise(`${src}/**/*.wxss`, dist),
copyPromise(`${src}/**/*.json`, dist),
...getFiles(src, true).reduce((sum: Array<Promise<void>>, fileName) => {
if (fileName.endsWith('.scss')) {
sum.push(
compileFile(
fileName,
dist + fileName.substring(src.length).replace('.scss', `.${cssFileSuffix[targetPlatform]}`)
)
);
}
return sum;
}, [])
]).then(() => {
if (targetPlatform === 'wx') {
return;
}
getFiles(dist, true).forEach((fileName) => {
if (fileName.endsWith('.wxss')) {
const newFileName = fileName.substring(0, fileName.length - 5) + `.${cssFileSuffix[targetPlatform]}`;
renameSync(fileName, newFileName);
return;
}
if (fileName.endsWith('.wxml')) {
const newFileName = fileName.substring(0, fileName.length - 5) + `.${MpXmlFileSuffix[targetPlatform]}`;
renameSync(fileName, newFileName);
const xml = readFile(newFileName);
if (targetPlatform === 'xhs') {
writeFile(newFileName, toXhsML(xml));
return;
}
if (targetPlatform === 'my') {
writeFile(newFileName, toAliXml(xml));
return;
}
if (targetPlatform === 'swan') {
writeFile(newFileName, toSwanXML(xml));
return;
}
return;
}
if (fileName.endsWith('.wxs')) {
const newFileName = fileName.substring(0, fileName.length - 4) + `.${xjsFileSuffix[targetPlatform]}`;
renameSync(fileName, newFileName);
if (targetPlatform === 'my') {
const content = readFile(newFileName);
writeFile(newFileName, content.replace('module.exports = ', 'export default'));
}
return;
}
});
});
};