@farmfe/plugin-react-components
On-demand components auto importing for React.
Installationâ
- npm
- yarn
- pnpm
npm install @farmfe/plugin-react-components
yarn add @farmfe/plugin-react-components
pnpm add @farmfe/plugin-react-components
Usageâ
@farmfe/plugin-react-components
is a Rust plugin, you only need to configure its package name in plugins
field in farm.config.ts
.
import { UserConfig } from '@farmfe/core';
const config: UserConfig = {
plugins: ['@farmfe/plugin-react-components', { /** options here */}]
}
Featuresâ
- đ Supports React out-of-the-box.
- ⨠Supports both components and directives.
- đ Tree-shakable, only registers the components you use.
- đĒ Folder names as namespaces.
- đĻž Full TypeScript support.
- đ Built-in resolvers for popular UI libraries.
Usageâ
Use components in templates as you would usually do, it will import components on demand, and there is no import
and component registration
required anymore! If you register the parent component asynchronously (or lazy route), the auto-imported components will be code-split along with their parent.
It will automatically turn this
export function Main() {
return <HelloWorld msg="Hello React + Farm" />
}
into this
import HelloWorld from './src/components/HelloWorld'
export function Main() {
return <HelloWorld msg="Hello React + Farm" />
}
Note By default this plugin will import components in the
src/components
path. You can customize it using thedirs
option.
TypeScriptâ
To get TypeScript support for auto-imported components.
Components({
dts: true, // enabled by default if `typescript` is installed
})
Once the setup is done, a components.d.ts
will be generated and updates automatically with the type definitions. Feel free to commit it into git or not as you want.
Make sure you also add
components.d.ts
to yourtsconfig.json
underinclude
.
Importing from UI Librariesâ
We have several built-in resolvers for popular UI libraries like Ant Design, Arco Design, and Material UI, where you can enable them by:
Supported Resolvers:
// farm.config.js
import { UserConfig } from '@farmfe/core';
const config: UserConfig = {
plugins: ['@farmfe/plugin-react-components', {
local: true,
resolvers:[
{
module: "antd",
prefix: "Ant"
},
{
module:"@arco-design/web-react",
prefix: "Arco",
import_style: true // style/index.js
}
]
}]
}
Configurationâ
The following show the default values of the configuration
component
{
// relative paths to the directory to search for components.
dirs: ['src/components'],
// resolvers for custom components.
resolvers: [],
/**
* Components are introduced with Absolute or Relative path.
*
* @default Absolute
*/
import_mode: "Absolute"
/**
* Is it valid for local components
*
* @default true
*/
local: true,
/**
* import style `style/index.js` , also accepts a path for custom path (<Component>/**) with components
*
* @default false
*/
importStyle?: boolean | string
// generate `components.d.ts` global declarations,
// also accepts a path for custom filename
// default: `true` if package typescript is installed
dts: true,
// Filters for transforming targets (components to insert the auto import)
// Note these are NOT about including/excluding components registered - use `Regex` for that
include: ["src/components"],
exclude: ["node_modules"],
}