Skip to content

Commit

Permalink
feat(alita wx-react): 解偶 小程序js , reactjs
Browse files Browse the repository at this point in the history
  • Loading branch information
ykforerlang committed Nov 19, 2019
1 parent 464f5c4 commit 37ae032
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 63 deletions.
6 changes: 4 additions & 2 deletions packages/wx-react/src/WxNormalComp.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import geneUUID from "./geneUUID"
import {cleanPageComp} from './util'


export default function (CompMySelf, RNApp) {
export default function (compPath) {

const o = {
properties: {
Expand Down Expand Up @@ -48,7 +48,7 @@ export default function (CompMySelf, RNApp) {
}

// 可能是页面组件,需要加入相关生命周期
if (CompMySelf && RNApp) {
if (compPath) {
o.methods.onLoad = function (query) {
const paramStr = query.params
let paramsObj = {}
Expand All @@ -59,6 +59,8 @@ export default function (CompMySelf, RNApp) {
const uuid = geneUUID()
this.data.diuu = uuid

const CompMySelf = wx._pageCompMaps[compPath]

renderPage(
createElement(
CompMySelf,
Expand Down
76 changes: 32 additions & 44 deletions src/struc/handleEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default function (ast, filepath) {
}

const historyMap = {}
const pageCompPaths = []

const moduleMap = {}
const compImportMap = {}
Expand Down Expand Up @@ -134,8 +135,16 @@ export default function (ast, filepath) {
const projectRelativePath = moduleMap[name].replace('.comp', '') // 组件的.comp 后缀需要移除
compImportMap[name] = moduleMap[name]


const pageCompPath = global.execArgs.configObj.subDir.endsWith('/') ? global.execArgs.configObj.subDir + projectRelativePath : global.execArgs.configObj.subDir + '/' + projectRelativePath

historyMap[global.execArgs.packageName + key]
= global.execArgs.configObj.subDir.endsWith('/') ? global.execArgs.configObj.subDir + projectRelativePath : global.execArgs.configObj.subDir + '/' + projectRelativePath
= pageCompPath

pageCompPaths.push(t.objectProperty(
t.stringLiteral(pageCompPath),
t.identifier(name)
))

appJSON.pages.push(projectRelativePath)

Expand Down Expand Up @@ -229,48 +238,6 @@ export default function (ast, filepath) {
})

traverse(ast, {
enter: path => {
if (path.type === 'ImportDeclaration') {
const specifiers = path.node.specifiers
for (let i = 0; i < specifiers.length; i++) {
const ele = specifiers[i]
const name = ele.local.name

if (compImportMap[name]) {
path.remove()
break
}
}
return
}

if (path.type === 'CallExpression'
&& path.node.callee.name === 'require'
&& path.key === 'init'
) {
const id = path.parentPath.node.id
if (id.type === 'Identifier') {
if (compImportMap[id.name]) {
path.parentPath.parentPath.remove()
}
} else if (id.type === 'ObjectPattern'){

for(let i = 0 ; i < id.properties.length; i++) {
const ele = id.properties[i]

const name = ele.key.name
if (compImportMap[name]) {
path.parentPath.parentPath.remove()
break
}

}
}

return
}
},


exit: path => {
if (path.type === 'ExportDefaultDeclaration') {
Expand Down Expand Up @@ -325,6 +292,23 @@ export default function (ast, filepath) {
t.identifier(`wx._historyConfig = {...(wx._historyConfig || {}), ...${JSON.stringify(historyMap, null)}}`)
)
)

// be lazy

path.node.body.push(t.variableDeclaration(
'const',
[
t.variableDeclarator(
t.identifier('__pageCompPath'),
t.objectExpression(pageCompPaths)
)
]
))
path.node.body.push(
t.expressionStatement(
t.identifier(`wx._pageCompMaps = {...(wx._pageCompMaps || {}), ...__pageCompPath}`)
)
)
}
}
})
Expand All @@ -335,8 +319,12 @@ export default function (ast, filepath) {
JSON.stringify(appJSON, null, '\t')
)


const entryRequirePath = filepath.replace(global.execArgs.OUT_DIR + npath.sep, '')
.replace(/\\/g, '/')

const appJSPATH = npath.resolve(global.execArgs.OUT_DIR, 'app.js')
const appJSCode = `
const appJSCode = `require('./${entryRequirePath}')
wx._beta = ${global.execArgs.beta ? 'true' : 'false'}
App({})
`
Expand Down
27 changes: 10 additions & 17 deletions src/tran/geneJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,24 @@ const path = require('path')


export default function (info) {
const {filepath, outComp, entryFilePath} = info
const {filepath, outComp, entryFilePath, isPageComp} = info
const finalJSPath = miscNameToJSName(filepath)

const outCompCode = `
import {
WxNormalComp,
} from "${RNWXLIBMaps.react}"
const outCompCode = `import { WxNormalComp } from "${RNWXLIBMaps.react}"
Component(WxNormalComp())
`

const compFilename = path.basename(finalJSPath, '.js')

let entryRelativePath = ''
if (entryFilePath) {
entryRelativePath = getRelativePath(finalJSPath, entryFilePath)
}

const RNAppCode = (entryFilePath ? `import RNApp from "${entryRelativePath}"` : `const RNApp = {}`)
const projectRelativePath = finalJSPath
.replace(global.execArgs.OUT_DIR + path.sep, '')
.replace('.js', '')
.replace(/\\/g, '/') // win 平台

const pageCompPath = global.execArgs.configObj.subDir.endsWith('/') ? global.execArgs.configObj.subDir + projectRelativePath : global.execArgs.configObj.subDir + '/' + projectRelativePath

const renderCompCode = `import CompMySelf from "./${compFilename}.comp"
import { WxNormalComp } from "${RNWXLIBMaps.react}"
${RNAppCode}
const renderCompCode = `import { WxNormalComp } from "${RNWXLIBMaps.react}"
const pageCompPath = "${pageCompPath}"
Component(WxNormalComp(CompMySelf, RNApp))
Component(WxNormalComp(pageCompPath))
`

for(let i = 0; i < outComp.length; i++) {
Expand Down

0 comments on commit 37ae032

Please sign in to comment.