UNPKG

@antmove/wx-alipay

Version:

transform wechat miniprogram to alipay miniprogram tool.

152 lines (134 loc) 4.38 kB
const path = require('path') const { transformStr } = require('@antmove/utils') const fs = require('fs-extra') const Config = require('../config') const appJson = require('../config/jsonInfo/globalconfig') const pageJson = require('../config/jsonInfo/pageconfig') const uiComponents = { '@vant/weapp': 'vant-aliapp/dist/dist', 'vant-weapp': 'vant-aliapp/dist/dist' } const keys = Object.keys const windowConfigMap = {} mkJsonMap(appJson.window.props, windowConfigMap) mkJsonMap(pageJson, windowConfigMap) function mkJsonMap(props, targetJson) { keys(props) .forEach((prop) => { const value = props[prop] if (value.type === 1) { targetJson[prop] = value.key } }) } /** * replace key of object */ function replaceTheKey(obj, configMap) { if (!obj) { return false } keys(obj) .forEach((key) => { const _key = configMap[key] if (_key) { obj[_key] = obj[key] delete obj[key] } }) return obj } function toAbsolutePath(str = '') { if (!str) { return false } str = str.replace(/^\.\//, '/') str = str.replace(/^\w/, (...$) => { return `/${$[0]}` }) return str } function isNpm(packageObj, filename) { if (filename[0] === '.' || filename[0] === '/' || !packageObj) { return false } const dependencies = packageObj.dependencies || {} let isNpmComponent = false keys(dependencies) .forEach(d => { const reg = new RegExp(`^${d}`) isNpmComponent = filename.match(reg) }) return isNpmComponent } module.exports = function(jsonStr, fileInfo) { if (!jsonStr) { return '' } const json = JSON.parse(jsonStr) replaceTheKey(json, windowConfigMap) // process wrap components const tagsInfo = fileInfo.tagsInfo const ctx = this const componentPages = this.$options.componentPages || {} // process custome components json.usingComponents = json.usingComponents || {} if (fileInfo.appUsingComponents) { keys(fileInfo.appUsingComponents) .forEach((c) => { if (!fileInfo.customAppUsingComponents) { return false } let cPath = fileInfo.customAppUsingComponents[c] if (!cPath) { return false } if (fileInfo.packageInfo && !isNpm(fileInfo.packageInfo, cPath)) { cPath = path.join(fileInfo.output, cPath) cPath = path.relative(path.join(fileInfo.dist, '../'), cPath) } /** * not support npm packages components */ cPath = toAbsolutePath(cPath) cPath = cPath.replace(/\\/g, '/') json.usingComponents[c] = cPath }) } keys(json.usingComponents) .forEach((key) => { const _key = transformStr(key) let _val = json.usingComponents[key] let rule = _val if (!isNpm(fileInfo.packageInfo, _val)) { if ((rule[0] !== '/' && rule[0] !== '.' )) { const tempPath = path.join(fileInfo.dirname, `${rule}.wxml`) if (fs.pathExistsSync(tempPath)) { rule = `./${rule}` } else { rule = `/${rule}` } } } else { keys(uiComponents) .forEach(u => { rule = rule.replace(u, uiComponents[u]) }) } _val = rule delete json.usingComponents[key] keys(componentPages) .forEach((p) => { if (_val[0] === '/' && path.join(ctx.$options.entry, p) === path.join(ctx.$options.entry, _val)) { const arr = _val.split('/') arr.splice(-1, 1, componentPages[p].componentName) _val = arr.join('/') } else if (path.join(fileInfo.path, `../${_val}`) === path.join(ctx.$options.entry, p)) { const arr = _val.split('/') arr.splice(-1, 1, componentPages[p].componentName) _val = arr.join('/') } }) json.usingComponents[_key] = _val }) if (tagsInfo) { tagsInfo.forEach((tagInfo) => { if (tagInfo.type === 5) { Config.compile.customComponent[tagInfo.tagName] = true // the __component directory will rename as component const componentPath = tagInfo.path.replace('__component', 'component') json.usingComponents = json.usingComponents || {} json.usingComponents[tagInfo.tagName] = componentPath } }) } return JSON.stringify(json) }