@empjs/cli

EMP 是一个基于 Rspack 的高性能模块联邦框架,专注于微前端和组件共享,支持 React、Vue 等多框架。
快速开始
环境要求
- Node.js
^20.19.0 || >=22.12.0
- pnpm
10.x(推荐使用仓库根 packageManager 指定的 pnpm@10.33.0)
安装
pnpm i @empjs/cli@rc
npm i @empjs/cli@rc
项目脚本配置
在 package.json 中添加以下脚本:
"scripts": {
"dev": "emp dev",
"build": "emp build",
"start": "emp serve",
"stat": "emp build --analyze",
"emp": "emp"
}
执行指令
开发模式
pnpm dev
pnpm dev --env test
pnpm dev --hot
pnpm dev --env-vars API_URL=https://example.com
构建
pnpm build
pnpm stat
pnpm build --watch
pnpm build --watch --serve
本地预览
pnpm start
配置文件
TypeScript 配置 (tsconfig.json)
{
"extends": "@empjs/cli/tsconfig/react",
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@/*": ["src/*"],
"~@/*": ["src/*"]
},
"plugins": [
{
"name": "typescript-plugin-css-modules",
"options": {
"additionalData": "@import '~@/css/mixin';",
"allowUnknownClassnames": true
},
"postcssOptions": {
"useConfig": true
}
}
]
},
"include": ["src"]
}
基础配置 (emp-config.js/ts)
import {defineConfig} from '@empjs/cli'
export default defineConfig(store => {
return {
chain(chainConfig) {},
}
})
Rspack 2 配置入口
build.rspack 用于显式接入 Rspack 2 的新增能力。EMP 不会默认开启高风险实验能力,业务需要按场景配置。
import {defineConfig} from '@empjs/cli'
export default defineConfig(() => {
return {
build: {
useESM: true,
target: 'es2018',
moduleIds: 'hashed',
rspack: {
experiments: {pureFunctions: true},
splitChunks: {chunks: 'all', enforceSizeThreshold: 80000},
parser: {
javascript: {
pureFunctions: ['createPureValue'],
},
css: {
resolveImport: false,
},
},
swc: {
detectSyntax: 'auto',
},
},
},
}
})
React 与模块联邦配置示例
import {defineConfig} from '@empjs/cli'
import pluginReact from '@empjs/plugin-react'
import {externalReact, pluginRspackEmpShare} from '@empjs/share'
export default defineConfig(store => {
const ip = store.server.ip
const port = 6001
return {
plugins: [
pluginReact(),
pluginRspackEmpShare({
name: 'mfHost',
exposes: {
'./App': './src/App',
'./CountComp': './src/CountComp',
'./Section': './src/component/Section',
},
manifest: true,
dts: {
generateTypes: true,
},
empRuntime: {
framework: {
global: 'EMP_ADAPTER_REACT',
libs: [`https://unpkg.com/@empjs/cdn-react@0.18.0/dist/reactRouter.${store.mode}.umd.js`],
},
runtime: {
lib: `http://${ip}:2100/sdk.js`,
},
setExternals: externalReact,
},
}),
],
define: {ip, port},
build: {
polyfill: {
mode: 'entry',
entryCdn: 'https://unpkg.com/@empjs/polyfill@0.0.2/dist/es.js',
browserslist: store.browserslistOptions.h5,
},
sourcemap: true,
},
server: {
port,
open: false,
hot: true,
},
debug: {
clearLog: false,
},
}
})
高级功能
模块联邦
EMP 提供了强大的模块联邦功能,支持组件共享和微前端架构:
- 暴露组件:通过
exposes 配置暴露组件
- 远程加载:使用
remotes 配置加载远程组件
- 类型支持:通过
dts 配置生成类型声明
多框架支持
EMP 支持多种前端框架:
- React:使用
@empjs/plugin-react
- Vue 2:使用
@empjs/plugin-vue2
- Vue 3:使用
@empjs/plugin-vue3
QQ 交流群
