|
| 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