native-federation-typescript
Bundler agnostic plugins to share federated types.
Install
npm i -D @module-federation/native-federation-typescript
This module provides two plugins:
NativeFederationTypeScriptRemote
This plugin is used to build the federated types.
Configuration
{
moduleFederationConfig: any;
tsConfigPath?: string;
typesFolder?: string;
compiledTypesFolder?: string;
deleteTypesFolder?: boolean;
additionalFilesToCompile?: string[]
compilerInstance?: 'tsc' | 'vue-tsc'
}
Additional configuration
Note that, for Webpack, the plugin automatically inject the devServer.static.directory
configuration.
For the other bundlers, you should configure it by yourself.
NativeFederationTypeScriptHost
This plugin is used to download the federated types.
Configuration
{
moduleFederationConfig: any;
typesFolder?: string;
deleteTypesFolder?: boolean;
maxRetries?: number;
}
Bundler configuration
Vite
import { NativeFederationTypeScriptHost, NativeFederationTypeScriptRemote } from '@module-federation/native-federation-typescript/vite';
export default defineConfig({
plugins: [
NativeFederationTypeScriptRemote({
}),
NativeFederationTypeScriptHost({
}),
],
server: {
proxy: {
'/@mf-types.zip': {
target: 'http://localhost:3000',
changeOrigin: true,
rewrite: () => `/@fs/${process.cwd()}/dist/@mf-types.zip`,
},
},
fs: {
allow: ['./dist'],
},
},
});
Rollup
import { NativeFederationTypeScriptHost, NativeFederationTypeScriptRemote } from '@module-federation/native-federation-typescript/rollup';
export default {
plugins: [
NativeFederationTypeScriptRemote({
}),
NativeFederationTypeScriptHost({
}),
],
};
Webpack
const { NativeFederationTypeScriptHost, NativeFederationTypeScriptRemote } = require('@module-federation/native-federation-typescript/webpack');
module.exports = {
plugins: [
NativeFederationTypeScriptRemote({
}),
NativeFederationTypeScriptHost({
}),
],
};
esbuild
import { build } from 'esbuild';
import { NativeFederationTypeScriptHost, NativeFederationTypeScriptRemote } from '@module-federation/native-federation-typescript/esbuild';
build({
plugins: [
NativeFederationTypeScriptRemote({
}),
NativeFederationTypeScriptHost({
}),
],
});
Rspack
const { NativeFederationTypeScriptHost, NativeFederationTypeScriptRemote } = require('@module-federation/native-federation-typescript/rspack');
module.exports = {
plugins: [
NativeFederationTypeScriptRemote({
}),
NativeFederationTypeScriptHost({
}),
],
};
Rolldown
import { NativeFederationTypeScriptHost, NativeFederationTypeScriptRemote } from '@module-federation/native-federation-typescript/rolldown';
export default {
plugins: [
NativeFederationTypeScriptRemote({
}),
NativeFederationTypeScriptHost({
}),
],
};
TypeScript configuration
To have the type definitions automatically found for imports, add paths to the compilerOptions
in the tsconfig.json
:
{
"paths": {
"*": ["./@mf-types/*"]
}
}
Examples
To use it in a host
module, refer to this example.
To use it in a remote
module, refer to this example.