-
Notifications
You must be signed in to change notification settings - Fork 29
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
The import paths in the built remote entry file do not use the base path configured in vite.config. #217
Comments
For those eager to resolve this issue quickly, I’m providing the following workaround until the author fixes the problem. vite.config.js import { federation } from '@module-federation/vite'
import fs from 'node:fs'
import path from 'node:path'
const CDN_ORIGIN = 'https://cdn.example.com/project-name'
export default {
base: CDN_ORIGIN,
...
plugins: [
federation({
name: 'remote',
filename: 'remote.js',
exposes: {
'./preload': './src/preload',
},
shared: [],
}),
{
name: 'modify-remote-js',
apply: 'build',
closeBundle () {
const outputDir = path.resolve(__dirname, 'dist')
const fileToProcess = path.join(outputDir, 'remote.js')
if (fs.existsSync(fileToProcess)) {
let content = fs.readFileSync(fileToProcess, 'utf-8')
// For assets deploy to CDN
content = content.replace(/\.\/assets/g, `${CDN_ORIGIN}/assets`)
// Fixed: [Uncaught TypeError] i is not a function at hostInit-xxx.js
content = content.replace('init,', 'init=function(){},')
fs.writeFileSync(fileToProcess, content)
}
},
},
],
} |
Thanks @baijunjie for your help, would you like to drop a PR for that? |
@gioboa I’m very willing to provide a PR, but there are some issues I haven’t fully understood yet, so I’m not sure how to make the modifications. I found the template code for the remote entry file, but noticed that the path is a placeholder
const VIRTUAL_EXPOSES = 'virtual:mf-exposes'
`import exposesMap from "${VIRTUAL_EXPOSES}"` |
@baijunjie Thanks, you can search
|
@gioboa I tried debugging I have a question: in this case, how exactly is the remote entry file |
Use https://module-federation.io/configure/manifest.html and configure the remoteEntry file to include a hash for placement on a CDN. |
Under normal circumstances, the reference paths for resource files built by a plugin should align with the base path specified in the Vite config.
The filename of the exposed remote module usually doesn't include a hash because it's the entry point for remote modules and needs to be accessible by external applications. Therefore, it is typically deployed separately on Nginx, while other resources are placed on a CDN. If a relative path is used, it can cause a bug where files can't be found.
vite.config.js
output remote.js
The text was updated successfully, but these errors were encountered: