Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

支持注册基础组件 #324

Merged
merged 2 commits into from
Nov 21, 2019
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
7 changes: 6 additions & 1 deletion .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"packages": ["packages/remax", "packages/remax-cli"]
"packages": [
"packages/remax",
"packages/remax-cli",
"packages/remax.macro",
"packages/babel-preset-remax"
]
}
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
lib
node_modules
**/__tests__/**/expected
**/__tests__/**/expected
43 changes: 43 additions & 0 deletions docs/guide/component.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,46 @@ Remax
```html
<view class="view" style="display: flex;" onTap="handleClick"></view>
```

Darmody marked this conversation as resolved.
Show resolved Hide resolved
## 注册基础组件

如果小程序添加了新的组件,而你所用的 Remax 版本还没提供该组件的支持,Remax 允许你自己创建一个新的基础组件。

假设微信小程序新增了一个 `<foo-bar>` 组件,你可以这么做以让 Remax 提前支持:

```jsx
import { createHostComponent } from 'remax/macro';

const FooBar = createHostComponent('foo-bar', ['foo']);

function Page() {
return <FooBar foo="bar" />;
}
```

如果你使用的是 TypeScript,还可以定义 `props` 类型:

```jsx
import { createHostComponent } from 'remax/macro';

const FooBar = createHostComponent<{ foo: string; }>('foo-bar', ['foo]);

function Page() {
return <FooBar foo="bar" />;
}
```

> 组件名称和组件属性不能是动态变量,以下写法是错误的。

```jsx
import { createHostComponent } from 'remax/macro';

const componentName = 'foo-bar';
const props = ['foo'];
// 必须直接写明组件名称和组件属性,不可用动态写法或变量传递的形式。
const FooBar = createHostComponent(componentName, props);

function Page() {
return <FooBar foo="bar" />;
}
```
5 changes: 4 additions & 1 deletion lint-staged.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ const micromatch = require('micromatch');
module.exports = {
'*.{ts,tsx}': ['eslint --fix', 'prettier --write', 'git add'],
'*.{md,js,jsx}': files => {
const match = micromatch.not(files, '**/__tests__/**/expected/**/*.js');
const match = micromatch.not(files, [
'**/__tests__/**/expected/**/*.js',
'**/tests/fixtures/**/output.js',
]);
return match.length > 1
? [`prettier ${match.join(' ')} --write`, `git add ${match.join(' ')}`]
: [];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "1.0.0",
"scripts": {
"watch": "lerna run watch --parallel",
"prebuild": "lerna run build --scope remax",
"prebuild": "lerna run build --scope remax --scope remax.macro",
"build": "lerna run build --ignore website",
"test": "lerna run --scope remax --scope remax-cli --stream test",
"test:cov": "lerna run --scope remax --scope remax-cli --stream test -- --collect-coverage",
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-preset-remax/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"@babel/plugin-syntax-jsx": "^7.2.0",
"@babel/preset-env": "^7.6.0",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.6.0"
"@babel/preset-typescript": "^7.6.0",
"babel-plugin-macros": "^2.6.2"
},
"devDependencies": {
"@babel/core": "^7.6.0",
Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset-remax/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function preset(api: any, presetOption: PresetOption) {
return {
presets,
plugins: [
require('babel-plugin-macros'),
require('@babel/plugin-proposal-class-properties'),
require('@babel/plugin-proposal-object-rest-spread'),
require('@babel/plugin-syntax-jsx'),
Expand Down
16 changes: 16 additions & 0 deletions packages/remax-cli/src/__tests__/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ describe('build', () => {
path.resolve(__dirname, `./fixtures/universe/expected/toutiao`)
);

runTest(
'createHostComponent',
'alipay',
path.resolve(__dirname, `./fixtures/createHostComponent/expected/alipay`)
);
runTest(
'createHostComponent',
'wechat',
path.resolve(__dirname, `./fixtures/createHostComponent/expected/wechat`)
);
runTest(
'createHostComponent',
'toutiao',
path.resolve(__dirname, `./fixtures/createHostComponent/expected/toutiao`)
);

runTest('customRootDir');
runTest('disablePxToRpx');
runTest('babelrc');
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import '../npm/remax/esm/render.js';
import { cloneElement, createElement } from 'react';
import '../npm/remax/esm/createAppConfig.js';
import '../npm/remax/esm/Platform.js';
import '../npm/remax/esm/createHostComponent.js';
import createPageConfig from '../npm/remax/esm/createPageConfig.js';
import '../npm/remax/esm/index.js';
import View from '../npm/remax/esm/adapters/alipay/components/View.js';
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import '../npm/remax/esm/render.js';
import { createElement } from 'react';
import '../npm/remax/esm/createAppConfig.js';
import '../npm/remax/esm/Platform.js';
import '../npm/remax/esm/createHostComponent.js';
import createPageConfig from '../npm/remax/esm/createPageConfig.js';
import '../npm/remax/esm/index.js';
import View from '../npm/remax/esm/adapters/alipay/components/View.js';
Expand Down
Loading