Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a3d71d

Browse files
author
msojocs
committed
fix: 可视化
1 parent cc00feb commit 0a3d71d

29 files changed

+19974
-10
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ jobs:
5454
sudo find -maxdepth 1 -not -name ${{ env.name }} -not -name . -exec mv {} ${{ env.name }} \;
5555
ls -l
5656
sudo cp -r "${{ env.name }}/package.nw/node_modules/nodegit" nodegit
57+
sudo cp -r "${{ env.name }}/compiler" compiler
5758
env:
5859
name: 'release-${{ github.ref_name }}'
5960

compiler/wcc_node/index.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const util = require('./util')
2+
const path = require('path')
3+
4+
let wcc
5+
try {
6+
console.warn('wcc load')
7+
wcc = require('./src/wcc')
8+
} catch (err) {
9+
console.error('wcc', err)
10+
// wcc = require('./build/Release/wcc.node')
11+
}
12+
13+
const fs = util.fs
14+
15+
exports = async function (options) {
16+
// console.warn('wcc init options:', options);
17+
if (!options) throw Error('options is required')
18+
19+
const lazyload = !!options.lazyloadConfig
20+
21+
options = Object.assign(
22+
{
23+
files: [], // FILES
24+
contents: [],
25+
replaceContent: {},
26+
verbose: false,
27+
debug: false, // -d
28+
debugWXS: false, // -ds
29+
showNewTree: false,
30+
isPlugin: false,
31+
addTestAttre: false,
32+
independent: false,
33+
genfuncname: '$gwx', // -gn
34+
isCut: false, // --split
35+
cwd: process.cwd,
36+
debug: false,
37+
lazyload, // -ll
38+
lazyloadConfig: '',
39+
},
40+
options,
41+
)
42+
43+
return new Promise(async (resolve, reject) => {
44+
let st = Date.now()
45+
46+
// 获取文件内容
47+
if (!options.contents.length) {
48+
const tasks = options.files.map((file) => {
49+
if (typeof options.replaceContent[file] === 'string') {
50+
return options.replaceContent[file]
51+
}
52+
return fs.readFile(path.resolve(options.cwd, file), 'utf8')
53+
})
54+
options.contents = await Promise.all(tasks) || []
55+
}
56+
// console.warn('wcc get files', Date.now() - st, options.contents)
57+
let result
58+
try {
59+
// console.warn('final options:', options);
60+
// TODO: fix
61+
result = wcc(options.cwd, options.files, {cut: options.isCut}, options)
62+
// console.warn('wcc result', result)
63+
} catch(errmsg) {
64+
reject(new Error(errmsg))
65+
return
66+
}
67+
68+
// console.log('wcc get compile', Date.now() - st)
69+
if (options.output) {
70+
const output = path.resolve(options.cwd, options.output)
71+
const dir = path.dirname(output)
72+
if (lazyload) {
73+
// lazyload 为 true时,wcc 返回值是个对象, 需要序列化一下
74+
result = JSON.stringify(result)
75+
}
76+
try {
77+
await fs.stat(dir)
78+
} catch (e) {
79+
await fs.mkdir(dir, {
80+
recursive: true,
81+
})
82+
}
83+
await fs.writeFile(output, result, 'utf8')
84+
}
85+
console.warn('wcc get output', Date.now() - st)
86+
resolve(result)
87+
})
88+
}
89+
90+
Object.defineProperty(exports, 'version', {
91+
get() {
92+
return wcc.version
93+
},
94+
})
95+
96+
module.exports = exports

compiler/wcc_node/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "miniprogram-wcc",
3+
"version": "0.0.1",
4+
"description": "WCC node C++ addon",
5+
"main": "index.js",
6+
"scripts": {
7+
"install": "node-gyp-build",
8+
"rebuild": "node-gyp rebuild",
9+
"build:dist": "node scripts/build",
10+
"build": "node-gyp build",
11+
"test": "node ./test/index",
12+
"format": "prettier *.js test/*.js scripts/*.js --write"
13+
},
14+
"author": "coverguo",
15+
"license": "MIT",
16+
"dependencies": {
17+
"node-gyp-build": "^4.2.1"
18+
},
19+
"devDependencies": {
20+
"eustia-module": "^1.21.2",
21+
"licia": "^1.21.2",
22+
"ncp": "^2.0.0",
23+
"node-gyp": "^7.0.0"
24+
}
25+
}

compiler/wcc_node/src/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const path = require('path')
2+
const vm = require('vm')
3+
const glob = require('glob')
4+
const unescapeJs = require('unescape-js')
5+
6+
const wcc = require('./wcc')
7+
8+
module.exports = {
9+
wxmlToJs(rootPath) {
10+
// wcc 编译器需要完整的 wxml 文件列表
11+
const files = glob.sync('**/*.wxml', {
12+
cwd: rootPath,
13+
nodir: true,
14+
dot: true,
15+
ignore: ['node_modules/**/*.wxml'],
16+
})
17+
const wxsFiles = glob.sync('**/*.wxs', {
18+
cwd: rootPath,
19+
nodir: true,
20+
dot: true,
21+
ignore: ['node_modules/**/*.wxs'],
22+
})
23+
const compileResult = wcc(rootPath, files.map(file => file.substring(0, file.length - 5)), wxsFiles)
24+
25+
return `
26+
${compileResult};
27+
return $gwx;
28+
`
29+
},
30+
}

compiler/wcc_node/src/wcc.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
const { spawnSync } = require('child_process')
2+
const fs = require('fs')
3+
const path = require('path')
4+
5+
/**
6+
* 获取 wxml 编译器路径
7+
*/
8+
let wxmlParserPath = ''
9+
function getWXMLParsePath() {
10+
if (wxmlParserPath) return wxmlParserPath
11+
12+
const fileName = process.platform === 'darwin' ? '../bin/mac/wcc' : process.platform === 'linux' ? '../bin/linux/wcc' : '../bin/windows/wcc.exe'
13+
wxmlParserPath = path.join(__dirname, fileName)
14+
15+
// 尝试修改权限
16+
try {
17+
fs.chmodSync(wxmlParserPath, 0o777)
18+
} catch (err) {
19+
// ignore
20+
}
21+
22+
return wxmlParserPath
23+
}
24+
25+
/**
26+
* 获取自定义组件编译参数
27+
*/
28+
function getComponentArgs(files) {
29+
let args = []
30+
let count = 0
31+
32+
files.forEach(file => {
33+
const fileJson = file.fileJson
34+
35+
if (fileJson.usingComponents) {
36+
args.push(file.pagePath)
37+
args.push(Object.keys(fileJson.usingComponents).length)
38+
args = args.concat(Object.keys(fileJson.usingComponents))
39+
count++
40+
}
41+
})
42+
args.unshift(count)
43+
44+
return args
45+
}
46+
47+
/**
48+
* 获取完整文件列表,包括自定义组件
49+
*/
50+
function getAllFiles(rootPath, files) {
51+
const ret = []
52+
const hasCheckMap = {}
53+
54+
for (let i = 0, len = files.length; i < len; i++) {
55+
const file = files[i]
56+
57+
let fileJson = {}
58+
const realPath = path.join(rootPath, file)
59+
if (hasCheckMap[realPath]) continue
60+
hasCheckMap[realPath] = true
61+
try {
62+
fileJson = require(`${realPath}.json`)
63+
} catch(err) {
64+
// ignore
65+
}
66+
67+
// 自定义组件
68+
if (fileJson.usingComponents) {
69+
Object.keys(fileJson.usingComponents).forEach(subFileKey => {
70+
const subFile = fileJson.usingComponents[subFileKey]
71+
72+
len++
73+
74+
let relativePath = path.relative(rootPath, path.join(path.dirname(realPath), subFile))
75+
relativePath = relativePath.replace(/\\/g, '/')
76+
files.push(relativePath)
77+
})
78+
}
79+
80+
ret.push({
81+
pagePath: `${file}.wxml`,
82+
jsonPath: `${file}.json`,
83+
fileJson,
84+
})
85+
}
86+
87+
return ret
88+
}
89+
90+
/**
91+
* 入口
92+
* 编译 wxml 到 js
93+
* files
94+
*
95+
* @param {*} rootPath
96+
* @param {*} files 文件列表,包含组件
97+
* @param {*} param2
98+
* @param {*} options 配置选项
99+
* @returns
100+
*/
101+
function wxmlToJS(rootPath, files, { cut } = {}, options={}) {
102+
const type = cut ? '-xc' : '-cc'
103+
// files = getAllFiles(rootPath, files)
104+
105+
// @TODO,如果遇到参数过长被操作系统干掉的情况,可以使用 --config-path FILE 配置,参数空格换成空行
106+
// const componentArgs = getComponentArgs(files), componentArgs.join(' ')
107+
const args = ['-d', '--split', options.wxmlCompileConfigSplit, type, options.wxmlCompileConfig]
108+
.concat(files)
109+
.concat(['-gn', '$gwx'])
110+
111+
// TODO:可用性检测
112+
// wxs调试
113+
if(options.debugWXS)args.unshift('-ds')
114+
// 懒加载
115+
if(options.lazyload)args=args.concat(['-ll', options.lazyloadConfig])
116+
117+
// wxmlParserPath 二进制可执行文件路径
118+
const wxmlParserPath = getWXMLParsePath()
119+
// console.warn('wcc args:', args)
120+
const wcc = spawnSync(wxmlParserPath, args, { cwd: rootPath })
121+
122+
if (wcc.status === 0) {
123+
return wcc.stdout.toString()
124+
} else {
125+
throw new Error(`编译 .wxml 文件错误:${wcc.stderr.toString()}`)
126+
}
127+
}
128+
129+
module.exports = wxmlToJS

0 commit comments

Comments
 (0)