我目前正在尝试设置一个使用Webpack的模块联邦来共享组件的项目。
为此,我使用cli设置了两个基本的vue项目,并在两个项目中添加了vue.config.js文件:
主机项目(将包含共享组件) (运行在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: {}
})
]
}
}
组件项目(共享该组件的)(运行在 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: {}
})
]
}
}
我尝试在我的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>
确实它试图加载组件,但是它没有从本地主机:8001(组件托管的地方)加载它,而是试图从本地主机:8000加载它:
在本地主机:8001上存在相同的路径。一些调试显示,webpack publicPath 似乎被设置为“ /”(导致localhost:8000上的托管应用程序将url设置为/js/src_components_HelloWorld_vue.js
)
/******/ /* webpack/runtime/publicPath */
/******/ !function() {
/******/ __webpack_require__.p = "/";
/******/ }();
我认为这是由于vue-cli与webpack互动的方式造成的。这是一个已知的问题,如何解决?