originjs / vite-plugin-federation

Module Federation for vite & rollup
Other
2.24k stars 234 forks source link

Uncaught (in promise) TypeError: Failed to construct 'URL': Invalid URL #591

Open tomorrow285 opened 5 months ago

tomorrow285 commented 5 months ago

Versions

Reproduction

After I run npm run build, I got this error.

This is my vite.config.ts:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import vitePluginRequire from 'vite-plugin-require';
import ViteRequireContext from '@originjs/vite-plugin-require-context';
import federation from '@originjs/vite-plugin-federation';
import topLevelAwait from 'vite-plugin-top-level-await';
import { viteCommonjs, esbuildCommonjs } from '@originjs/vite-plugin-commonjs';

export default defineConfig(({ mode }) => {
    return {
        base: '/',
        plugins: [
            react(),
            vitePluginRequire(),
            ViteRequireContext(),
            federation({
                name: 'bohrium',
                remotes: {
                    dpMol: {
                        external: 'https://cdn.xxxx/remoteEntry.js',
                        externalType: 'url',
                        format: 'var',
                    },
                },
                filename: 'remoteEntry.[contenthash:8].js',
                exposes: {
                    './A': '../src/shared/hooks/a',
                    './B': '../src/shared/components/b',
                },
                shared: ['react', 'react-dom'],
            }),
            topLevelAwait({
                promiseExportName: '__tla',
                promiseImportName: i => `__tla_${i}`,
            }),
            viteCommonjs(),
        ],
        optimizeDeps: {
            esbuildOptions: {
                target: ['es2015', 'safari11'],
                plugins: [esbuildCommonjs()],
            },
            exclude: ['jquery'],
        },
        build: {
            target: ['es2015', 'safari11'],
            outDir: './build',
            rollupOptions: {
                external: ['react', 'react-dom', 'jquery'],
                output: {
                    globals: {
                        react: 'React',
                        'react-dom': 'ReactDOM',
                        jquery: ['jQuery', '$'],
                    },
                },
            },
            sourcemap: true,
        },

    };
});

This is the error log:

Uncaught (in promise) TypeError: Failed to construct 'URL': Invalid URL
    at __federation_fn_import-b9cca9f6.js:1:5743

What is actually happening?

The error occurs in new URL(xxx, K.url) because K is an empty object.

This is the code in __federation_fn_import-b9cca9f6.js, the complete document is in the attachment:

const K = {};
const q = {
    react: {
        get: ()=>()=>I(new URL("__federation_shared_react-2fa0e958.js",K.url).href),
        import: !0
    },
[__federation_fn_import-b9cca9f6.js.zip](https://github.com/originjs/vite-plugin-federation/files/14902568/__federation_fn_import-b9cca9f6.js.zip)

    "react-dom": {
        get: ()=>()=>I(new URL("__federation_shared_react-dom-661945d7.js",K.url).href),
        import: !0
    }
}
1511578084 commented 5 months ago

你这个问题解决了么?

Issues-translate-bot commented 5 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Have you solved this problem?

CS19940801 commented 2 months ago

我也遇到同样的问题,该如何解决? image

foreverstars commented 4 weeks ago

我也遇到同样的问题

1723110899864
Issues-translate-bot commented 4 weeks ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


I also encountered the same problem