module-federation / vite

Vite Plugin for Module Federation
MIT License
343 stars 27 forks source link

Adding hostInit does not work if there is no html file (SvelteKit) #189

Open RobinBomkamp opened 3 days ago

RobinBomkamp commented 3 days ago

If you want to use module-federation/vite in combination with SvelteKit. The hostInit file does not get included in the generated output. The file is compiled, but the reference is missing.

It used to work in version 1.1.3 of this package.

In version 1.1.4 there was a fix for the issue #167. This caused the problem. After the fix the hostInit will be referenced directly in the html element in a script tag. Therefore, the plugin check every file ending with '.html' (see pluginAddEntry.ts)

SvelteKit does not generate any HTML files. The element is generated on the server side due to ssr. A template of the base html file is part of the generated internal.js file.

...addEntry({
      entryName: 'hostInit',
      entryPath: getHostAutoInitPath(),
      inject: 'html',
    }),

The inject: 'html' causes the problem. If you are setting this value to 'entry' or simply remove it the hostInit will be referenced as before the issue #167.

Should this field be made configurable? Or can it be injected as an entry for svelte kit projects as before?

zhangHongEn commented 3 days ago

I noticed another issue remains unresolved. Can version 1.1.3 be successfully used in SvelteKit?

zhangHongEn commented 3 days ago

If the SSR environment can be detected, use inject: 'entry'.