originjs / vite-plugin-federation

Module Federation for vite & rollup
Other
2.34k stars 239 forks source link

Failed to fetch dynamically imported module: http://localhost:5001/assets/remoteEntry.js #362

Open ycy1621260121 opened 1 year ago

ycy1621260121 commented 1 year ago

vite + vue@2.7.14 Failed to fetch dynamically imported module: http://localhost:5001/assets/remoteEntry.js

ruleeeer commented 1 year ago

Is http://localhost:5001/assets/remoteEntry.js an accessible address? it should point to a JavaScript file

Darkdreams commented 1 year ago

I also have this problem, I can access this link. You can see the code. Error: Uncaught (in promise) TypeError: Failed to fetch dynamically imported module:http://localhost:3000/assets/remoteEntry.js

package.json

"dependencies": {
    "@originjs/vite-plugin-federation": "^1.2.3",
    "vite-plugin-top-level-await": "^1.3.1",
    "vue": "^3.3.4"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^4.2.3",
    "vite": "^4.4.0"
  }

Remote

export default defineConfig({
  server:{
    port: 8080
  },
  cacheDir: 'node_modules/.cacheDir',
  plugins: [
    vue(),
    federation({
      name: 'remote',
      filename: 'remoteEntry.js',
      exposes: {
        './Test': './src/components/TestVue.vue'
      },
      shared: ["vue"],
      cors: true
    }),
    topLevelAwait({
      promiseExportName: "__tla",
      promiseImportName: i => `__tla_${i}`
    })
  ],
  build: {
    assetsInlineLimit: 40960,
    minify: true,
    cssCodeSplit: false,
    sourcemap:true,
    rollupOptions: {
      output: {
        minifyInternalExports: false
      }
    }
  }
})

Host

export default defineConfig({
    server: {
      port: 4000
    },
    cacheDir: 'node_modules/.cacheDir',
    plugins: [
        vue(),
        federation({
            name: 'host',
            filename: 'remoteEntry.js',
            remotes: {
                remote: 'http://localhost:3000/assets/remoteEntry.js'
            },
            shared: ["vue"]
        }),
        topLevelAwait({
          promiseExportName: "__tla",
          promiseImportName: i => `__tla_${i}`
        })
    ],
    build: {
      target: 'esnext',
      minify: false,
      cssCodeSplit: true,
      rollupOptions: {
        output: {
          format: 'esm',
          entryFileNames: 'assets/[name].js',
          minifyInternalExports: false,
        },
      },
    }
})

Host - main.js

import { createApp, defineAsyncComponent } from 'vue'
import './style.css'
import App from './App.vue'
const RemoteTest = defineAsyncComponent(() => import("remote/Test"));
const app = createApp(App);
app.component("RemoteTest", RemoteTest)
app.mount('#app')

Host - App.vue

<template>
  <RemoteTest />
</template>
moygospadin commented 1 year ago

Is http://localhost:5001/assets/remoteEntry.js an accessible address? it should point to a JavaScript file

Do u solve it?

YKalashnikov commented 11 months ago

any updates on it ?

alexyork commented 5 months ago

@ycy1621260121 @Darkdreams @moygospadin @YKalashnikov I also get this issue. I can access http://localhost:5001/assets/remoteEntry.js when I go to that URL and I see the JS code, but the host app fails to load it for some reason. Did any of you figure out what is going on?

QC2168 commented 3 weeks ago

How did you solve the problem