Skip to content

Commit 946d51d

Browse files
atzcljanryWang
atzcl
authored andcommitted
refactor(antd): adjust the handling of importing components on demand (#485)
1 parent 1646e63 commit 946d51d

File tree

6 files changed

+195
-94
lines changed

6 files changed

+195
-94
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ npm-debug.log*
77
package-lock.json
88
lib/
99
esm/
10+
temp_esm/
1011
dist/
1112
build/
1213
coverage/
@@ -15,4 +16,4 @@ examples/test
1516
.idea/
1617
.vscode/
1718
TODO.md
18-
tsconfig.tsbuildinfo
19+
tsconfig.tsbuildinfo

packages/antd/README.zh-cn.md

+80-26
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ npm install --save @uform/antd
2525
- [`table`](#table)
2626
- [布局组件](#Layout-Components)
2727
- [`<FormCard/>`](#FormCard)
28-
- [`<FormBlock/>`](#FormBlock)
28+
- [`<FormBlock/>`](#FormBlock)
2929
- [`<FormStep/>`](#FormStep)
3030
- [`<FormLayout/>`](#FormLayout)
3131
- [`<FormItemGrid/>`](#FormItemGrid)
@@ -53,15 +53,15 @@ npm install --save @uform/antd
5353
- [`useFieldState`](#useFieldState)
5454
- [`useForm`](#useForm)
5555
- [`useField`](#useField)
56-
- [`useVirtualField`](#useVirtualField)
57-
- [`useFormSpy`](#useFormSpy)
56+
- [`useVirtualField`](#useVirtualField)
57+
- [`useFormSpy`](#useFormSpy)
5858
- [API](#API)
5959
- [`createFormActions`](#createFormActions)
6060
- [`createAsyncFormActions`](#createAsyncFormActions)
6161
- [`FormEffectHooks`](#FormEffectHooks)
6262
- [`createEffectHook`](#createEffectHook)
6363
- [`connect`](#connect)
64-
- [`registerFormField`](#registerFormField)
64+
- [`registerFormField`](#registerFormField)
6565
- [Interfaces](#Interfaces)
6666
- [`ISchema`](#ischema)
6767
- [`IFormActions`](#IFormActions)
@@ -82,10 +82,64 @@ npm install --save @uform/antd
8282
- [`IFieldProps`](#IFieldProps)
8383
- [`IConnectOptions`](#IConnectOptions)
8484
- [`ISpyHook`](#ISpyHook)
85-
8685

8786
### 使用方式
8887

88+
#### 安装
89+
90+
```bash
91+
$ yarn add antd @uform/antd
92+
93+
# or
94+
95+
$ npm install --save antd @uform/antd
96+
```
97+
98+
### 按需加载
99+
100+
#### 在 umijs 中使用
101+
在开始之前,你可能需要安装 [umijs](https://umijs.org/zh/guide/getting-started.html), 并开启 [antd 配置](https://umijs.org/zh/plugin/umi-plugin-react.html#antd)
102+
103+
然后在 umijs 的 `.umirc.js``config/config.js` (二选一)中增加 `extraBabelIncludes` 配置项
104+
105+
> 在使用 uform 组件的时候,请使用 ES Modules import 导入模块
106+
107+
```js
108+
extraBabelIncludes: [
109+
/node_modules[\\/][\\@]uform[\\/]antd[\\/]esm/
110+
],
111+
```
112+
113+
---
114+
115+
#### 在 create-react-app 中使用
116+
117+
在开始之前,请先按照 antd 的[教程](https://ant-design.gitee.io/docs/react/use-with-create-react-app-cn),完成对 `babel-plugin-import` 的配置, 然后只需要在 `config-overrides.js` 中加入 `babelInclude`
118+
119+
> 在使用 uform 组件的时候,请使用 ES Modules import 导入模块
120+
121+
```js
122+
// config-overrides.js
123+
124+
const { override, fixBabelImports, babelInclude } = require('customize-cra');
125+
const path = require('path');
126+
127+
module.exports = override(
128+
fixBabelImports('import', {
129+
libraryName: 'antd',
130+
libraryDirectory: 'es',
131+
style: 'css',
132+
}),
133+
babelInclude([
134+
path.resolve('src'),
135+
/node_modules[\\/][\\@]uform[\\/]antd[\\/]esm/,
136+
]),
137+
)
138+
```
139+
140+
#### 更多脚手架
141+
`@uform/antd` 底层依赖 `antd`,在对 `@uform/antd` 进行按需加载实际也是对 `antd` 进行按需加载,从上面的例子可以看出,脚手架只需要配置好了 `babel-plugin-import`,然后再把 `@uform/antd/esm` 加入 `babel``include` 中即可完成功能配置
142+
89143
---
90144

91145
#### 快速开始
@@ -405,7 +459,7 @@ interface IAntdSchemaFormProps {
405459
// 内联表单
406460
inline?: boolean
407461
// 扩展class
408-
className?: string
462+
className?: string
409463
style?: React.CSSProperties
410464
// label 布局控制
411465
labelCol?: number | { span: number; offset?: number }
@@ -451,7 +505,7 @@ interface IAntdSchemaFormProps {
451505
import React, { Component } from 'react'
452506
import ReactDOM from 'react-dom'
453507
import SchemaForm, {
454-
Field,
508+
Field,
455509
connect,
456510
createFormActions
457511
} from '@uform/antd'
@@ -481,7 +535,7 @@ ReactDOM.render(
481535
import React, { Component } from 'react'
482536
import ReactDOM from 'react-dom'
483537
import SchemaForm, {
484-
Field,
538+
Field,
485539
createFormActions,
486540
FormLayout,
487541
FormButtonGroup,
@@ -597,7 +651,7 @@ import React, { Component } from 'react'
597651
import ReactDOM from 'react-dom'
598652
import SchemaForm, {
599653
FormSlot,
600-
Field,
654+
Field,
601655
createFormActions,
602656
FormLayout,
603657
FormButtonGroup,
@@ -611,10 +665,10 @@ ReactDOM.render(
611665
<SchemaForm>
612666
<FormSlot><div>required</div></FormSlot>
613667
<Field name="a" required type="string" title="field1" />
614-
668+
615669
<FormSlot><div>description</div></FormSlot>
616670
<Field name="b" description="description" type="string" title="field1" />
617-
671+
618672
<FormSlot><div>default value</div></FormSlot>
619673
<Field name="c" default={10} type="string" title="field1" />
620674

@@ -2416,7 +2470,7 @@ const FormFragment = () => {
24162470

24172471
return (
24182472
<React.Fragment>
2419-
<CheckedField name="trigger" label="show/hide" />
2473+
<CheckedField name="trigger" label="show/hide" />
24202474
<div>
24212475
<InputField label="a" name="a" />
24222476
</div>
@@ -2457,7 +2511,7 @@ import React, { useRef } from 'react'
24572511
import ReactDOM from 'react-dom'
24582512
import { Form, Field, VirtualField,
24592513
createFormActions, createEffectHook,
2460-
useForm,
2514+
useForm,
24612515
useFormState,
24622516
useFormEffects,
24632517
useFieldState,
@@ -2485,7 +2539,7 @@ const InputField = props => (
24852539
)
24862540

24872541
const actions = createFormActions()
2488-
const FormFragment = (props) => {
2542+
const FormFragment = (props) => {
24892543
const [formState, setFormState ] = useFormState({ extendVar: 0 })
24902544
const { extendVar } = formState
24912545

@@ -2523,7 +2577,7 @@ import React, { useRef } from 'react'
25232577
import ReactDOM from 'react-dom'
25242578
import { Form, Field, VirtualField,
25252579
createFormActions, createEffectHook,
2526-
useForm,
2580+
useForm,
25272581
useFormEffects,
25282582
useFieldState,
25292583
LifeCycleTypes
@@ -2551,12 +2605,12 @@ const InputField = props => (
25512605

25522606
const changeTab$ = createEffectHook('changeTab')
25532607
const actions = createFormActions()
2554-
const TabFragment = (props) => {
2608+
const TabFragment = (props) => {
25552609
const [fieldState, setLocalFieldState ] = useFieldState({ current: 0 })
25562610
const { current } = fieldState
25572611
const { children, dataSource, form } = props
25582612

2559-
const update = (cur) => {
2613+
const update = (cur) => {
25602614
form.notify('changeTab', cur)
25612615
setLocalFieldState({
25622616
current: cur
@@ -2665,7 +2719,7 @@ const FormFragment = (props) => {
26652719
props: fieldProps,
26662720
mutators
26672721
} = useField({ name: 'username' })
2668-
2722+
26692723
return <input {...fieldProps} {...props} value={state.value} onChange={mutators.change} />
26702724
}
26712725
```
@@ -2691,7 +2745,7 @@ const FormFragment = (props) => {
26912745
state,
26922746
props: fieldProps,
26932747
} = UseVirtualField({ name: 'username' })
2694-
2748+
26952749
return <div style={{ width: fieldProps.width, height: fieldProps.height }}>
26962750
{props.children}
26972751
</div>
@@ -2723,7 +2777,7 @@ const FormFragment = (props) => {
27232777
count: state.count ? state.count + 1 : 1
27242778
})
27252779
})
2726-
2780+
27272781
return <div>
27282782
<div>count: {state.count || 0}</div>
27292783
</div>
@@ -2886,7 +2940,7 @@ ReactDOM.render(<App />, document.getElementById('root'))
28862940
> 包装字段组件,让字段组件只需要支持value/defaultValue/onChange属性即可快速接入表单
28872941

28882942
```typescript
2889-
type Connect = <T extends React.ComponentType<IFieldProps>>(options?: IConnectOptions<T>) =>
2943+
type Connect = <T extends React.ComponentType<IFieldProps>>(options?: IConnectOptions<T>) =>
28902944
(Target: T) => React.PureComponent<IFieldProps>
28912945
```
28922946
**用法**
@@ -3606,12 +3660,12 @@ interface IConnectOptions<T> {
36063660
//默认props
36073661
defaultProps?: Partial<IConnectProps>
36083662
//取值函数,有些场景我们的事件函数取值并不是事件回调的第一个参数,需要做进一步的定制
3609-
getValueFromEvent?: (props: IFieldProps['x-props'], event: Event, ...args: any[]) => any
3663+
getValueFromEvent?: (props: IFieldProps['x-props'], event: Event, ...args: any[]) => any
36103664
//字段组件props transformer
3611-
getProps?: (connectProps: IConnectProps, fieldProps: IFieldProps) => IConnectProps
3665+
getProps?: (connectProps: IConnectProps, fieldProps: IFieldProps) => IConnectProps
36123666
//字段组件component transformer
3613-
getComponent?: (
3614-
target: T,
3667+
getComponent?: (
3668+
target: T,
36153669
connectProps: IConnectProps,
36163670
fieldProps: IFieldProps
36173671
) => T
@@ -3627,4 +3681,4 @@ interface ISpyHook {
36273681
state: any
36283682
type: string
36293683
}
3630-
```
3684+
```

packages/antd/build.ts

+26-4
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,42 @@ import { compile, getCompileConfig } from '../../scripts/build'
22
import ts from 'typescript'
33
import tsImportPluginFactory from 'ts-import-plugin'
44
import glob from 'glob'
5+
import * as fs from 'fs-extra'
56

67
const transformer = tsImportPluginFactory({
78
libraryName: 'antd',
8-
//style: 'css',
9+
// style: 'css',
910
})
1011

1112
function buildESM() {
1213
const { fileNames, options } = getCompileConfig(require.resolve('./tsconfig.json'), {
1314
outDir: './esm',
1415
module: ts.ModuleKind.ESNext
1516
})
16-
compile(fileNames, options, { before: [transformer] })
17+
compile(fileNames, options)
1718
console.log('esm build successfully')
1819
}
1920

21+
const TEMP_OUT_DIR = './temp_esm'
22+
23+
function buildTempESM() {
24+
const { fileNames, options } = getCompileConfig(require.resolve('./tsconfig.json'), {
25+
outDir: TEMP_OUT_DIR,
26+
module: ts.ModuleKind.ESNext
27+
})
28+
compile(fileNames, options, { before: [transformer] })
29+
30+
console.log('temporary esm build successfully')
31+
}
32+
33+
function clearTempESM() {
34+
fs.removeSync(TEMP_OUT_DIR)
35+
36+
console.log('clear temporary esm build successfully')
37+
}
38+
2039
function buildES5() {
21-
const rootNames = glob.sync('./esm/**/*.js')
40+
const rootNames = glob.sync(`${TEMP_OUT_DIR}/**/*.js`)
2241
compile(rootNames, {
2342
allowJs: true,
2443
esModuleInterop: true,
@@ -34,4 +53,7 @@ function buildES5() {
3453

3554

3655
buildESM()
37-
buildES5()
56+
57+
buildTempESM()
58+
buildES5()
59+
clearTempESM()

packages/next/build.ts

+25-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { compile, getCompileConfig } from '../../scripts/build'
22
import ts from 'typescript'
33
import tsImportPluginFactory from 'ts-import-plugin'
44
import glob from 'glob'
5+
import * as fs from 'fs-extra'
56

67
const transformer = tsImportPluginFactory({
78
libraryName: '@alifd/next',
@@ -13,12 +14,30 @@ function buildESM() {
1314
outDir: './esm',
1415
module: ts.ModuleKind.ESNext
1516
})
16-
compile(fileNames, options, { before: [transformer] })
17+
compile(fileNames, options)
1718
console.log('esm build successfully')
1819
}
1920

21+
const TEMP_OUT_DIR = './temp_esm'
22+
23+
function buildTempESM() {
24+
const { fileNames, options } = getCompileConfig(require.resolve('./tsconfig.json'), {
25+
outDir: TEMP_OUT_DIR,
26+
module: ts.ModuleKind.ESNext
27+
})
28+
compile(fileNames, options, { before: [transformer] })
29+
30+
console.log('temporary esm build successfully')
31+
}
32+
33+
function clearTempESM() {
34+
fs.removeSync(TEMP_OUT_DIR)
35+
36+
console.log('clear temporary esm build successfully')
37+
}
38+
2039
function buildES5() {
21-
const rootNames = glob.sync('./esm/**/*.js')
40+
const rootNames = glob.sync(`${TEMP_OUT_DIR}/**/*.js`)
2241
compile(rootNames, {
2342
allowJs: true,
2443
esModuleInterop: true,
@@ -34,4 +53,7 @@ function buildES5() {
3453

3554

3655
buildESM()
37-
buildES5()
56+
57+
buildTempESM()
58+
buildES5()
59+
clearTempESM()

0 commit comments

Comments
 (0)