The shared configuration is used to share common dependencies between consumers and producers, reducing the runtime download volume and thus improving performance. shared allows you to configure rules for reusing dependency versions. You can learn more about the scenarios where shared is applicable and how to use shared through the FAQ.
PluginSharedOptionsundefinedThe PluginSharedOptions type is as follows:
booleanfalseWhether to allow only one version of the shared module within the shared scope (singleton mode).
stringrequire('project/package.json')[devDeps | dep]['depName']The required version, which can be a version range. The default value is the current application's dependency version.
requiredVersion. If it is, it will be used normally. If it is less than requiredVersion, a warning will be given in the console, and the smallest version available in the shared dependencies will be used.requiredVersion and the other sets singleton, the dependency with requiredVersion will be loaded, and the singleton party will directly use the dependency with requiredVersion, regardless of the version.Setting eager to true will package the shared dependencies into the entry file, which may result in a large entry file size. Use with caution.
booleanfalseWhether to immediately load the shared module.
Under normal circumstances, you need to enable lazy entry, and then asynchronously load shared modules on demand. If you want to use shared but do not want to enable lazy entry, you can set eager to true.
string'default'share scope name, default value is 'default' .
Consuming modules across projects often encounters issues such as duplicate dependency loading and singleton restrictions. These issues can be resolved by setting up shared.
lodash.getreactlodash (not used on demand)antdAt this point, you can add the corresponding dependencies to the shared configuration.
Depending on the use case, Module Federation supports two forms of shared dependency configuration: array and object. The former is suitable for most scenarios, while the latter is suitable for complex customization needs.
Array Format (General Scenario)
Simply add the corresponding dependencies to the shared configuration in the Module Federation build configuration, for example:
Object Format (Customized Configuration)
Add the shared dependencies in the shared configuration of the Module Federation Plugin, with the key being the dependency name and the value being the provided configuration.