3.1.3 • Published 1 year ago

@module-federation/typescript v3.1.3

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Federated Types

This plugin enables Typescript Types support for Module Federated Components.

Installation

$ npm i @module-federation/typescript

Usage

Register the plugin in webpack configuration (webpack.config.js) file

import webpack from 'webpack';
const { FederatedTypesPlugin } = require('@module-federation/typescript');

const federationConfig = {
  name: 'my-app',
  filename: 'remoteEntry.js',
  exposes: {
    //...exposed components
    './Button': './src/Button',
    './Input': './src/Input',
  },
  remotes: {
    app2: 'app2@http://localhost:3002/remoteEntry.js', // or Just the URL 'http://localhost:3002/remoteEntry.js'
  },
  shared: ['react', 'react-dom'],
};

module.exports = {
  /* ... */
  plugins: [
    // ...
    new FederatedTypesPlugin({
      federationConfig,
      // ...
    }),
  ],
};

To enable verbose logging add folowing in webpack config:

infrastructureLogging: {
  level: 'log';
}

The Module Federation plugin is required to be registered separately from this plugin. The federation configuration provided to the Typescript plugin or Module Federation plugin can be different, as an example - to discern pure javascript remotes from Typescript remotes.

You need to register this plugin in both remote and host apps. The plugin will automatically create a directory named @mf-types in the host app - that contains all the types exported by the remote apps.

To have the type definitions automatically found for imports, add paths in tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "*": ["./@mf-types/*"]
    }
  }
}

baseUrl must also be set for paths to work properly

Plugin Options

SettingTypeRequiredDefaultDescription
additionalFilesToCompilestring[]No[]Any additional files to be included (besides ModuleFederationPluginOptions.remotes) in the emission of Typescript types. This is useful for global.d.ts files not directly referenced.
compilertsc or vue-tscNotscThe compiler to use to emit declaration files. Use vue-tsc to emit declarations from your Vue Templates
disableTypeCompilationbooleanNofalseDisable compiling types for exposed components
disableDownloadingRemoteTypesbooleanNofalseDisable downloading types from remote apps
downloadRemoteTypesTimeoutnumberNo2000The maximum time to wait for downloading remote types. This is to prevent blocking compilation or hanging the plugin.
federationConfigYes-Configuration for ModuleFederationPlugin
typescriptFolderNamestringNo@mf-typesThe folder name to download remote types and output compiled types
typescriptCompiledFolderNamestringNo_typesThe folder name to output the raw output from the ts compiler

Usage in Next.js

You need to manually pass the federationConfig object to the plugin. The remotes value should contain absolute path.

Sample code:

// next.config.js
const FederatedTypesPlugin = require('@module-federation/typescript');

module.exports = {
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    config.plugins.push(
      new FederatedTypesPlugin({
        federationConfig: {
          ...federationConfig,
          remotes: { app2: 'app2@http://localhost:3000/remoteEntry.js' },
        },
        // ...
      })
    );
    return config;
  },
};

Support

Drop me a message on twitter for support/feedback, or maybe just say Hi at @prasannamestha

Credits

Shoutout to @ScriptedAlchemy for helping with the development of this plugin.

3.1.4

1 year ago

3.1.3

1 year ago

3.1.2

1 year ago

2.4.1

2 years ago

2.4.0

2 years ago

2.4.2

2 years ago

3.1.0-canary.1

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.3.0

2 years ago

2.2.2

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.1.2

2 years ago

2.0.3

3 years ago

2.1.1

3 years ago

2.0.2

3 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.1.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

0.1.1

3 years ago

0.2.2

3 years ago

0.2.5

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.10

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago