vite-plugin-federation icon indicating copy to clipboard operation
vite-plugin-federation copied to clipboard

Preload helper is loading the resources from wrong urls

Open anhnt2310 opened this issue 3 years ago • 6 comments

Versions

  • originjs: ^1.1.6
  • node: v16.15

Reproduction

I see the file preload-helper.js that is bundled could be wrong

image

You can see in the above image, code will automatically set the URL with the base URL /, after that, It will be appended to <header> of the web.

image

But the host-app fetches preload-helper.js from remote-app and run on host-app. Actually, the host-app doesn't have those. So we will get some errors like this

image

Did I get this problem right?

My vite.config.ts

export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), '');
  const antdLessConfig = createAntdLessConfig(env);

  return {
    plugins: [
      react(),
      eslint({
        cache: mode === 'production',
        emitError: true,
        emitWarning: mode === 'development',
      }),
      relay,
      dynamicImport(),
      federation({
        name: 'remoteULK',
        filename: 'remoteEntry.js',
        exposes: {
          './ULKApp': './src/bootstrap',
        },
        shared: {
          react: { requiredVersion: deps.react },
          'react-dom': { requiredVersion: deps['react-dom'] },
        },
      }),
      vitePluginImp({
        optimize: true,
        libList: [
          {
            libName: 'antd',
            libDirectory: 'es',
            style: (name) => `antd/es/${name}/style`,
          },
        ],
      }),
    ],
    resolve: {
      alias: [
        {
          find: '@',
          replacement: path.resolve(__dirname, 'src'),
        },
        {
          find: '~',
          replacement: path.resolve(__dirname),
        },
      ],
    },
    server: {
      open: true,
      port: +env.DEV_PORT,
      // https: 'http',
    },
    preview: {
      port: +env.DEV_PORT,
    },
    build: {
      target: 'esnext',
      minify: false,
      cssCodeSplit: false,
    },
    css: {
      preprocessorOptions: {
        ...antdLessConfig,
      },
    },
  };
});

What is Expected?

Loading resources properly

anhnt2310 avatar Jul 06 '22 04:07 anhnt2310

image The same problem, each component get wrong URL from `preload-helper.js`

but after that, I can get the component with right URL from chunk image image

I found a temporary solution: image need to add a strict base option, and preload-helper.js will have right URLs. But it is bad way and hard to support if your application works in 2 envs.

AndreiSoroka avatar Aug 01 '22 21:08 AndreiSoroka

@ruleeeer Sorry for ping, but maybe do you have any idea?

AndreiSoroka avatar Aug 05 '22 20:08 AndreiSoroka

@AndreiSoroka How can I reproduce the problem?

Jiannan-dev avatar Aug 12 '22 01:08 Jiannan-dev

@AndreiSoroka How can I reproduce the problem

I'll create reproduce repo this week

AndreiSoroka avatar Aug 16 '22 08:08 AndreiSoroka

@ruleeeer https://github.com/AndreiSoroka/mf-reproduce-vite-preload

strange, but last time there were no problems with styles 😄

ezgif-2-1c7dd1f3cc

AndreiSoroka avatar Aug 19 '22 21:08 AndreiSoroka

@AndreiSoroka HI , thx for your reproduce repo I changed the configuration and I see it locally like this and it seems to work

image image

you only need to add cssCodeSplit:false into build:{...} in remote-app/vite.config.js image

Jiannan-dev avatar Aug 26 '22 09:08 Jiannan-dev