fix: wcc, wcsc node跟进结构

跟进1.05.2204180版本的wcc、wcsc编译器变更 #41
This commit is contained in:
msojocs 2022-04-22 21:15:24 +08:00
parent 05d5494835
commit 22b579ec5e
18 changed files with 1939 additions and 16 deletions

View File

@ -37,6 +37,7 @@ clean_cache() {
echo "清理缓存";
rm -rf "$APPDATA/WeappCache";
rm -rf "$APPDATA/WeappVendor";
rm -rf "$DATA_DIR/wechat_devtools"
}
# 检测是否有清除缓存的必要

View File

@ -0,0 +1,4 @@
module.exports = {
wcc: require('./wcc'),
wcsc: require('./wcsc')
}

View File

@ -0,0 +1,95 @@
const util = require('./util')
const path = require('path')
let wcc
function loadWccAddon() {
if (wcc) return
// wcc = require('../build/Release/wcc.node')
wcc = require('../src/wcc')
}
const fs = util.fs
exports = async function (options) {
loadWccAddon()
if (!options) throw Error('options is required')
const lazyload = !!options.lazyloadConfig
options = Object.assign(
{
files: [],
contents: [],
replaceContent: {},
verbose: false,
debug: false,
debugWXS: false,
showNewTree: false,
isPlugin: false,
addTestAttre: false,
independent: false,
genfuncname: '$gwx',
isCut: false,
cwd: process.cwd,
debug: false,
lazyload,
lazyloadConfig: '',
},
options,
)
return new Promise(async (resolve, reject) => {
let st = Date.now()
// 获取文件内容
if (!options.contents.length) {
const tasks = options.files.map((file) => {
if (typeof options.replaceContent[file] === 'string') {
return options.replaceContent[file]
}
return fs.readFile(path.resolve(options.cwd, file), 'utf8')
})
options.contents = await Promise.all(tasks) || []
}
// console.log('wcc get files', Date.now() - st, options.contents)
let result
try {
result = wcc(options)
} catch(errmsg) {
reject(new Error(errmsg))
return
}
console.log('wcc get compile', Date.now() - st)
if (options.output) {
const output = path.resolve(options.cwd, options.output)
const dir = path.dirname(output)
if (lazyload) {
// lazyload 为 true时wcc 返回值是个对象, 需要序列化一下
result = JSON.stringify(result)
}
try {
await fs.stat(dir)
} catch (e) {
await fs.mkdir(dir, {
recursive: true,
})
}
await fs.writeFile(output, result, 'utf8')
}
console.log('wcc get output', Date.now() - st)
resolve(result)
})
}
Object.defineProperty(exports, 'version', {
get() {
loadWccAddon()
return wcc.version
},
})
module.exports = exports

View File

@ -0,0 +1,93 @@
const util = require('./util')
const path = require('path')
const fs = util.fs
let wcsc
function loadWcscAddon() {
if (wcsc) return
// wcsc = require('../build/Release/wcsc.node')
wcsc = require('../src/wcsc')
}
function tranWcscResultToObject(resultStr) {
const resultArr = resultStr.split('=')
const result = {}
for (let i = 0, len = resultArr.length; i < len && resultArr[i + 1]; i += 2) {
result[resultArr[i]] = resultArr[i + 1]
}
return result
}
exports = async function (options) {
loadWcscAddon()
if (!options) throw Error('options is required')
// avoid undefined or null
if (typeof options.subPackage !== 'string') {
delete options.subPackage
}
if (typeof options.lazyload !== 'boolean') {
delete options.lazyload
}
options = Object.assign(
{
files: [],
contents: [],
pageCount: 0,
cwd: process.cwd,
replaceContent: {},
debug: false,
classPrefix: '',
lazyload: false,
},
options,
)
if (!options.contents.length) {
const tasks = options.files.map((file) => {
if (typeof options.replaceContent[file] === 'string') {
return options.replaceContent[file]
}
return fs.readFile(path.resolve(options.cwd, file), 'utf8')
})
options.contents = await Promise.all(tasks) || []
}
let wccResult
try {
wccResult = wcsc(options)
} catch (errmsg) {
throw new Error(errmsg)
}
const result = options.lazyload ? wccResult : tranWcscResultToObject(wccResult)
if (options.output) {
const output = path.resolve(options.cwd, options.output)
const dir = path.dirname(output)
try {
await fs.stat(dir)
} catch (e) {
await fs.mkdir(dir, {
recursive: true,
})
}
await fs.writeFile(output, JSON.stringify(result, null, 2), 'utf8')
}
return result
}
Object.defineProperty(exports, 'version', {
get() {
loadWcscAddon()
return wcsc.version
},
})
module.exports = exports

View File

@ -2,24 +2,15 @@
"name": "miniprogram-wcc",
"version": "0.0.1",
"description": "WCC node C++ addon",
"main": "index.js",
"main": "lib/index.js",
"scripts": {
"install": "node-gyp-build",
"rebuild": "node-gyp rebuild",
"build:dist": "node scripts/build",
"build": "node-gyp build",
"test": "node ./test/index",
"test": "node ./test/wcc/index && node ./test/wcsc/index",
"format": "prettier *.js test/*.js scripts/*.js --write"
},
"author": "coverguo",
"license": "MIT",
"dependencies": {
"node-gyp-build": "^4.2.1"
},
"devDependencies": {
"eustia-module": "^1.21.2",
"licia": "^1.21.2",
"ncp": "^2.0.0",
"node-gyp": "^7.0.0"
}
}

View File

@ -0,0 +1,25 @@
{
"name": "miniprogram-wcc",
"version": "0.0.1",
"description": "WCC node C++ addon",
"main": "index.js",
"scripts": {
"install": "node-gyp-build",
"rebuild": "node-gyp rebuild",
"build:dist": "node scripts/build",
"build": "node-gyp build",
"test": "node ./test/index",
"format": "prettier *.js test/*.js scripts/*.js --write"
},
"author": "coverguo",
"license": "MIT",
"dependencies": {
"node-gyp-build": "^4.2.1"
},
"devDependencies": {
"eustia-module": "^1.21.2",
"licia": "^1.21.2",
"ncp": "^2.0.0",
"node-gyp": "^7.0.0"
}
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,143 @@
const { spawn, spawnSync } = require('child_process')
const fs = require('fs')
const path = require('path')
const os = require('os')
const { throws } = require('assert')
/**
* 获取 wxss 编译器路径
*/
let wxssParserPath = ''
function getWXSSParsePath() {
if (wxssParserPath) return wxssParserPath
const fileName = process.platform === 'darwin' ? '../bin/mac/wcsc' : process.platform === 'linux' ? '../bin/linux/wcsc' : '../bin/windows/wcsc.exe'
wxssParserPath = path.join(__dirname, fileName)
// 尝试修改权限
try {
fs.chmodSync(wxssParserPath, 0o777)
} catch (err) {
// ignore
}
return wxssParserPath
}
/**
* 获取完整文件列表
*/
function getAllFiles(rootPath, files) {
const ret = []
let compWxssNum = 0
for (let i = 0, len = files.length; i < len; i++) {
const file = files[i]
let fileJson = null
try {
fileJson = require(path.join(rootPath, `${file}.json`))
} catch(err) {
// ignore
}
if (fileJson) {
// 组件 wxss
compWxssNum++
ret.unshift(`${file}.wxss`)
} else {
ret.push(`${file}.wxss`)
}
}
return {
list: ret,
compWxssNum,
}
}
/**
* 编译 wxss js
*/
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)
options.subPackage && (args.push("--subpackage"), args.push(options.subPackage.replace(/\/$/, "")));
const wxssParserPath = getWXSSParsePath()
// 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
})
}
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

File diff suppressed because it is too large Load Diff

View File

@ -22,12 +22,11 @@ cd $tmp_dir && npm install miniprogram-compiler
# 可视化编译
(cd "${package_dir}/node_modules/" \
&& rm -rf wcc wcsc \
&& mkdir -p "wcc/bin/linux" "wcsc/bin/linux" \
&& mkdir -p "wcc/bin/linux" \
&& cp -r "${tmp_dir}/node_modules/miniprogram-compiler/bin/linux/wcc" "wcc/bin/linux/wcc" \
&& cp -r "${tmp_dir}/node_modules/miniprogram-compiler/bin/linux/wcsc" "wcsc/bin/linux/wcsc" \
&& chmod 0755 "wcc/bin/linux/wcc" "wcsc/bin/linux/wcsc" \
&& cp -r "${srcdir}/compiler/wcc_node"/* "wcc" \
&& cp -r "${srcdir}/compiler/wcsc_node"/* "wcsc"
&& cp -r "${tmp_dir}/node_modules/miniprogram-compiler/bin/linux/wcsc" "wcc/bin/linux/wcsc" \
&& chmod 0755 "wcc/bin/linux/wcc" "wcc/bin/linux/wcsc" \
&& cp -r "${srcdir}/compiler/wcc_node"/* "wcc"
)
# 预览编译设置NO_WINE=true环境变量生效