Skip to content

更新代码 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,44 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [2.6.4](https://github.com/areslabs/alita/compare/v2.6.3...v2.6.4) (2020-05-22)


### Bug Fixes

* **alita-core:** 修复wxml引用wxs变量名t和template模板相关变量冲突导致无法正常渲染问题 ([716c335](https://github.com/areslabs/alita/commit/716c335b8c638661bb5e2e37af667693e2d443a0))





## [2.6.3](https://github.com/areslabs/alita/compare/v2.6.2...v2.6.3) (2020-05-21)

**Note:** Version bump only for package root





## [2.6.2](https://github.com/areslabs/alita/compare/v2.6.1...v2.6.2) (2020-05-21)

**Note:** Version bump only for package root





## [2.6.1](https://github.com/areslabs/alita/compare/v2.6.0...v2.6.1) (2020-05-21)


### Bug Fixes

* **wx-react:** 修复RNBaseComponent 的子节点 被 generic的bug ([0123f7f](https://github.com/areslabs/alita/commit/0123f7fb256e1bbc6eba1c2768e48bfa80141a8c))





# [2.6.0](https://github.com/areslabs/alita/compare/v2.5.1...v2.6.0) (2020-05-15)


Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"conventionalCommits": true
}
},
"version": "2.6.0"
"version": "2.6.4"
}
32 changes: 32 additions & 0 deletions packages/alita-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,38 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [2.6.4](https://github.com/areslabs/alita/compare/v2.6.3...v2.6.4) (2020-05-22)

**Note:** Version bump only for package @areslabs/alita





## [2.6.3](https://github.com/areslabs/alita/compare/v2.6.2...v2.6.3) (2020-05-21)

**Note:** Version bump only for package @areslabs/alita





## [2.6.2](https://github.com/areslabs/alita/compare/v2.6.1...v2.6.2) (2020-05-21)

**Note:** Version bump only for package @areslabs/alita





## [2.6.1](https://github.com/areslabs/alita/compare/v2.6.0...v2.6.1) (2020-05-21)

**Note:** Version bump only for package @areslabs/alita





# [2.6.0](https://github.com/areslabs/alita/compare/v2.5.1...v2.6.0) (2020-05-15)

**Note:** Version bump only for package @areslabs/alita
Expand Down
2 changes: 1 addition & 1 deletion packages/alita-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"weixin",
"redux"
],
"version": "2.6.0",
"version": "2.6.4",
"description": "首个ReactNative转微信小程序引擎",
"main": "./lib/index.js",
"scripts": {
Expand Down
35 changes: 35 additions & 0 deletions packages/alita-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,41 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [2.6.4](https://github.com/areslabs/alita/compare/v2.6.3...v2.6.4) (2020-05-22)


### Bug Fixes

* **alita-core:** 修复wxml引用wxs变量名t和template模板相关变量冲突导致无法正常渲染问题 ([716c335](https://github.com/areslabs/alita/commit/716c335b8c638661bb5e2e37af667693e2d443a0))





## [2.6.3](https://github.com/areslabs/alita/compare/v2.6.2...v2.6.3) (2020-05-21)

**Note:** Version bump only for package @areslabs/alita-core





## [2.6.2](https://github.com/areslabs/alita/compare/v2.6.1...v2.6.2) (2020-05-21)

**Note:** Version bump only for package @areslabs/alita-core





## [2.6.1](https://github.com/areslabs/alita/compare/v2.6.0...v2.6.1) (2020-05-21)

**Note:** Version bump only for package @areslabs/alita-core





# [2.6.0](https://github.com/areslabs/alita/compare/v2.5.1...v2.6.0) (2020-05-15)


Expand Down
2 changes: 1 addition & 1 deletion packages/alita-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"weixin",
"redux"
],
"version": "2.6.0",
"version": "2.6.4",
"description": "alita-core",
"main": "./lib/index.js",
"scripts": {
Expand Down
10 changes: 5 additions & 5 deletions packages/alita-core/src/extractWxCompFiles/extractWxmlFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ export const handleChanged = (info, finalJSPath) => {
// 如果只使用一个child 小程序会报递归, 然后就不渲染了
const subT = `
<template name="${name}">
<block wx:if="{{t.l(d)}}">{{d}}</block>
<${genericCompName} wx:elif="{{d.${genericCompDiuu}}}" diuu="{{d.${genericCompDiuu}}}" style="{{t.s(d.${genericCompDiuu}style)}}"/>
<block wx:if="{{_t.l(d)}}">{{d}}</block>
<${genericCompName} wx:elif="{{d.${genericCompDiuu}}}" diuu="{{d.${genericCompDiuu}}}" style="{{_t.s(d.${genericCompDiuu}style)}}"/>
<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>
<${genericCompName} wx:elif="{{item.${genericCompDiuu}}}" diuu="{{item.${genericCompDiuu}}}" style="{{t.s(item.${genericCompDiuu}style)}}"/>
<block wx:if="{{_t.l(item)}}">{{item}}</block>
<${genericCompName} wx:elif="{{item.${genericCompDiuu}}}" diuu="{{item.${genericCompDiuu}}}" style="{{_t.s(item.${genericCompDiuu}style)}}"/>
<template wx:else is="{{item.tempName}}" data="{{...item}}"/>
</block>
</block>
Expand All @@ -97,7 +97,7 @@ export const handleChanged = (info, finalJSPath) => {

const utilWxsPath = `${RootPrefixPlaceHolader}/commonwxs.wxs`

templateWxml = `<wxs src="${utilWxsPath}" module="t" />
templateWxml = `<wxs src="${utilWxsPath}" module="_t" />

${templateWxml}

Expand Down
8 changes: 7 additions & 1 deletion packages/alita-core/src/packByWebpack/gatherInfo-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {isReactComponent, parseCode, isReactFragment} from "../util/uast"
import {getModuleInfo, setModuleInfo} from '../util/cacheModuleInfos'
import {getLibCompInfos} from "../util/getAndStorecompInfos";
import {judgeLibPath} from "../util/util";
import deleteNoWxCode from "../preproccessCode/deleteNoWxCode"
import optimizeImports from "../preproccessCode/importOptimize"

/**
* 收集alita 处理所必须的信息
Expand All @@ -30,7 +32,11 @@ export default function (this: webpack.loader.LoaderContext, context: string):
const filepath = this.resourcePath

console.log(`开始处理:${filepath.replace(configure.inputFullpath, '')} ...`.info)
const ast = parseCode(context, npath.extname(filepath))
let ast = parseCode(context, npath.extname(filepath))
//删除非wx平台的代码
ast = deleteNoWxCode(ast)
//移除无用的的Import,减少import不支持转化的模块导致转化错误(包括移除非wx平台的代码,导致的无用import)
ast = optimizeImports(ast)

//TODO 暂时通过此方式,让ast和sourceCode/filepath 建立联系
// @ts-ignore
Expand Down
52 changes: 52 additions & 0 deletions packages/alita-core/src/preproccessCode/deleteNoWxCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import traverse from "@babel/traverse"
import * as t from '@babel/types'

/**
* 删除非wx平台的代码
* if (Platform.OS === 'wx) { ... } else { ... } ===> if (Platform.OS === 'wx) { ... } ('wx' === Platform.OS) 同理)
* if (Platform.OS !== 'wx) { ... } else { ... } ===> if (Platform.OS !== 'wx) { } else { ... }
* let value = Platform.OS === 'wx? value1 : value2 ===> let value = value1
* let value = Platform.OS !== 'wx? value2 : value1 ===> let value = value2
*/
export default function deleteNoWxCode(ast) {
traverse(ast, {
IfStatement(path) {
checkAndRemoveCode(path, true, false)
},
ConditionalExpression(path) {
checkAndRemoveCode(path, false, true)
}
})

return ast
}

function checkAndRemoveCode(path,isIfStatement,isConditionalExpression){
let hasPlatformOS = false; //条件语句存在 Platform.OS
path.traverse({
MemberExpression(path) {
if (t.isIdentifier(path.node.object, { name: "Platform"})
&& t.isIdentifier(path.node.property, { name: "OS" })) {
hasPlatformOS = true;
}
}
});

if (hasPlatformOS && t.isBinaryExpression(path.node.test, { operator: "===" })
&& (t.isStringLiteral(path.node.test.right, { value: "wx"}) || t.isStringLiteral(path.node.test.left, { value: "wx"}))) {
if (isIfStatement && path.node.alternate) {
path.replaceWith(path.node.consequent)
} else if(isConditionalExpression && path.node.consequent){
path.replaceWith(path.node.consequent)
}
}

if (hasPlatformOS && t.isBinaryExpression(path.node.test, { operator: "!==" })
&& (t.isStringLiteral(path.node.test.right, { value: "wx" }) || t.isStringLiteral(path.node.test.left, { value: "wx"}))) {
if (isIfStatement && path.node.consequent) {
path.node.consequent.body= [];
} else if(isConditionalExpression && path.node.alternate){
path.replaceWith(path.node.alternate)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* match the syntax like
* `import a from 'a'`
* `import { b, c } from 'a'`
*/

import * as t from '@babel/types'
import { defaultIgnoreImportList } from '.';

/**
* get identifiers from the code, as follows
*
* input: `import { b, c } from 'a'`
* output: ['b', 'c']
*
* input: `import a from 'a'`
* output: ['a']
*/
function getSpecifierIdentifiers(specifiers = [], withPath = false) {
const collection = [];
function loop(path, index) {
const node = path.node
const item = !withPath ? node.local.name : { path, name: node.local.name }
switch (node.type) {
case 'ImportDefaultSpecifier':
case 'ImportSpecifier':
case 'ImportNamespaceSpecifier':
collection.push(item);
break;
}
}
specifiers.forEach(loop);

return collection;
}

/**
* input: `import a, {b, c as d} from 'where'`
* output: [a, b, d]
*
* input: `import 'where'`
* output: undefined
*/
export default function getSpecImport(path, opts = {}) {
const source = path.get('source')
const specifiers = path.get('specifiers')
const ignore = defaultIgnoreImportList
if (t.isImportDeclaration(path)) {
if (t.isStringLiteral(source) && (!ignore || !match(ignore, source.node.value))) {
if (specifiers && specifiers.length > 0) {
return getSpecifierIdentifiers(specifiers, true);
}
}
}
}

function match (rule, value) {
if (typeof rule === 'string') {
return rule === value
}
if (rule instanceof RegExp) {
return rule.test(value)
}
if (typeof rule === 'function') {
return rule(value)
}
if (Array.isArray(rule)) {
return rule.some(r => match(r, value))
}
}
Loading