Skip to content

Commit 44d735e

Browse files
committed
feat(alita): 根据chunk copy wxComponents目录,并提供getCompPath 方法
1 parent 402f81e commit 44d735e

File tree

4 files changed

+108
-24
lines changed

4 files changed

+108
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import * as path from 'path'
2+
import * as fse from 'fs-extra'
3+
import {packageInfos} from '../util/getAndStorecompInfos'
4+
import {getModuleInfos} from '../util/cacheModuleInfos'
5+
import configure from '../configure'
6+
7+
8+
const compPathInfos = {}
9+
10+
export default function () {
11+
12+
const allKeys = Object.keys(packageInfos)
13+
14+
for(let i = 0; i < allKeys.length; i ++ ) {
15+
const packageName = allKeys[i]
16+
const packageInfo = packageInfos[packageName]
17+
18+
if (!packageInfo.wxComponents) {
19+
continue
20+
}
21+
22+
const chunks = getRelativeChunks(getModuleInfos(), packageInfo.dirname)
23+
24+
let wxCompPathRelative = ''
25+
if (packageInfo.wxComponents.path) {
26+
// 需要copy 微信组件
27+
wxCompPathRelative = (packageInfo.wxComponents.path.startsWith('/') ? "." + packageInfo.wxComponents.path : packageInfo.wxComponents.path)
28+
29+
30+
chunks.forEach(chunk => {
31+
const chunkDic = chunk === '_rn_' ? '' : chunk.replace('/_rn_', '')
32+
const targetDic = path.resolve(configure.outputFullpath, chunkDic, 'npm', packageInfo.aliasPackName)
33+
34+
if (!fse.existsSync(targetDic)) {
35+
const sourcePath = path.resolve(packageInfo.dirname, wxCompPathRelative)
36+
const targetPath = path.resolve(configure.outputFullpath, chunkDic, 'npm', packageInfo.aliasPackName, wxCompPathRelative)
37+
38+
fse.copySync(sourcePath, targetPath)
39+
}
40+
})
41+
}
42+
43+
44+
const pathMap = {}
45+
packageInfo.wxComponents.components.forEach(comp => {
46+
const {name, path: compPath} = comp
47+
const relativePath = compPath.startsWith('/') ? `.${compPath}` : compPath
48+
pathMap[name] = path.posix.resolve('/npm', packageInfo.aliasPackName, wxCompPathRelative, relativePath)
49+
})
50+
51+
compPathInfos[packageName] = {
52+
chunks,
53+
pathMap,
54+
}
55+
}
56+
}
57+
58+
59+
export function getCompPath(chunk, packageName, element) {
60+
if (!compPathInfos[packageName]) {
61+
return
62+
}
63+
64+
const {chunks, pathMap} = compPathInfos[packageName]
65+
66+
if (pathMap[element]) {
67+
68+
if (chunks.length === 1 && chunks[0] === '_rn_') {
69+
return pathMap[element]
70+
} else {
71+
return `/${chunk.replace('/_rn_', '')}${pathMap[element]}`
72+
}
73+
}
74+
}
75+
76+
77+
function getRelativeChunks(moduleInfos, dirname) {
78+
const chunks = new Set()
79+
80+
const allModuleRes = Object.keys(moduleInfos)
81+
for(let i = 0; i < allModuleRes.length; i ++) {
82+
const res = allModuleRes[i]
83+
84+
if (res.includes(`${dirname}/`)) {
85+
moduleInfos[res].chunks.forEach(c => {
86+
chunks.add(c)
87+
})
88+
}
89+
}
90+
91+
return Array.from(chunks)
92+
}

src/packByWebpack/WatchModuleUpdatedPlugin.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {setModuleDepsAndChunks} from '../util/cacheModuleInfos'
22

33
import {handleChanged, handleDeleted} from '../extractWxCompFiles'
4+
import copyPackageWxComponents from '../extractWxCompFiles/copyPackageWxComponents'
45

56
/**
67
* 生成小程序组件 json文件
@@ -51,6 +52,8 @@ export default class WatchModuleUpdatedPlugin {
5152
// 设置module deps,chunks,生成小程序json文件会使用到
5253
setAllModuleDepsAndChunks(compilation)
5354

55+
copyPackageWxComponents()
56+
5457
hanldeModuleChanged(compilation, handleChanged, handleDeleted)
5558
}
5659
);

src/util/cacheModuleInfos.ts

+4
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,8 @@ export function updateModuleOutFiles(filepath, outFiles) {
7777
moduleInfos[filepath].outFiles = outFiles
7878
}
7979

80+
export function getModuleInfos() {
81+
return moduleInfos
82+
}
83+
8084

src/util/getAndStorecompInfos.ts

+9-24
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import configure from "../configure";
55
import {getLibPath} from './util'
66

77

8-
/*组件名和路径映射,方便后续生成小程序json文件*/
9-
export const compInfos: any = {}
8+
export const packageInfos: any = {}
109

1110
/*Text节点,一般来说只要官方的Text组件*/
1211
export const textComp = new Set(['Text'])
@@ -26,49 +25,36 @@ export function getLibCompInfos(idens, JSXElements, filepath, relativePath) {
2625
// 动画组件 AnimatedView 会退化为view
2726
if (packagePath === '@areslabs/wx-animated') return
2827

29-
if (!compInfos[packagePath]) {
28+
if (!packageInfos[packagePath]) {
3029
const pajPath = syncResolve(path.dirname(filepath), `${packagePath}/package.json`)
3130

3231
const json = fse.readJSONSync(pajPath)
3332

3433
if (!json.wxComponents) {
35-
compInfos[packagePath] = {}
34+
packageInfos[packagePath] = {}
3635
return
3736
}
3837

39-
40-
const components = json.wxComponents.components
41-
42-
const aliasPP = configure.resolve.alias[packagePath] || packagePath
43-
44-
let wxCompPathRelative = '.'
45-
if (json.wxComponents.path) {
46-
const wxCompPath = json.wxComponents.path
47-
wxCompPathRelative = wxCompPath.startsWith('/') ? "." + wxCompPath : wxCompPath
48-
const wxCompTargetPath = path.resolve(configure.outputFullpath, 'npm', aliasPP)
49-
if (!fse.existsSync(wxCompTargetPath)) {
50-
const sourcePath = path.resolve(path.dirname(pajPath), wxCompPathRelative)
51-
const targetPath = path.resolve(configure.outputFullpath, 'npm', aliasPP, wxCompPathRelative)
52-
fse.copySync(sourcePath, targetPath)
53-
}
38+
packageInfos[packagePath] = {
39+
dirname: path.dirname(pajPath),
40+
aliasPackName: configure.resolve.alias[packagePath] || packagePath,
41+
wxComponents: json.wxComponents
5442
}
5543

56-
const pathMap = {}
44+
const components = json.wxComponents.components
5745
for(let i = 0; i < components.length; i ++ ) {
5846

5947
const comp = components[i]
6048

6149
const {
6250
name,
63-
path: comPath,
6451
base = true,
6552
needOperateChildren,
6653
jsxProps,
6754
isText
6855
} = comp
6956

70-
const finalPath = comPath.startsWith('/') ? `.${comPath}` : comPath
71-
pathMap[name] = path.posix.resolve('/npm', aliasPP, wxCompPathRelative, finalPath)
57+
7258

7359
if (needOperateChildren === true) {
7460
extChildComp.add(name)
@@ -86,6 +72,5 @@ export function getLibCompInfos(idens, JSXElements, filepath, relativePath) {
8672
textComp.add(name)
8773
}
8874
}
89-
compInfos[packagePath] = pathMap
9075
}
9176
}

0 commit comments

Comments
 (0)