Skip to content

Avoid runtime imports of typescript if DtsPlugin is not configured #4271

@MichaelWashburnJr

Description

@MichaelWashburnJr

Clear and concise description of the problem

👋 I'd like to update some of the places where DtsPlugin gets imported so that it is only required when needed.

I'm working on a project that prunes typescript out of the node_modules folder because it is not in use (but still installed due to a dep on scip-typescript). Typescript increased the footprint of our project by 32MB when it wasn't being pruned, hence why we delete it.

Suggested solution

Originally, I had this as my alternative suggestion, but I think its a much cleaner and extensible approach so I'm flipping them.

I would suggest we create a BaseModuleFederationPlugin, which is just a slimmed down version of the rspack/webpack ModuleFederationPlugin
with minimal dependencies.

The base plugin wouldn't configure dts, the stats plugin, bridge plugin, or any other optional plugins. However, it could centralize the logic for adding these and just rely on dependency injection.

To maintain backwards compatibility, the existing rspack/ModuleFederationPlugin would just extend from the base and either add a few hooks to implement the additional plugins or initialize the base with the dts, stats, and bridge plugin.

This way, people who don't want to bring in certain dependencies can import directly from the base and control the dependencies that get used. You could potentially even create your own dts plugins to use different versions of typescript than the module federation master branch.

Alternative

I've done local testing and for an Rspack usage, I only need to adjust imports to DtsPlugin in these two files:

  • packages/manifest/src/utils.ts
  • packages/rspack/src/ModuleFederationPlugin.ts

In these files I would remove top of file imports from require('@module-federation/dts-plugin'); (and dts-plugin/core) and replace them by requiring the module after we check that dts is configured.

Doing this makes it so that typescript itself is no longer imported during runtime if dts is disabled in your module federation config.

Additional context

No response

Validations

  • Read the Contributing Guidelines.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions