fix: 可视化wcsc #16

This commit is contained in:
msojocs 2022-02-25 12:24:28 +08:00
parent f0d100c761
commit 6dcf602079
11 changed files with 445 additions and 216 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -2,7 +2,6 @@ const util = require('./util')
const path = require('path')
const fs = util.fs
// TODO:未找到触发操作,所以相关功能未验证
let wcsc
try {
wcsc = require('./src/wcsc')
@ -59,20 +58,8 @@ exports = async function (options) {
let wcscResult
try {
console.warn('wcsc options', options)
wcscResult = wcsc(options.cwd, options.files, options)
if(options.lazyload){
const t = wcscResult
wcscResult = {
common: t.comm,
pageWxss: {}
}
for(let key in t){
console.log(key)
if(key.endsWith('.wxss')){
wcscResult.pageWxss[key] = t[key]
}
}
}
wcscResult = await wcsc(options)
console.warn('wcsc ok')
} catch (errmsg) {
throw new Error(errmsg)
}

View File

@ -1,6 +1,8 @@
const { spawnSync } = require('child_process')
const { spawn, spawnSync } = require('child_process')
const fs = require('fs')
const path = require('path')
const os = require('os')
const { throws } = require('assert')
/**
* 获取 wxss 编译器路径
@ -57,35 +59,85 @@ function getAllFiles(rootPath, files) {
/**
* 编译 wxss js
*/
function wxssToJS(rootPath, files, options) {
async function wxssToJS(options) {
// 创建临时目录
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'wcsc_'))
// 判断是否replace,是写入replace,否则拷贝文件到临时目录
for(let file of options.files){
if (typeof options.replaceContent[file] === 'string') {
// 写入替换内容
fs.mkdirSync(path.dirname(path.resolve(tmp, file)), {recursive:true})
fs.writeFileSync(path.resolve(tmp, file), options.replaceContent[file])
}else{
// 复制原文件
fs.mkdirSync(path.dirname(path.resolve(tmp, file)), {recursive:true})
fs.copyFileSync(path.resolve(options.cwd, file), path.resolve(tmp, file))
}
}
// 使用临时目录执行wcc
options.cwd = tmp
let rootPath = options.cwd, files=options.files
// files = getAllFiles(rootPath, files)
const args = ['-db', '-pc', String(options.pageCount)].concat(files)
const wxssParserPath = getWXSSParsePath()
console.warn('wcsc args: ', args)
const wcsc = spawnSync(wxssParserPath, args, { cwd: rootPath })
if (wcsc.status === 0) {
let res = wcsc.stdout.toString();
res = res.split('=')
const funcList = {}
for (let i = 0, len = res.length; i < len && res[i + 1]; i += 2) {
funcList[res[i]] = res[i + 1].replace(
/((\\x[\da-f]{2}|\\u[\da-f]{4})){1,}/gi,
function ($0, $1, $2) {
return eval('"' + $0 + '"');
// console.warn('wcsc args: ', args)
// const wcsc = spawnSync(wxssParserPath, args, { cwd: rootPath })
return new Promise((resolve, reject)=>{
const wcsc = spawn(wxssParserPath, args, {
cwd: rootPath,
});
const spwanData = [],
errData = [];
wcsc.stdout.on("data", (e) => {
spwanData.push(e);
});
wcsc.stderr.on("data", (e) => {
errData.push(e);
});
wcsc.on("close", (code) => {
console.warn('close', new Date().getTime()/1000)
if (code === 0) {
let result = Buffer.concat(spwanData).toString();
if(options.lazyload){
result = result.split('=')
let funcList = {}
for (let i = 0, len = result.length; i < len && result[i + 1]; i += 2) {
funcList[result[i]] = result[i + 1]
.replace(
/[^\\]((\\x[\da-f]{2}|\\u[\da-f]{4})){1,}/gi,
function ($0, $1, $2) {
return eval('"' + $0 + '"');
}
)
.replace(/\\[\s\S]{1}/gi, function ($0, $1, $2) {
// console.log($0, $1)
const c = $0 === "\\n" ? "\n" : $0[1];
return c
})
}
).replace(/\\[\s\S]{1}/gi, function ($0, $1, $2) {
// console.log($0, $1)
const c = $0 === "\\n" ? "\n" : $0[1];
return c
})
}
return funcList
} else {
return new Error(`编译 .wxss 文件错误:${wcsc.stderr.toString()}`)
}
const t = funcList
funcList = {
common: t.comm,
pageWxss: {}
}
for(let key in t){
if(key.endsWith('.wxss')){
funcList.pageWxss[key] = t[key]
}
}
result = funcList
}
resolve(result)
} else {
throw new Error(`编译 .wxss 文件错误(${wcsc.status})${wcsc.stderr.toString()}`)
}
})
})
}
module.exports = wxssToJS

5
test/debug-wcsc Normal file
View File

@ -0,0 +1,5 @@
#!/bin/bash
root_dir=$(cd `dirname $0`/.. && pwd -P)
rm -rf ~/.config/wechat_devtools/WeappCache/requireCache
$root_dir/bin/wechat-devtools

View File

@ -1,14 +1,17 @@
#!/usr/bin/env node
function encode1(s) {
return encodeURI(s).replace(
/%(u[0-9A-F]{4})|(%[0-9A-F]{2})/gm,
function ($0, $1, $2) {
return ($1 && "\\" + $1.toLowerCase()) || decodeURI($2);
}
);
}
console.log(encode1('content:\\\\x22\\\\x22'))
console.log(encodeURI('\\\\x22\\\\x22'))
// console.log(JSON.parse())
let str1 = "window.screen.orientation \\x26\\x26 /^landscape/.tes";
str1 = str1
.replace(
/[^\\]((\\x[\da-f]{2}|\\u[\da-f]{4})){1,}/gi,
function ($0, $1, $2) {
console.log($0, eval('"' + $0 + '"'));
return eval('"' + $0 + '"');
}
)
.replace(/\\[\s\S]{1}/gi, function ($0, $1, $2) {
// console.log($0, $1)
const c = $0 === "\\n" ? "\n" : $0[1];
return c;
});
console.log(str1);

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,25 +0,0 @@
const wcsc = require('../../../package.nw/node_modules/wcsc')
const fs = require('fs')
const options = {
"files": [
"./pages/index/index.wxss",
"./pages/logs/logs.wxss",
"./app.wxss"],
"contents": [
"/**index.wxss**/\n.userinfo {\n display: flex;\n flex-direction: column;\n align-items: center;\n color: #aaa;\n}\n\n.userinfo-avatar {\n overflow: hidden;\n width: 128rpx;\n height: 128rpx;\n margin: 20rpx;\n border-radius: 50%;\n}\n\n.usermotto {\n margin-top: 200px;\n}", ".log-list {\n display: flex;\n flex-direction: column;\n padding: 40rpx;\n}\n.log-item {\n margin: 10rpx;\n}\n", "/**app.wxss**/\n.container {\n height: 100%;\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: space-between;\n padding: 200rpx 0;\n box-sizing: border-box;\n} \n"
],
"pageCount": 2,
"cwd": "/home/msojocs/Documents/we1",
"replaceContent": {
"./pages/index/index.wxss": "/**index.wxss**/\n.userinfo {\n display: flex;\n flex-direction: column;\n align-items: center;\n color: #aaa;\n}\n\n.userinfo-avatar {\n overflow: hidden;\n width: 128rpx;\n height: 128rpx;\n margin: 20rpx;\n border-radius: 50%;\n}\n\n.usermotto {\n margin-top: 200px;\n}"
},
"debug": true,
"classPrefix": "",
"lazyload": true
}
wcsc(options)
.then(res=>{
console.log(res)
fs.writeFileSync('test/wcsc/viewedit/1/node.json', JSON.stringify(res, null, 2))
})

View File

@ -0,0 +1,103 @@
const wcsc = require("../../../package.nw/node_modules/wcsc");
const fs = require("fs");
let options = {
files: ["./pages/index/index.wxss", "./pages/logs/logs.wxss", "./app.wxss"],
contents: [
"/**index.wxss**/\n.userinfo {\n display: flex;\n flex-direction: column;\n align-items: center;\n color: #aaa;\n}\n\n.userinfo-avatar {\n overflow: hidden;\n width: 128rpx;\n height: 128rpx;\n margin: 20rpx;\n border-radius: 50%;\n}\n\n.usermotto {\n margin-top: 200px;\n}",
".log-list {\n display: flex;\n flex-direction: column;\n padding: 40rpx;\n}\n.log-item {\n margin: 10rpx;\n}\n",
"/**app.wxss**/\n.container {\n height: 100%;\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: space-between;\n padding: 200rpx 0;\n box-sizing: border-box;\n} \n",
],
pageCount: 2,
cwd: "/home/msojocs/Documents/we1",
replaceContent: {
"./pages/index/index.wxss":
"/**index.wxss**/\n.userinfo {\n display: flex;\n flex-direction: column;\n align-items: center;\n color: #aaa;\n}\n\n.userinfo-avatar {\n overflow: hidden;\n width: 128rpx;\n height: 128rpx;\n margin: 20rpx;\n border-radius: 50%;\n}\n\n.usermotto {\n margin-top: 200px;\n}",
},
debug: true,
classPrefix: "",
lazyload: false,
};
options = {
pageCount: 54,
files: [
"./pages/login/login.wxss",
"./pages/index/index.wxss",
"./pages/articleView/articleView.wxss",
"./pages/calendar/calendar.wxss",
"./pages/card/card.wxss",
"./pages/checkIn/edit.wxss",
"./pages/checkIn/list.wxss",
"./pages/courseTable/courseTable.wxss",
"./pages/courseTable/imgCropper/imgCropper.wxss",
"./pages/exam/exam.wxss",
"./pages/grade/grade.wxss",
"./pages/laboratory/list.wxss",
"./pages/laboratory/detail.wxss",
"./pages/maintenance/maintenance.wxss",
"./pages/my/ADMag/ADMag.wxss",
"./pages/my/ADMag/watchAD/watchAD.wxss",
"./pages/my/dataMag/dataMag.wxss",
"./pages/my/my.wxss",
"./pages/my/sub/sub.wxss",
"./pages/my/sso/sso.wxss",
"./pages/newsList/newsList.wxss",
"./pages/officeGrade/query.wxss",
"./pages/THEOL/dir/dir.wxss",
"./pages/THEOL/THEOL.wxss",
"./pages/THEOL/tree/tree.wxss",
"./pages/auth/auth.wxss",
"./pages/my/bind/bind.wxss",
"./pages/my/register/register.wxss",
"./pages/my/bind/mini.wxss",
"./pages/terms/privacy.wxss",
"./pages/resources/list.wxss",
"./pages/my/about.wxss",
"./components/card.wxss",
"./components/beat-char/beat-char.wxss",
"./components/mytree/mytree.wxss",
"./components/scroll/index.wxss",
"./components/tab/index.wxss",
"./miniprogram_npm/weui-miniprogram/cell/cell.wxss",
"./miniprogram_npm/weui-miniprogram/cells/cells.wxss",
"./miniprogram_npm/weui-miniprogram/loading/loading.wxss",
"./node-modules/mp-html/dist/uni-app/components/mp-html/mp-html.wxss",
"./node-modules/mp-html/dist/uni-app/components/mp-html/node/node.wxss",
"./pages/my/info.wxss",
"./uni_modules/uni-card/components/uni-card/uni-card.wxss",
"./uni_modules/uni-icons/components/uni-icons/uni-icons.wxss",
"./uni_modules/uni-row/components/uni-col/uni-col.wxss",
"./uni_modules/uni-row/components/uni-row/uni-row.wxss",
"./uni_modules/uni-title/components/uni-title/uni-title.wxss",
"./wxcomponents/dynamicForm/index.wxss",
"./wxcomponents/dynamicForm/components/timePicker/timePicker.wxss",
"./wxcomponents/dynamicForm/vant/icon/index.wxss",
"./wxcomponents/dynamicForm/vant/info/index.wxss",
"./wxcomponents/dynamicForm/vant/uploader/index.wxss",
"./wxcomponents/image-cropper/image-cropper.wxss",
"./app.wxss",
"./common/main.wxss",
"./wxcomponents/dynamicForm/index-wxa-auto-dark.wxss",
"./wxcomponents/dynamicForm/components/timePicker/timePicker-wxa-auto-dark.wxss",
"./wxcomponents/dynamicForm/vant/common/index-wxa-auto-dark.wxss",
"./wxcomponents/dynamicForm/vant/common/index.wxss",
"./wxcomponents/dynamicForm/vant/icon/index-wxa-auto-dark.wxss",
"./wxcomponents/dynamicForm/vant/info/index-wxa-auto-dark.wxss",
"./wxcomponents/dynamicForm/vant/uploader/index-wxa-auto-dark.wxss",
],
cwd: "/mnt/disk1/Project/WeCuit-Project/WeCuit-Mini-uni-test_uni_vue-cli/dist/build/mp-weixin",
lazyload: true,
replaceContent: {
"./pages/login/login.wxss":
".login-page.data-v-53bada72{display:flex;flex-direction:column;justify-content:flex-end;height:100vh}.logo-area.data-v-53bada72{display:flex;justify-content:center;height:80%}.logo.data-v-53bada72{display:flex;align-items:center}.logo-item.data-v-53bada72{height:200rpx;width:200rpx;border-radius:calc(50%)}.login-area.data-v-53bada72{display:flex;justify-content:center;align-items:center;margin:50rpx}.loading.data-v-53bada72{margin-top:5rpx;margin-right:10rpx;transform-origin:center center;-webkit-animation:rotate-data-v-53bada72 1s linear infinite;animation:rotate-data-v-53bada72 1s linear infinite}@-webkit-keyframes rotate-data-v-53bada72{0%{transform:rotate(0)}50%{transform:rotate(180deg)}100%{transform:rotate(1turn)}}@keyframes rotate-data-v-53bada72{0%{transform:rotate(0)}50%{transform:rotate(180deg)}100%{transform:rotate(1turn)}}",
},
debug: true,
};
wcsc(options).then((res) => {
// console.log(res);
fs.writeFileSync(
"test/wcsc/viewedit/1/node.json",
JSON.stringify(res, null, 2)
);
});