-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(alita): 配合WatchModuleUpdatedPlugin 监听到的模块更新,更新对应的小程序组件文件
- Loading branch information
1 parent
3e55be2
commit 2a6f24c
Showing
5 changed files
with
432 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import configure from "../configure"; | ||
import * as path from "path"; | ||
|
||
|
||
export const handleChanged = (info, finalJSPath) => { | ||
const newWxOutFiles = {} | ||
|
||
const {outComp, isPageComp} = info.RFInfo | ||
|
||
|
||
let outCompCode = `Component(wx.__bridge.WxNormalComp())` | ||
|
||
let renderCode | ||
if (isPageComp) { | ||
const projectRelativePath = finalJSPath | ||
.replace(configure.outputFullpath + path.sep, '') | ||
.replace('.js', '') | ||
.replace(/\\/g, '/') // win 平台 | ||
|
||
const pageCompPath = configure.configObj.subDir.endsWith('/') ? configure.configObj.subDir + projectRelativePath : configure.configObj.subDir + '/' + projectRelativePath | ||
renderCode = `Component(wx.__bridge.WxNormalComp("${pageCompPath}"))` | ||
} else { | ||
renderCode = outCompCode | ||
} | ||
|
||
for(let i = 0; i < outComp.length; i++) { | ||
const name = outComp[i] | ||
|
||
const jspath = (name === 'render' ? finalJSPath : finalJSPath.replace('.js', `${name}.js`)) | ||
const jscode = (name === 'render' ? renderCode : outCompCode) | ||
|
||
newWxOutFiles[jspath] = jscode | ||
} | ||
|
||
return newWxOutFiles | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import * as path from "path"; | ||
|
||
import {getModuleInfo} from '../util/cacheModuleInfos' | ||
import {getLibPath, judgeLibPath} from "../util/util" | ||
import configure from "../configure"; | ||
import {compInfos} from "../util/getAndStorecompInfos"; | ||
|
||
|
||
|
||
export const handleChanged = (module, info, finalJSPath) => { | ||
|
||
const newWxOutFiles = {} | ||
const {json, outComp} = info.RFInfo | ||
|
||
const renderUsingComponents = getUsedCompPaths(module) | ||
|
||
for(let i = 0; i < outComp.length; i ++) { | ||
const name = outComp[i] | ||
if (name === 'render') { | ||
continue | ||
} else { | ||
renderUsingComponents[name] = path.basename(finalJSPath).replace('.js', `${name}`) | ||
} | ||
} | ||
|
||
|
||
const renderJSON = { | ||
...json, | ||
usingComponents: renderUsingComponents | ||
} | ||
|
||
let renderJSONStr = JSON.stringify(renderJSON, null, '\t') | ||
|
||
for(let i = 0; i < outComp.length; i ++) { | ||
const name = outComp[i] | ||
|
||
const comppath = (name === 'render' ? finalJSPath.replace('.js', `.json`) : finalJSPath.replace('.js', `${name}.json`)) | ||
newWxOutFiles[comppath] = renderJSONStr | ||
} | ||
|
||
return newWxOutFiles | ||
} | ||
|
||
|
||
|
||
function getUsedCompPaths(module) { | ||
|
||
const info = getModuleInfo(module) | ||
|
||
const usedComps = {} | ||
|
||
info.JSXElements.forEach(element => { | ||
|
||
if (!info.im[element]) { | ||
//TODO 配合移除 "单文件单组件" | ||
usedComps[element] = `./${element}` | ||
return | ||
} | ||
|
||
const source = info.im[element].source | ||
if (isRnBaseSkipEle(element, source)) { | ||
return | ||
} | ||
|
||
const elementKey = source === 'react-native' ? `WX${element}` : element | ||
usedComps[elementKey] = getFinalPath(element, source, module, info) | ||
}) | ||
|
||
return usedComps | ||
} | ||
|
||
function isRnBaseSkipEle(element, source) { | ||
|
||
if (source === 'react-native' && ( | ||
element === 'View' | ||
|| element === 'Text' | ||
|| element === 'TouchableWithoutFeedback' | ||
|| element === 'TouchableOpacity' | ||
|| element === 'TouchableHighlight' | ||
|| element === 'Image' | ||
)) { | ||
return true | ||
} | ||
|
||
if (source === '@areslabs/wx-animated' && ( | ||
element === 'AnimatedView' | ||
|| element === 'AnimatedImage' | ||
|| element === 'AnimatedText' | ||
)) { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
|
||
function getFinalPath(element, source, module, info) { | ||
if (source === 'react-native') { | ||
return compInfos[source][`WX${element}`] | ||
} | ||
|
||
// import {xx} from 'xx' | ||
if (judgeLibPath(source) && source === getLibPath(source) && compInfos[source][element]) { | ||
return compInfos[source][element] | ||
} | ||
|
||
const absolutePath = info.deps[source] //syncResolve(path.dirname(filepath), source) | ||
|
||
return deepSeekPath(element, absolutePath, module) | ||
} | ||
|
||
|
||
function deepSeekPath(element, absolutePath, module) { | ||
|
||
let info = getModuleInfo(absolutePath) | ||
let im = info.im | ||
|
||
while (im[element]) { | ||
const source = im[element].source | ||
absolutePath = info.deps[source] | ||
info = getModuleInfo(absolutePath) | ||
im = info.im | ||
} | ||
|
||
return shortPath(absolutePath, module) | ||
} | ||
|
||
function shortPath(ao, module) { | ||
const aoArr = ao.split(path.sep) | ||
const filepathArr = path.dirname(module).split(path.sep) | ||
|
||
var i = 0 | ||
while (filepathArr[i] === aoArr[i]) { | ||
i ++ | ||
} | ||
|
||
const backPath = filepathArr.slice(i).map(() => '..').join('/') | ||
const toPath = aoArr.slice(i).join('/') | ||
|
||
const relativePath = `${backPath || '.'}/${toPath}` | ||
|
||
const absolutePath = ao.replace(configure.inputFullpath, '') | ||
.replace('node_modules', 'npm') | ||
|
||
const shortPath = relativePath.length > absolutePath.length ? absolutePath : relativePath | ||
|
||
const extname = path.extname(shortPath) | ||
|
||
// remove ext | ||
return shortPath.replace(`.wx${extname}`, '') | ||
.replace(extname, '') | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import {getRootPathPrefix, miscNameToJSName, wxCompoutPath} from "../util/util" | ||
import * as nodepath from "path"; | ||
import * as t from "@babel/types"; | ||
import traverse from "@babel/traverse"; | ||
import {geneReactCode} from "../util/uast"; | ||
|
||
|
||
export const handleChanged = (info, finalJSPath) => { | ||
const newWxOutFiles = {} | ||
|
||
|
||
const { templates, outComp, childTemplates} = info.RFInfo; | ||
|
||
const newTemp = []; | ||
for (let i = 0; i < templates.length; i++) { | ||
newTemp.push(t.jsxText("\n ")); | ||
newTemp.push(templates[i]); | ||
newTemp.push(t.jsxText("\n ")); | ||
} | ||
newTemp.push(t.jsxText("\n ")); | ||
|
||
|
||
const ast = t.jsxElement( | ||
t.jsxOpeningElement( | ||
t.jsxIdentifier("InnerTmpOpeningElement"), | ||
[] | ||
), | ||
t.jsxClosingElement(t.jsxIdentifier("InnerTmpOpeningElement")), | ||
newTemp, | ||
false | ||
); | ||
|
||
const past = t.program([ | ||
t.expressionStatement(ast) | ||
]); | ||
|
||
traverse(past, { | ||
exit: path => { | ||
if (path.type === "JSXExpressionContainer" | ||
&& (path.node as t.JSXExpressionContainer).expression.type === "JSXEmptyExpression" | ||
) { | ||
path.remove(); | ||
return; | ||
} | ||
} | ||
}); | ||
|
||
|
||
let templateWxml = geneReactCode(ast); | ||
templateWxml = templateWxml.replace("<InnerTmpOpeningElement>", ""); | ||
templateWxml = templateWxml.replace("</InnerTmpOpeningElement>", ""); | ||
|
||
for (let i = 0; i < childTemplates.length; i++) { | ||
const name = childTemplates[i]; | ||
// 如果只使用一个child 小程序会报递归, 然后就不渲染了 | ||
const subT = ` | ||
<template name="${name}"> | ||
<block wx:if="{{t.l(d)}}">{{d}}</block> | ||
<template wx:elif="{{d.tempName}}" is="{{d.tempName}}" data="{{...d}}"/> | ||
<block wx:else> | ||
<block wx:for="{{d}}" wx:key="key"> | ||
<block wx:if="{{t.l(item)}}">{{item}}</block> | ||
<template wx:else is="{{item.tempName}}" data="{{...item}}"/> | ||
</block> | ||
</block> | ||
</template> | ||
`; | ||
|
||
templateWxml = subT + templateWxml; | ||
} | ||
|
||
|
||
const utilWxsPath = getRootPathPrefix(finalJSPath) + '/commonwxs.wxs' | ||
|
||
templateWxml = `<wxs src="${utilWxsPath}" module="t" /> | ||
${templateWxml} | ||
` | ||
|
||
newWxOutFiles[`${finalJSPath.replace(".js", "Template.wxml")}`] = templateWxml | ||
|
||
|
||
// gene all outComp | ||
geneAllOutComp(outComp, finalJSPath, newWxOutFiles); | ||
|
||
|
||
return newWxOutFiles | ||
} | ||
|
||
|
||
function geneAllOutComp(outComp, finalJSPath, newWxOutFiles) { | ||
const basename = nodepath.basename(finalJSPath); | ||
const temppath = basename.replace(".js", "Template.wxml"); | ||
|
||
for (let i = 0; i < outComp.length; i++) { | ||
const name = outComp[i]; | ||
|
||
const wxmlFilepath = (name === "render" | ||
? finalJSPath.replace(".js", ".wxml") | ||
: finalJSPath.replace(".js", `${name}.wxml`) | ||
); | ||
|
||
const wxmlAst = []; | ||
wxmlAst.push(t.jsxText(`<import src="./${temppath}"/>`)) | ||
wxmlAst.push(t.jsxText("\n")); | ||
wxmlAst.push(t.jsxText(`<template wx:if="{{(_r && _r.tempName)}}" is="{{_r.tempName}}" data="{{..._r}}"/>`)) | ||
let tmpWxmlAst = t.jsxElement( | ||
t.jsxOpeningElement( | ||
t.jsxIdentifier("InnerTmpOpeningElement"), | ||
[] | ||
), | ||
t.jsxClosingElement(t.jsxIdentifier("InnerTmpOpeningElement")), | ||
wxmlAst, | ||
false | ||
); | ||
|
||
let WXMLCode = geneReactCode(tmpWxmlAst); | ||
WXMLCode = WXMLCode.replace("<InnerTmpOpeningElement>", ""); | ||
WXMLCode = WXMLCode.replace("</InnerTmpOpeningElement>", ""); | ||
|
||
|
||
newWxOutFiles[wxmlFilepath] = WXMLCode | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {getModuleInfo} from '../util/cacheModuleInfos' | ||
import {getRootPathPrefix, miscNameToJSName} from "../util/util" | ||
|
||
|
||
export const handleChanged = (info, finalJSPath) => { | ||
const newWxOutFiles = {} | ||
|
||
const {isPageComp, outComp} = info.RFInfo | ||
|
||
for (let i = 0; i < outComp.length; i++) { | ||
const name = outComp[i]; | ||
|
||
const wxssFilepath = (name === "render" | ||
? finalJSPath.replace(".js", ".wxss") | ||
: finalJSPath.replace(".js", `${name}.wxss`) | ||
); | ||
|
||
const pageCommonPath = getRootPathPrefix(finalJSPath) + "/pageCommon.wxss" | ||
const compCommonPath = getRootPathPrefix(finalJSPath) + "/compCommon.wxss" | ||
|
||
let wxssCode = null | ||
if (name === 'render' && isPageComp) { | ||
wxssCode = `@import '${pageCommonPath}'; | ||
@import '${compCommonPath}';` | ||
} else { | ||
wxssCode = `@import '${compCommonPath}';` | ||
} | ||
|
||
|
||
newWxOutFiles[wxssFilepath] = wxssCode | ||
} | ||
|
||
|
||
|
||
return newWxOutFiles | ||
} |
Oops, something went wrong.