Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds dynamic remotes example #1

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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: 7 additions & 0 deletions hack/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

pnpm install
pnpm build
cd packages/examples/react-vite
pnpm build
pnpm serve
6 changes: 3 additions & 3 deletions packages/examples/react-vite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ This example demos consumption of federated modules from a vite bundle. `host` (

## Running

First, run `pnpm install`, then `pnpm build` and `pnpm preview`. This will build and serve both `host` and `remote` on ports 5000, 5001 respectively.
First, run `pnpm install`, then `pnpm build` and `pnpm preview`. This will build and serve both `host` and `remote` on ports 3000, 3001 respectively.

- HOST: [localhost:5000](http://localhost:5000/)
- REMOTE: [localhost:5001](http://localhost:5001/)
- HOST: [localhost:3000](http://localhost:3000/)
- REMOTE: [localhost:3001](http://localhost:3001/)

`CTRL + C` can only stop the host server. You can run `pnpm stop` to stop all services.
8 changes: 4 additions & 4 deletions packages/examples/react-vite/host/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --port 5000 --strictPort",
"dev": "vite --port 3000 --strictPort",
"build": "vite build",
"preview": "vite preview --port 5000 --strictPort",
"serve": "vite preview --port 5000 --strictPort"
"preview": "vite preview --port 3000 --strictPort",
"serve": "vite preview --port 3000 --strictPort"
},
"dependencies": {
"react": "^18.2.0",
Expand All @@ -19,4 +19,4 @@
"@vitejs/plugin-react": "^3.0.0",
"vite": "^4.0.3"
}
}
}
21 changes: 19 additions & 2 deletions packages/examples/react-vite/host/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import React, { Suspense } from 'react'
// FIXME: if you use an import() somewhere in this file
// it will trigger an auto-injection of this import too
// which will raise an error
import {
__federation_method_setRemote,
__federation_method_getRemote
} from '__federation__'
import reactLogo from './assets/react.svg'
import './App.css'
import Button from 'remoteApp/Button';

function App() {
const RemoteButton = React.lazy(() => {
__federation_method_setRemote('myRemote', {
url: 'http://localhost:3001/assets/remoteEntry.js',
format: 'esm'
})
return __federation_method_getRemote('myRemote', './Button')
})

return (
<div className="App">
<div>
Expand All @@ -15,7 +30,9 @@ function App() {
</div>
<h1>Vite + React</h1>
<div className="card">
<Button />
<Suspense>
<RemoteButton />
</Suspense>
<p>
Edit <code>src/App.jsx</code> and save to test HMR
</p>
Expand Down
4 changes: 3 additions & 1 deletion packages/examples/react-vite/host/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export default defineConfig({
federation({
name: 'app',
remotes: {
remoteApp: 'http://localhost:5001/assets/remoteEntry.js',
// FIXME: need a dummy import here or we get a:
// "ReferenceError: __rf_placeholder__shareScope is not defined"
dummyApp: 'dummy.js',
},
shared: ['react','react-dom']
})
Expand Down
39 changes: 19 additions & 20 deletions packages/examples/react-vite/package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
{
"name": "react-vite",
"private": true,
"version": "1.0.0",
"workspaces": [
"host",
"remote"
],
"scripts": {
"build": "pnpm --parallel --filter \"./**\" build",
"serve": "pnpm --parallel --filter \"./**\" preview",
"build:remotes": "pnpm --parallel --filter \"./remote\" build",
"serve:remotes": "pnpm --parallel --filter \"./remote\" serve",
"dev:hosts": "pnpm --filter \"./host\" dev",
"stop": "kill-port --port 5000,5001"
},
"devDependencies": {
"kill-port": "^2.0.1",
"@originjs/vite-plugin-federation": "^1.1.10"
}
"name": "react-vite",
"private": true,
"version": "1.0.0",
"workspaces": [
"host",
"remote"
],
"scripts": {
"build": "pnpm --parallel --filter \"./**\" build",
"serve": "pnpm --parallel --filter \"./**\" preview",
"build:remotes": "pnpm --parallel --filter \"./remote\" build",
"serve:remotes": "pnpm --parallel --filter \"./remote\" serve",
"dev:hosts": "pnpm --filter \"./host\" dev",
"stop": "kill-port --port 3000,3001"
},
"devDependencies": {
"kill-port": "^2.0.1",
"@originjs/vite-plugin-federation": "^1.1.10"
}
}
8 changes: 4 additions & 4 deletions packages/examples/react-vite/remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --port 5001 --strictPort",
"dev": "vite --port 3001 --strictPort",
"build": "vite build",
"preview": "vite preview --port 5001 --strictPort",
"serve": "vite preview --port 5001 --strictPort"
"preview": "vite preview --port 3001 --strictPort",
"serve": "vite preview --port 3001 --strictPort"
},
"dependencies": {
"react": "^18.2.0",
Expand All @@ -19,4 +19,4 @@
"@vitejs/plugin-react": "^3.0.0",
"vite": "^4.0.3"
}
}
}
7 changes: 6 additions & 1 deletion packages/lib/src/dev/remote-development.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ function __federation_method_wrapDefault(module ,need){
function __federation_method_getRemote(remoteName, componentName){
return __federation_method_ensure(remoteName).then((remote) => remote.get(componentName).then(factory => factory()));
}
export {__federation_method_ensure, __federation_method_getRemote , __federation_method_unwrapDefault , __federation_method_wrapDefault}

function __federation_method_setRemote(remoteName, remoteConfig) {
remotesMap[remoteName] = remoteConfig;
}

export {__federation_method_ensure, __federation_method_getRemote, __federation_method_setRemote, __federation_method_unwrapDefault , __federation_method_wrapDefault}
;`
},
config(config: UserConfig) {
Expand Down
5 changes: 5 additions & 0 deletions packages/lib/src/prod/remote-production.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,14 @@ export function prodRemotePlugin(
return __federation_method_ensure(remoteName).then((remote) => remote.get(componentName).then(factory => factory()));
}

function __federation_method_setRemote(remoteName, remoteConfig) {
remotesMap[remoteName] = remoteConfig;
}

export {
__federation_method_ensure,
__federation_method_getRemote,
__federation_method_setRemote,
__federation_method_unwrapDefault,
__federation_method_wrapDefault
}
Expand Down