Publish the package to CDN as a remote package, so another application can dynamic import it at runtime. This allows the remote package maintainer to publish a new version without asking its user to rebundle and redeploy.
In this example
- publisher: it provide dynamic library at http://localhost:3001/assets/remoteEntry.js
- consumer: it is CSR application, references publisher library
- shared-store: it is a plain old javascript library, shared between publisher / consumer
Remote package should be able to share javascript object between publisher / consumer at runtime, so that
- frameworks such as Vue/React do not need to bundled several times, they can share one global instance
- data store must be global singleton
During development, we might change publisher and consumer at the same time. There should be a way to edit/debug without go through publishing process.
Even if the remote package is dynamically loaded, we still want to statically type check the api compatibility.
Loading library dynamically takes time, there should be way to insert loading indicator
consumer/tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"noEmit": true,
"types": ["jest"],
"lib": ["ESNext", "DOM"],
"baseUrl": ".",
"paths": {
"@publisher/*": ["../publisher/src/*"]
}
},
"include": [
"src/**/*.ts"
]
}
we are using typescript paths alias to provide type definition of @publisher, so that
const { default: plusButton } = await import('@publisher/plusButton')
plusButton(document.getElementById('slot1')!);
can be compiled with type checking
refer to https://github.com/originjs/vite-plugin-federation for more information