Skip to content

Commit

Permalink
feat: add demo
Browse files Browse the repository at this point in the history
  • Loading branch information
liangxiaoming committed Oct 27, 2024
1 parent 0ab4eac commit e5e5bf3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
65 changes: 64 additions & 1 deletion demos/react18_with_base_config/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,72 @@
# EMP3 React18 DEMO with base config

`demo`主要展示,`EMP3.0`在部署到非根目录地址时候,解决获取远程模块`host`异常的问题。

[官网相关配置说明](https://empjs.dev/plugin/tool/share.html#getpublicpath)

## 运行
```
pnpm i
pnpm dev
```

[文档说明](https://empjs.dev/guide/empShare/react.html)
访问`http://localhost:1801/`可在控制台看到以下报错:

```
GET http://localhost:1801/output/emp.js net::ERR_ABORTED 404
```
实际上正确的`host`地址应该是 `http://localhost:1802/output/emp.js`

查看`host`项目的`emp.config.ts`

`react-host-with-base-config` / `emp.config.ts`:

```ts title="react-host-with-base-config/emp.config.ts"
import {defineConfig} from '@empjs/cli'
...
export default defineConfig(store => {
return {
...
base: '/output/',
...
}
})
```
由于项目需要,基站的访问地址需要在二级目录`/output/`,使用了[base配置项](https://empjs.dev/config/base/base.html)

但配置了`base`项后,影响了消费者(`react-app`)加载基站资源的地址策略,从原来使用基站的Host,改为使用`base`配置项的值作为资源前缀。

为了解决这个问题,需要在基站项目`pluginRspackEmpShare`项中,额外添加`getPublicPath`配置项。

...
修改`host`项目的`emp.config.ts`,添加`pluginRspackEmpShare``getPublicPath`配置。

`react-host-with-base-config` / `emp.config.ts`:

```ts title="react-host-with-base-config/emp.config.ts"
import {defineConfig} from '@empjs/cli'
import {pluginRspackEmpShare} from '@empjs/share'
export default defineConfig(store => {
return {
plugins: [
...
pluginRspackEmpShare({
...
getPublicPath: `return "http://localhost:1802/output/"`,
...
}),
],
...
}
})
```

## 再次运行
```
pnpm i
pnpm dev
```
访问`http://localhost:1801/`,此时看到基站资源可以被正确加载。

[官网相关配置说明](https://empjs.dev/plugin/tool/share.html#getpublicpath)

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default defineConfig(store => {
'./App': './src/App',
'./CountComp': './src/CountComp',
},
// getPublicPath: `return "http://localhost:1802/output/"`,
empRuntime: {
runtimeLib: "https://unpkg.com/@empjs/share@3.1.5/output/sdk.js",
frameworkLib: "https://unpkg.com/@empjs/libs-18@0.0.1/dist",
Expand Down

0 comments on commit e5e5bf3

Please sign in to comment.