Skip to content

Commit

Permalink
fix(jsx2mp): attributes transform in wechat&release 0.4.18 (#1933)
Browse files Browse the repository at this point in the history
* fix(jsx2mp): attributes transform in wechat

* fix(jsx2mp): lint error

* feat(jsx2mp): finish event transform handle

* refactor(jsx2mp): finish event handle

* docs(jsx2mp): add event handler example

* fix(example): lint error

* chore(jsx2mp): remove useless code

* docs(jsx2mp): update comment

* build(jsx-compiler): publish version 0.4.18
  • Loading branch information
ChrisCindy authored Aug 27, 2020
1 parent 5eebec6 commit f3ead1d
Show file tree
Hide file tree
Showing 26 changed files with 328 additions and 16 deletions.
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ npm run build
* use-rax-compiled-component-in-runtime-miniapp: Rax 小程序运行时项目使用 Rax 编译时组件
* app-lifecycle: 使用 app 生命周期
* page-lifecycle-with-router: 使用 page 生命周期 + 路由跳转
* event-handler: 事件绑定(编译时小程序)

3 changes: 3 additions & 0 deletions examples/event-handler/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['rax']
};
17 changes: 17 additions & 0 deletions examples/event-handler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

*~
*.swp
*.log

.DS_Store
.idea/
.temp/

build/
dist/
lib/
coverage/
node_modules/

template.yml
15 changes: 15 additions & 0 deletions examples/event-handler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# rax-materials-basic-app

## Getting Started

### `npm run start`

Runs the app in development mode.

Open [http://localhost:9999](http://localhost:9999) to view it in the browser.

The page will reload if you make edits.

### `npm run build`

Builds the app for production to the `build` folder.
20 changes: 20 additions & 0 deletions examples/event-handler/build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"inlineStyle": false,
"plugins": [
[
"build-plugin-rax-app",
{
"targets": [
"miniapp",
"wechat-miniprogram"
],
"miniapp": {
"buildType": "compile"
},
"wechat-miniprogram": {
"buildType": "compile"
}
}
]
]
}
33 changes: 33 additions & 0 deletions examples/event-handler/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@rax-materials/scaffolds-app-js",
"author": "rax",
"description": "Rax 无线跨端应用工程,使用 JavaScript。",
"version": "0.1.0",
"scripts": {
"build": "build-scripts build",
"start": "build-scripts start",
"lint": "eslint --ext .js --ext .jsx ./"
},
"dependencies": {
"rax": "^1.1.0",
"rax-app": "^2.0.0",
"driver-universal": "^3.0.0",
"rax-image": "^2.0.0",
"rax-link": "^1.0.1",
"rax-text": "^1.0.0",
"rax-view": "^1.0.0",
"rax-document": "^0.1.0"
},
"devDependencies": {
"@alib/build-scripts": "^0.1.0",
"babel-eslint": "^10.0.3",
"build-plugin-rax-app": "^5.0.0",
"eslint": "^6.8.0",
"eslint-config-rax": "^0.1.0",
"eslint-plugin-import": "^2.20.0",
"eslint-plugin-module": "^0.1.0",
"eslint-plugin-react": "^7.18.0"
},
"private": true,
"originTemplate": "@rax-materials/scaffolds-app-js"
}
4 changes: 4 additions & 0 deletions examples/event-handler/src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { runApp } from 'rax-app';
import appConfig from './app.json';

runApp(appConfig);
11 changes: 11 additions & 0 deletions examples/event-handler/src/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"routes": [
{
"path": "/",
"source": "pages/Home/index"
}
],
"window": {
"title": "Rax App"
}
}
5 changes: 5 additions & 0 deletions examples/event-handler/src/components/CustomComp/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.logo {
width: 200rpx;
height: 180rpx;
margin-bottom: 20rpx;
}
15 changes: 15 additions & 0 deletions examples/event-handler/src/components/CustomComp/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createElement } from 'rax';
import Image from 'rax-image';

import './index.css';

export default (props) => {
const { uri } = props;
const source = { uri };
return (
<Image
className="logo"
source={source}
/>
);
};
21 changes: 21 additions & 0 deletions examples/event-handler/src/document/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createElement } from 'rax';
import { Root, Style, Script} from 'rax-document';

function Document() {
return (
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no,viewport-fit=cover" />
<title>rax-materials-basic-app</title>
<Style />
</head>
<body>
{/* root container */}
<Root />
<Script />
</body>
</html>
);
}
export default Document;
16 changes: 16 additions & 0 deletions examples/event-handler/src/pages/Home/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.home {
align-items: center;
margin-top: 200rpx;
}

.title {
font-size: 45rpx;
font-weight: bold;
margin: 20rpx 0;
}

.info {
font-size: 36rpx;
margin: 8rpx 0;
color: #555;
}
21 changes: 21 additions & 0 deletions examples/event-handler/src/pages/Home/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createElement } from 'rax';
import View from 'rax-view';
import Text from 'rax-text';
import Image from 'rax-image';

import './index.css';

import CustomComp from '../../components/CustomComp';

export default function Home() {
function noop() {}
return (
<View className="home">
<View className="test" onClick={noop}>onClick -> onTap (alibaba miniapp) && bindtap (wechat miniprogram) in rax-view</View>
<Text className="test" onClick={noop}>onClick -> bindtap (wechat miniprogram) in rax-text</Text>
<Image onClick={noop} onChange={noop}>onClick/onChange are not transformed (alibaba miniapp) && onClick -> bindonClick/onChange -> bindonChange (wechat miniprogram)</Image>
<button className="test" style={{color: 'red'}} onClick={noop} onChange={noop}>onClick -> onTap (alibaba miniapp) && onClick -> bindtap (wechat miniprogram) && onChange -> bindchange (wechat miniprogram) </button>
<CustomComp onClick={noop} onChange={noop}>not transformed</CustomComp>
</View>
);
}
3 changes: 2 additions & 1 deletion examples/with-miniapp-plugin-component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"rax-link": "^1.0.1",
"rax-text": "^1.0.0",
"rax-view": "^1.0.0",
"rax-document": "^0.1.0"
"rax-document": "^0.1.0",
"universal-env": "^3.0.0"
},
"devDependencies": {
"@alib/build-scripts": "^0.1.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createElement, useState } from 'rax';
import View from 'rax-view';
// eslint-disable-next-line
import Test from 'plugin://myPlugin/test';

export default function Home() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createElement, useState } from 'rax';
import View from 'rax-view';
import Text from 'rax-text';
// eslint-disable-next-line
import Plugin from 'plugin://myPlugin/hello-component';

export default function Home() {
Expand Down
2 changes: 1 addition & 1 deletion packages/jsx-compiler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsx-compiler",
"version": "0.4.17",
"version": "0.4.18",
"license": "BSD-3-Clause",
"description": "Parser for Rax JSX Statements.",
"files": [
Expand Down
7 changes: 7 additions & 0 deletions packages/jsx-compiler/src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const parserAdapters = {
compatibleText: false,
// Need transform style & class keyword
styleKeyword: false,
// No need to transform onClick -> bindonclick
needTransformEvent: false,
slotScope: true,
// Need transform key
needTransformKey: false,
Expand Down Expand Up @@ -71,6 +73,8 @@ const parserAdapters = {
},
styleKeyword: true,
slotScope: false,
// Need transform onClick -> bindonclick
needTransformEvent: true,
needTransformKey: true,
triggerRef: true,
processSlider: true,
Expand All @@ -97,10 +101,13 @@ const parserAdapters = {
},
styleKeyword: true,
slotScope: false,
// Need transform onClick -> bindonclick
needTransformEvent: true,
needTransformKey: true,
triggerRef: true,
needRegisterProps: true,
},
componentCommonProps
};

module.exports = parserAdapters;
1 change: 0 additions & 1 deletion packages/jsx-compiler/src/baseComponents.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = [
'rax-text',
'rax-scrollview',
'rax-recyclerview',
'rax-textinput',
Expand Down
3 changes: 3 additions & 0 deletions packages/jsx-compiler/src/modules/__tests__/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('Transform JSX Attribute', () => {
it('should not transform wechat custom component className', () => {
const code = '<Custom className="box">test</Custom>';
const ast = parseExpression(code);
ast.openingElement.name.isCustom = true;
_transformAttribute(ast, code, wxAdapter);
expect(genCode(ast).code).toEqual('<Custom className="box">test</Custom>');
});
Expand All @@ -49,6 +50,7 @@ describe('Transform JSX Attribute', () => {
it('should transform wechat custom component style into styleSheet', () => {
const code = "<Custom style={{width: '100rpx'}}>test</Custom>";
const ast = parseExpression(code);
ast.openingElement.name.isCustom = true;
_transformAttribute(ast, code, wxAdapter);
expect(genCode(ast).code).toEqual(`<Custom styleSheet={{
width: '100rpx'
Expand All @@ -57,6 +59,7 @@ describe('Transform JSX Attribute', () => {
it('should transform wechat custom component id', () => {
const code = '<Custom id="box">test</Custom>';
const ast = parseExpression(code);
ast.openingElement.name.isCustom = true;
_transformAttribute(ast, code, wxAdapter);
expect(genCode(ast).code).toEqual('<Custom id="box" componentId="box">test</Custom>');
});
Expand Down
Loading

0 comments on commit f3ead1d

Please sign in to comment.