I'm currently trying to set up a project that uses Webpack's Module Federation to share components.
To do so, I set up two basic vue projects with the cli and added a vue.config.js file in both projects:
Host project (that will include the shared component) (running on localhost:8000)
const { ModuleFederationPlugin } = require('webpack').container
module.exports = {
configureWebpack: {
plugins: [
new ModuleFederationPlugin({
name: 'shell',
filename: 'remoteEntry.js',
remotes: {
component: 'component@http://localhost:8001/remoteEntry.js'
},
exposes: {},
shared: {}
})
]
}
}
The component project (which shares the component) (running on localhost:8001):
const { ModuleFederationPlugin } = require('webpack').container
module.exports = {
configureWebpack: {
plugins: [
new ModuleFederationPlugin({
name: 'component',
filename: 'remoteEntry.js',
remotes: {},
exposes: {
'./HelloWorld': './src/components/HelloWorld.vue'
},
shared: {}
})
]
}
}
I try to load the component in my App.vue:
<template>
<img alt="Vue logo" src="./assets/logo.png" />
<HelloWorld msg="Welcome to Your Vue.js App" />
<otherComp />
</template>
<script>
import { defineAsyncComponent } from "vue";
import HelloWorld from "./components/HelloWorld.vue";
const otherComp = defineAsyncComponent(() => import("component/HelloWorld"));
export default {
name: "App",
components: {
HelloWorld,
otherComp,
},
};
</script>
Indeed it tries to load the component, but instead of loading it from localhost:8001 (where the component is hosted) it tries to load it from localhost:8000:
The same path at localhost:8001 does exist. Some debugging showed, that the webpack publicPath seems to be set to "/" (causing the hosting application at localhost:8000 to set the url to /js/src_components_HelloWorld_vue.js
)
/******/ /* webpack/runtime/publicPath */
/******/ !function() {
/******/ __webpack_require__.p = "/";
/******/ }();
I believe this is due to how vue-cli interacts with webpack. Is this a known problem and how can this be fixed?