Skip to content

Commit

Permalink
feat(alita): 简化配置项,Deprecated dependenciesMap extCompLibs。 启用dependen…
Browse files Browse the repository at this point in the history
…cies配置项将其统一
  • Loading branch information
ykforerlang committed Oct 28, 2019
1 parent c475e74 commit a30baf6
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/basetran/handleModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ function getImagePath(filepath, source) {
* @param rnName
*/
function getWxNpmPackageName(rnName) {
const dm = global.execArgs.configObj.dependenciesMap
// deprecated
const dm = global.execArgs.dependenciesMap || global.execArgs.configObj.dependenciesMap

for(let key in dm) {
if (dm[key] === false) continue
Expand Down
83 changes: 73 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import path from 'path'
import getopts from 'getopts'
import colors from 'colors'
import fse, { exists } from 'fs-extra'
import { getDependenciesMap, getRNCompList, emptyDir } from './util/util'
import { getDependenciesMap, getRNCompList, emptyDir, buildDefaultDependencies } from './util/util'
import packagz from '../package.json'
import filewatch from './filewatch/index'
import geneWXFileStruc from './util/geneWXFileStruc'
import getExtCompPathMaps from './util/getExtCompPathMaps'
import analyzingDependencies from './util/analyzingDependencies'
import geneWXPackageJSON from './util/geneWXPackageJSON'
import initProject from './util/initProject'
import program from 'commander'
Expand Down Expand Up @@ -104,16 +105,21 @@ const DEFAULTCONFIG = {
isFileIgnore: () => false,
subDir: '/',
packageJSONPath: './package.json',

/**
* deprecated
*/
dependenciesMap: getDependenciesMap('^1.0.0'),
extCompLibs: [
{
name: 'react-native',
compDir: 'component',
compLists: getRNCompList()
}
]
],
}


function enterI() {
if (process.argv.indexOf('-i') !== -1) {
return true
Expand Down Expand Up @@ -176,9 +182,18 @@ let configObj = DEFAULTCONFIG
if (fse.existsSync(CONFIGPATH)) {
console.log('配置文件路径:'.info, CONFIGPATH)
const userConfig = require(CONFIGPATH)

if (userConfig.dependenciesMap || userConfig.extCompLibs) {
console.log('\ndependenciesMap, extCompLibs配置项已经弃用!!!请使用dependencies替代,详情请参考文档。 \n'.warn)
}

configObj = {
...DEFAULTCONFIG,
...userConfig,

/**
* deprecated
*/
dependenciesMap: {
...DEFAULTCONFIG.dependenciesMap,
...(userConfig.dependenciesMap || {})
Expand All @@ -188,6 +203,14 @@ if (fse.existsSync(CONFIGPATH)) {
...(userConfig.extCompLibs || [])
]
}

if (userConfig.dependencies) {
configObj.dependencies = [
...(buildDefaultDependencies('^1.0.0')),
...userConfig.dependencies
]
}

main()
} else {
console.log('未发现配置文件,将使用默认配置'.info)
Expand All @@ -206,16 +229,56 @@ function main() {
outdir: options.outdir
}

global.execArgs = {
...global.execArgs,
...getExtCompPathMaps(configObj.extCompLibs, configObj.dependenciesMap)
}
if (configObj.dependencies) {
const {
dependenciesMap,
extCompPathMaps,
extChildComp,
extReactComp,
allBaseComp,
textComp,
jsxPropsMap
} = analyzingDependencies(configObj.dependencies)

// 生成微信目录结构
geneWXFileStruc(OUT_DIR, options.component)
// rn npm和wx npm包名映射
global.execArgs.dependenciesMap = dependenciesMap

// 生成微信package.json文件
geneWXPackageJSON(options.wxName, configObj.dependenciesMap)
// wx 组件名和路径的映射,方便生成小程序自定义组件的json文件
global.execArgs.extCompPathMaps = extCompPathMaps

// 需要操作children的RN组件,在转化的时候需要特殊处理,参考ScrollTabView
global.execArgs.extChildComp = extChildComp

// 所有配置在compLists,继承自非RNBaseComponent的组件,包括FlatList等
global.execArgs.extReactComp = extReactComp

// 所以继承RNBaseComponent的组件
global.execArgs.allBaseComp = allBaseComp

// 直接包裹文字的组件,需要特殊处理
global.execArgs.textComp = textComp

// 属性传递JSX的情况,需要特殊处理,参考FlatList的renderItem属性
global.execArgs.jsxPropsMap = jsxPropsMap

// 生成微信目录结构
geneWXFileStruc(OUT_DIR, options.component)

// 生成微信package.json文件
geneWXPackageJSON(options.wxName, dependenciesMap)
} else {
// deprecated
global.execArgs = {
...global.execArgs,
...getExtCompPathMaps(configObj.extCompLibs, configObj.dependenciesMap)
}

// 生成微信目录结构
geneWXFileStruc(OUT_DIR, options.component)

// 生成微信package.json文件
geneWXPackageJSON(options.wxName, configObj.dependenciesMap)
}

const ignored = /node_modules|\.git|\.expo|android|ios|\.idea|__tests__|.ios\.js|.android\.js|\.web\.js|\.sh|\.iml|\.vs_code|alita\.config\.js|babel\.config\.js|metro\.config\.js|\.gitignore|app\.json|package\.json|package-lock\.json|\.eslintrc\.js|\.eslintrc\.json|\.eslintrc|yarn\.lock|\.test\.js|.watchmanconfig/
filewatch(ignored)
Expand Down
97 changes: 97 additions & 0 deletions src/util/analyzingDependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* Copyright (c) Areslabs.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

/**
* 这是对alita配置文件extCompLibs字段的二次处理,生成如下字段,方便在Alita处理过程中使用。
* 关于alita配置文件,详见:https://areslabs.github.io/alita/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6.html
*
* extCompPathMaps:组件名和路径映射,方便后续生成小程序json文件
* jsxPropsMap: 属性是JSX片段,包括方法返回JSX片段等,Alita需要特殊处理这些属性
* extChildComp: 需要处理children 为childrencpt的集合
* extReactComp:配置在compLists,且不继承自RNBaseComponent
* textComp: Text节点,一般来说只要官方的Text组件
* allBaseComp:所有基本组件,包括RN官方组件,基本组件和自定义组件 Alita在转化的时候有一些区别
*
* //TODO 是否需要考虑 不同包下的组件同名??
* @returns {{extCompPathMaps, extChildComp: Set, extReactComp: Set, allBaseComp: Set, jsxPropsMap}}
*/
export default function analyzingDependencies(dependencies) {
const dependenciesMap = {}
const extCompPathMaps = {}
const jsxPropsMap = {}
const extChildComp = new Set([])
const extReactComp = new Set([])
const textComp = new Set(['Text'])
const allBaseComp = new Set([])
for(let i = 0; i < dependencies.length; i ++) {
const extLib = dependencies[i]
const libName = extLib.name
let wxLibName = extLib.wxName
let wxVersion = extLib.wxVersion

if (wxLibName === undefined) {
console.log(`包${libName} 未配置wxName,字段!!!将使用${libName}作为其微信小程序包名`.warn)
wxLibName = libName
}

if (wxVersion) {
dependenciesMap[libName] = [wxLibName, wxVersion]
} else {
dependenciesMap[libName] = wxLibName
}


if (!extLib.compLists) {
continue
}

const compPathMap = {}
for(let j = 0; j < extLib.compLists.length; j ++) {
const comps = extLib.compLists[j]
const {
name,
path,
base,
needOperateChildren,
jsxProps,
isText
} = comps

const finalPath = path.startsWith('/') ? path : `/${path}`
compPathMap[name] = `${wxLibName}${finalPath}`

if (needOperateChildren === true) {
extChildComp.add(name)
}

if (base === true) {
allBaseComp.add(name)
} else {
extReactComp.add(name)
}

if (jsxProps) {
jsxPropsMap[name] = jsxProps
}

if (isText === true) {
textComp.add(name)
}
}
extCompPathMaps[libName] = compPathMap
}
return {
dependenciesMap,
extCompPathMaps,
extChildComp,
extReactComp,
allBaseComp,
textComp,
jsxPropsMap
}
}
13 changes: 7 additions & 6 deletions src/util/getExtCompPathMaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
* extChildComp: 需要处理children 为childrencpt的集合
* extReactComp:一般来说对齐的组件 需要继承于 RNBaseComponent,但是有些复杂的组件需要继承于Component/PureComponent,比如FlatList
* textComp: Text节点,一般来说只要官方的Text组件
* allExtComp:所有组件,包括RN官方组件,配置在extCompLibs里的组件,这些组件在Alita转化的时候,会当做基本组件,基本组件的属性处理和自定义组件有一些区别
* allBaseComp:所有基本组件,包括RN官方组件,基本组件和自定义组件 Alita在转化的时候有一些区别
*
* @returns {{extCompPathMaps, extChildComp: Set, extReactComp: Set, allExtComp: Set, jsxPropsMap}}
* @returns {{extCompPathMaps, extChildComp: Set, extReactComp: Set, allBaseComp: Set, jsxPropsMap}}
*/
export default function getExtCompPathMaps(extCompLibs, dm) {
const extCompPathMaps = {}
const jsxPropsMap = {}
const extChildComp = new Set([])
const extReactComp = new Set([])
const textComp = new Set(['Text'])
const allExtComp = new Set([])
const allBaseComp = new Set([])
for(let i = 0; i < extCompLibs.length; i ++) {
const extLib = extCompLibs[i]
const libName = extLib.name
Expand All @@ -44,7 +44,7 @@ export default function getExtCompPathMaps(extCompLibs, dm) {
const compName = extLib.compLists[j]
if (typeof compName === 'string') {
compPathMap[compName] = `${wxLibName}${compDir}${compName}/index`
allExtComp.add(compName)
allBaseComp.add(compName)
} else {
const {
name,
Expand All @@ -61,6 +61,8 @@ export default function getExtCompPathMaps(extCompLibs, dm) {

if (extendsComponent === true) {
extReactComp.add(name)
} else {
allBaseComp.add(name)
}

if (jsxProps) {
Expand All @@ -71,7 +73,6 @@ export default function getExtCompPathMaps(extCompLibs, dm) {
textComp.add(name)
}

allExtComp.add(name)
}

}
Expand All @@ -81,7 +82,7 @@ export default function getExtCompPathMaps(extCompLibs, dm) {
extCompPathMaps,
extChildComp,
extReactComp,
allExtComp,
allBaseComp,
textComp,
jsxPropsMap
}
Expand Down
8 changes: 2 additions & 6 deletions src/util/uast.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export function isChildComp(name) {
}

// 基本组件children 不需要转化为childrencpt的组件
if (global.execArgs.allExtComp.has(name)) {
if (global.execArgs.allBaseComp.has(name)) {
return false
}

Expand Down Expand Up @@ -265,11 +265,7 @@ export function isBindElementByName(name) {
return true
}

if (global.execArgs.extReactComp.has(name)) {
return false
}

if (global.execArgs.allExtComp.has(name)) {
if (global.execArgs.allBaseComp.has(name)) {
return true
}

Expand Down
Loading

0 comments on commit a30baf6

Please sign in to comment.