Skip to content

Commit 676aabb

Browse files
committed
feat(alita): 添加 componentPaths 配置项, 兜底处理未找到路径的组件
1 parent e5a8879 commit 676aabb

File tree

4 files changed

+75
-3
lines changed

4 files changed

+75
-3
lines changed

examples/HelloWorldRN/src/TestPath.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
import React, { Component } from "react";
3+
import { Button, ScrollView, StyleSheet, Text, View, TouchableWithoutFeedback, Dimensions } from "react-native";
4+
5+
6+
class TestPath extends Component {
7+
render() {
8+
return (<Text>Inner</Text>)
9+
}
10+
}
11+
12+
export default {
13+
comp: TestPath
14+
}

src/configure.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ interface IConfigObj {
2121
appid?: string
2222

2323
exclude?: any,
24-
include?: any
24+
include?: any,
25+
26+
componentPaths?: any,
2527
}
2628

2729
interface IConfigure {

src/extractWxCompFiles/copyPackageWxComponents.ts

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import configure from '../configure'
77

88
const compPathInfos = {}
99

10+
const realPackChunks = {}
11+
1012
export default function () {
1113

1214
const allKeys = Object.keys(packageInfos)
@@ -20,6 +22,8 @@ export default function () {
2022
}
2123

2224
const chunks = getRelativeChunks(getModuleInfos(), packageInfo.dirname)
25+
realPackChunks[packageInfo.aliasPackName] = chunks
26+
2327

2428
let wxCompPathRelative = ''
2529
if (packageInfo.wxComponents.path) {
@@ -88,6 +92,10 @@ export function getCompPath(chunk, packageName, element) {
8892
}
8993
}
9094

95+
export function getRealPackChunks(realPackName) {
96+
return realPackChunks[realPackName]
97+
}
98+
9199

92100
function getRelativeChunks(moduleInfos, dirname) {
93101
const chunks = new Set()

src/extractWxCompFiles/extractJSONFile.ts

+50-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {getModuleInfo, setJsonRelativeFiles} from '../util/cacheModuleInfos'
44
import {getLibPath, judgeLibPath} from "../util/util"
55
import configure from "../configure";
66

7-
import {getCompPath} from './copyPackageWxComponents'
7+
import {getCompPath, getRealPackChunks} from './copyPackageWxComponents'
88

99

1010

@@ -72,7 +72,14 @@ function getUsedCompPaths(resouce, chunk, jsonRelativeFiles) {
7272
info.JSXElements.forEach(element => {
7373

7474
if (!info.im[element]) {
75-
usedComps[element] = `./${element}`
75+
// 非import/required组件,有两种情况,1:本文件声明了此组件, 2:组件在其他文件,引入方式非法
76+
if (configure.configObj.componentPaths && configure.configObj.componentPaths[element]) {
77+
const globalPath = configure.configObj.componentPaths[element]
78+
usedComps[element] = getGlobalChunkPath(globalPath, chunk, resouce, jsonRelativeFiles)
79+
} else {
80+
// 这里假定所有都是 本文件声明了组件
81+
usedComps[element] = `./${element}`
82+
}
7683
return
7784
}
7885

@@ -215,5 +222,46 @@ function shortPath(ao, module) {
215222
.replace(extname, '')
216223
}
217224

225+
function getGlobalChunkPath(globalPath, chunk, resouce, jsonRelativeFiles) {
226+
227+
let subpageDir = ''
228+
if (chunk !== '_rn_') {
229+
subpageDir = chunk.replace('/_rn_', '')
230+
resouce = resouce
231+
.replace(configure.inputFullpath, configure.inputFullpath + path.sep + subpageDir)
232+
}
233+
234+
let absoluteGlobalPath = null
235+
if (judgeLibPath(globalPath)) {
236+
const libPath = getLibPath(globalPath)
237+
jsonRelativeFiles.add(libPath)
238+
239+
const chunks = getRealPackChunks(libPath)
240+
241+
if (chunks.size === 1 && chunks.has('_rn_')) {
242+
absoluteGlobalPath = path.resolve(configure.inputFullpath, 'npm', globalPath)
243+
} else {
244+
absoluteGlobalPath = path.resolve(configure.inputFullpath, subpageDir, 'npm', globalPath)
245+
}
246+
} else {
247+
248+
absoluteGlobalPath = path.resolve(configure.inputFullpath, '.' + globalPath)
249+
250+
jsonRelativeFiles.add(absoluteGlobalPath)
251+
252+
const chunks = getModuleInfo(absoluteGlobalPath).chunks
253+
254+
if (chunks.length === 1 && chunks[0] === '_rn_') {
255+
// do nothing
256+
} else {
257+
absoluteGlobalPath = absoluteGlobalPath.replace(configure.inputFullpath, configure.inputFullpath + path.sep + subpageDir)
258+
}
259+
}
260+
261+
262+
return shortPath(absoluteGlobalPath, resouce)
263+
264+
}
265+
218266

219267

0 commit comments

Comments
 (0)