fix: Ensure federated types are downloaded before type compiling/ type checking

- Adds a function to the webpack.config and documentation to make sure host types are downloaded to the plugin before the first type compile/ type check is happening

Avoids an error where on cold start the types were not yet downloaded but types were already checked.
This commit is contained in:
rvveber
2025-11-12 17:16:11 +01:00
parent 482975c2b5
commit ab74aeff5c
5 changed files with 77 additions and 16 deletions

View File

@@ -1,6 +1,11 @@
const path = require('path');
const { NativeFederationTypeScriptHost } = require('@module-federation/native-federation-typescript/webpack');
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
const {
NativeFederationTypeScriptHost,
} = require('@module-federation/native-federation-typescript/webpack');
const {
NativeFederationTypeScriptHost: NativeFederationTypeScriptHostCore,
} = require('@module-federation/native-federation-typescript');
const moduleFederationConfig = {
name: 'plugin_frontend',
@@ -31,6 +36,24 @@ const moduleFederationConfig = {
},
};
let mfTypesReady;
const ensureFederatedTypesPlugin = {
apply(compiler) {
compiler.hooks.beforeCompile.tapPromise(
'EnsureFederatedTypes',
async () => {
if (!mfTypesReady) {
const downloader = NativeFederationTypeScriptHostCore.raw({
moduleFederationConfig,
});
mfTypesReady = downloader.writeBundle();
}
await mfTypesReady;
},
);
},
};
module.exports = (env, argv) => {
const dev = argv.mode !== 'production';
@@ -70,10 +93,11 @@ module.exports = (env, argv) => {
new ModuleFederationPlugin(moduleFederationConfig),
...(dev
? [
NativeFederationTypeScriptHost({
moduleFederationConfig,
}),
]
ensureFederatedTypesPlugin,
NativeFederationTypeScriptHost({
moduleFederationConfig,
}),
]
: []),
],
};