Skip to content

Commit d14fd2c

Browse files
committed
wcc,wcsc: lazyload for Linux
1 parent f25d9d7 commit d14fd2c

37 files changed

+19316
-24
lines changed

compiler/generatemd5.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ let exeList = []
99
const macExeList = [
1010
'wcc',
1111
'wcsc',
12-
'wcc.bin'
12+
'wcc.bin',
13+
'wcsc.bin'
1314
]
1415
const winExeList = [
1516
'wcc.exe',

compiler/nodejs/wcc

Lines changed: 254 additions & 0 deletions
Large diffs are not rendered by default.

compiler/nodejs/wcsc

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/usr/bin/env node
2+
const { spawn, spawnSync } = require("child_process");
3+
const path = require("path");
4+
const args = process.argv.slice(2);
5+
const wcscPath = path.resolve(__dirname, "./wcsc.bin")
6+
function encode1(s) {
7+
return encodeURI(s).replace(
8+
/%(u[0-9A-F]{4})|(%[0-9A-F]{2})/gm,
9+
function ($0, $1, $2) {
10+
return ($1 && "\\" + $1.toLowerCase()) || decodeURI($2);
11+
}
12+
);
13+
}
14+
if (args.includes("-ll")) {
15+
const wcsc = spawn(wcscPath, args, {
16+
cwd: process.cwd(),
17+
});
18+
const spwanData = [],
19+
errData = [];
20+
wcsc.stdout.on("data", (e) => {
21+
spwanData.push(e);
22+
});
23+
wcsc.stderr.on("data", (e) => {
24+
errData.push(e);
25+
});
26+
wcsc.on("close", (n) => {
27+
// console.log('close', new Date().getTime()/1000)
28+
if (0 === n) {
29+
const str = Buffer.concat(spwanData).toString().replace(/\\\\/g, '\\\\u005c');
30+
const resultSplit = encode1(str).split("=");
31+
const tempObj = {};
32+
for (
33+
let i = 0, total = resultSplit.length;
34+
i < total && resultSplit[i + 1];
35+
i += 2
36+
) {
37+
// a=b ---> a: b
38+
const key = resultSplit[i];
39+
if (key === "version") continue;
40+
tempObj[key] = resultSplit[i + 1].replace(
41+
/((\\x[\da-f]{2}|\\u[\da-f]{4})){1,}/gi,
42+
function ($0, $1, $2) {
43+
return eval('"' + $0 + '"');
44+
// return dict[$0] ? dict[$0] : eval('"' + $0 + '"')
45+
}
46+
);
47+
}
48+
const resultObj = {
49+
common: tempObj.comm, //.replace(/\\n/g, '\\u000a'),
50+
pageWxss: {},
51+
};
52+
// console.log(resultObj.common)
53+
// console.log('for key', new Date().getTime()/1000)
54+
for (const key in tempObj) {
55+
if (key.endsWith(".wxss")) {
56+
resultObj.pageWxss[key] = tempObj[key];
57+
}
58+
}
59+
let result = JSON.stringify(resultObj);
60+
String.prototype.splice = function (start, newStr) {
61+
return this.slice(0, start) + newStr + this.slice(start + 1);
62+
};
63+
result = result.replace(/\\\\/g, "\\");
64+
// console.log('main replace', new Date().getTime()/1000)
65+
// for (let i = 0; i < result.length; i++) {
66+
// if (result[i] === "\\") {
67+
// // && result[i + 1] !== 'u'
68+
// const c = result[i + 1] === "n" ? "\n" : result[i + 1];
69+
// result = result.splice(
70+
// i + 1,
71+
// "u" + c.charCodeAt(0).toString(16).padStart(4, "0")
72+
// );
73+
// }
74+
// }
75+
result = result.replace(/\\[\s\S]{1}/gi, function ($0, $1, $2) {
76+
// console.log($0, $1)
77+
const c = $0 === "\\n" ? "\n" : $0[1];
78+
return "\\u" + c.charCodeAt(0).toString(16).padStart(4, "0")
79+
})
80+
// console.log('main replace end', new Date().getTime()/1000)
81+
// result = result.replace(/u005c"/g, 'u005cx22')
82+
// console.log('done', new Date().getTime()/1000)
83+
process.stdout.write(result);
84+
}
85+
});
86+
87+
// const wcsc = spawnSync(path.resolve(__dirname, "../wcsc"), args, {
88+
// cwd: process.cwd(),
89+
// });
90+
// if (wcsc.status !== 0) {
91+
// console.error(wcsc.error);
92+
// process.exit(wcsc.status);
93+
// }
94+
// const str = wcsc.stdout.toString();
95+
// // console.log(str)
96+
// const resultSplit = encode1(str).split("=");
97+
// const tempObj = {};
98+
// for (
99+
// let i = 0, total = resultSplit.length;
100+
// i < total && resultSplit[i + 1];
101+
// i += 2
102+
// ) {
103+
// // a=b ---> a: b
104+
// const key = resultSplit[i];
105+
// if (key === "version") continue;
106+
// tempObj[key] = resultSplit[i + 1].replace(
107+
// /((\\x[\da-f]{2}|\\u[\da-f]{4})){1,}/gi,
108+
// function ($0, $1, $2) {
109+
// return eval('"' + $0 + '"');
110+
// // return dict[$0] ? dict[$0] : eval('"' + $0 + '"')
111+
// }
112+
// );
113+
// }
114+
// const resultObj = {
115+
// common: tempObj.comm, //.replace(/\\n/g, '\\u000a'),
116+
// pageWxss: {},
117+
// };
118+
// // console.log(resultObj.common)
119+
// for (const key in tempObj) {
120+
// if (key.endsWith(".wxss")) {
121+
// resultObj.pageWxss[key] = tempObj[key];
122+
// }
123+
// }
124+
// let result = JSON.stringify(resultObj);
125+
// String.prototype.splice = function (start, newStr) {
126+
// return this.slice(0, start) + newStr + this.slice(start + 1);
127+
// };
128+
// result = result.replace(/\\\\/g, "\\");
129+
// for (let i = 0; i < result.length; i++) {
130+
// if (result[i] === "\\") {
131+
// // && result[i + 1] !== 'u'
132+
// const c = result[i + 1] === "n" ? "\n" : result[i + 1];
133+
// result = result.splice(
134+
// i + 1,
135+
// "u" + c.charCodeAt(0).toString(16).padStart(4, "0")
136+
// );
137+
// }
138+
// }
139+
// process.stdout.write(result);
140+
} else {
141+
spawn(wcscPath, args, {
142+
cwd: process.cwd(),
143+
stdio: "inherit",
144+
});
145+
}

docs/WCC与WCSC.MD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,9 @@ Usage: ./bin/linux/wcsc [-lc] [-o OUTPUT] [-s <NAME OF FILE>] [-st] [-js] [-db]
6868
-cp: add class prefix
6969
-pc: page wxss files count
7070
```
71+
72+
## 按需注入和用时注入
73+
https://developers.weixin.qq.com/miniprogram/dev/framework/ability/lazyload.html#%E6%8C%89%E9%9C%80%E6%B3%A8%E5%85%A5
74+
7175
# 输出
7276

test/test-node

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
#!/usr/bin/env node
22

3-
const sl = require("spdlog")
4-
console.log(sl)
5-
// sl.setAsyncMode(5000, 1000);
3+
function encode1(s) {
4+
return encodeURI(s).replace(
5+
/%(u[0-9A-F]{4})|(%[0-9A-F]{2})/gm,
6+
function ($0, $1, $2) {
7+
return ($1 && "\\" + $1.toLowerCase()) || decodeURI($2);
8+
}
9+
);
10+
}
11+
12+
console.log(encode1('content:\\\\x22\\\\x22'))
13+
console.log(encodeURI('\\\\x22\\\\x22'))
14+
// console.log(JSON.parse())

0 commit comments

Comments
 (0)