Skip to content

feat(dts-plugin): support custom outputDir for DTS type emission#4487

Merged
ScriptedAlchemy merged 14 commits intomodule-federation:mainfrom
zhouxinyong:feat/dts-custom-output-dir
Mar 13, 2026
Merged

feat(dts-plugin): support custom outputDir for DTS type emission#4487
ScriptedAlchemy merged 14 commits intomodule-federation:mainfrom
zhouxinyong:feat/dts-custom-output-dir

Conversation

@zhouxinyong
Copy link
Copy Markdown
Contributor

Description

When webpack entry files are output to a subdirectory (e.g. dist/react/production/), the DTS plugin always emits @mf-types.zip and @mf-types.d.ts to the compiler output root (dist/react/). Consumers infer the types URL from the entry path, resulting in 404s.

This PR makes two minimal changes:

  1. Expose outputDir in SDK typesDtsRemoteOptions now includes outputDir?: string, allowing users to configure where types are emitted.
  2. Fix asset emission pathGenerateTypesPlugin now uses path.relative() instead of path.basename() to compute asset names, so types land in the correct subdirectory relative to the compiler output.

Usage

new ModuleFederationPlugin({
  dts: {
    generateTypes: {
      outputDir: `dist/react/${process.env.DEPLOY_ENVIRONMENT || 'production'}`
    }
  }
})

Backward Compatibility

  • Default behavior is unchanged: when outputDir is not set, path.relative() produces the same result as the previous path.basename().
  • No breaking changes to existing APIs.

Related Issue

N/A (workaround previously required a post-build copy script)

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation.

Expose the existing internal `outputDir` option in the SDK's `DtsRemoteOptions`
interface and fix `GenerateTypesPlugin` to use `path.relative()` instead of
`path.basename()` when computing asset emission paths. This allows users to
configure a custom output directory so that `@mf-types.zip` and `@mf-types.d.ts`
are emitted alongside entry files in subdirectories (e.g. `dist/react/production/`).

Previously, even if `outputDir` was set internally, `emitAsset()` always stripped
directory info via `path.basename()`, causing the zip to land at the compiler
output root instead of the intended subdirectory.
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 2, 2026

🦋 Changeset detected

Latest commit: 3c752b4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 45 packages
Name Type
@module-federation/dts-plugin Major
@module-federation/sdk Major
@module-federation/cli Major
@module-federation/enhanced Major
@module-federation/manifest Major
@module-federation/metro Major
@module-federation/rspack Major
@module-federation/devtools Major
@module-federation/data-prefetch Major
@module-federation/esbuild Patch
@module-federation/managers Major
@module-federation/modern-js-v3 Major
@module-federation/modern-js Patch
@module-federation/nextjs-mf Patch
@module-federation/node Patch
@module-federation/retry-plugin Major
@module-federation/rsbuild-plugin Major
@module-federation/rspress-plugin Major
@module-federation/runtime-core Major
@module-federation/runtime Major
@module-federation/storybook-addon Major
@module-federation/utilities Patch
@module-federation/webpack-bundler-runtime Major
@module-federation/bridge-react-webpack-plugin Major
@module-federation/bridge-react Major
@module-federation/bridge-vue3 Major
shared-tree-shaking-no-server-host Patch
shared-tree-shaking-no-server-provider Patch
@module-federation/metro-plugin-rnc-cli Major
@module-federation/metro-plugin-rnef Major
shared-tree-shaking-with-server-host Patch
shared-tree-shaking-with-server-provider Patch
node-dynamic-remote-new-version Patch
node-dynamic-remote Patch
remote5 Patch
remote6 Patch
website-new Patch
@module-federation/runtime-tools Major
@module-federation/inject-external-runtime-core-plugin Major
@module-federation/third-party-dts-extractor Major
@module-federation/bridge-shared Major
@module-federation/error-codes Major
create-module-federation Major
@module-federation/treeshake-server Major
@module-federation/treeshake-frontend Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify
Copy link
Copy Markdown

netlify bot commented Mar 2, 2026

Deploy Preview for module-federation-docs ready!

Name Link
🔨 Latest commit 3c752b4
🔍 Latest deploy log https://app.netlify.com/projects/module-federation-docs/deploys/69b3769c9e7d420008fb677f
😎 Deploy Preview https://deploy-preview-4487--module-federation-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dc5e1f0faa

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 2, 2026

Bundle Size Report

14 package(s) changed, 26 unchanged.

Package dist + ESM entry

Package Total dist (raw) Delta ESM gzip Delta
@module-federation/cli 26.7 kB no change 786 B no change
@module-federation/core 39.1 kB no change 173 B no change
@module-federation/devtools 4.80 MB -8411 B (-0.2%) 0 B no change
@module-federation/dts-plugin 300.2 kB +1.8 kB (+0.6%) 4.5 kB +222 B (+5.0%)
@module-federation/enhanced 810.8 kB no change 712 B no change
@module-federation/inject-external-runtime-core-plugin 5.4 kB -26 B (-0.5%) 548 B no change
@module-federation/managers 69.9 kB no change 334 B no change
@module-federation/manifest 138.4 kB no change 182 B no change
@module-federation/metro-plugin-rnc-cli 0 B no change 314 B no change
@module-federation/node 188.4 kB no change 217 B no change
@module-federation/runtime 19.5 kB -13 B (-0.1%) 616 B no change
@module-federation/sdk 118.6 kB +181 B (+0.1%) 798 B no change
@module-federation/storybook-addon 76.5 kB no change 100 B no change
@module-federation/utilities 110.6 kB no change 328 B no change

Bundle targets

Package Web bundle (gzip) Delta Node bundle (gzip) Delta
@module-federation/cli 2.3 kB -35 B (-1.5%) 2.3 kB -35 B (-1.5%)
@module-federation/core 1.1 kB -34 B (-3.0%) 1.0 kB -32 B (-3.0%)
@module-federation/devtools n/a n/a n/a n/a
@module-federation/dts-plugin 13.8 kB +124 B (+0.9%) 13.8 kB +124 B (+0.9%)
@module-federation/enhanced 5.9 kB -59 B (-1.0%) 5.9 kB -59 B (-1.0%)
@module-federation/inject-external-runtime-core-plugin 405 B no change 405 B no change
@module-federation/managers 2.4 kB -28 B (-1.1%) 2.4 kB -28 B (-1.1%)
@module-federation/manifest 6.2 kB -40 B (-0.6%) 6.2 kB -40 B (-0.6%)
@module-federation/metro-plugin-rnc-cli 411 B -27 B (-6.2%) 411 B -27 B (-6.2%)
@module-federation/node 9.2 kB -30 B (-0.3%) 9.2 kB -30 B (-0.3%)
@module-federation/runtime 625 B no change 625 B no change
@module-federation/sdk 4.4 kB no change 5.3 kB no change
@module-federation/storybook-addon 1.9 kB -24 B (-1.2%) 1.7 kB -24 B (-1.3%)
@module-federation/utilities 2.6 kB -33 B (-1.2%) 2.6 kB -33 B (-1.2%)

Consumer scenarios

Scenario Web output (gzip) Delta Node output (gzip) Delta Gap (node-web) Delta
Enhanced remoteEntry 19.1 kB -12 B (-0.1%) 20.1 kB -18 B (-0.1%) +1.0 kB -6 B

Total dist (raw): 10.62 MB (-6447 B (-0.1%))
Total ESM gzip: 69.0 kB (+222 B (+0.3%))
Total web bundle (gzip): 158.7 kB (-186 B (-0.1%))
Total node bundle (gzip): 159.3 kB (-184 B (-0.1%))
Tracked ./bundler entry gzip: 556 B (no change)
Tracked ./bundler web bundle (gzip): 4.5 kB (no change)
Tracked ./bundler node bundle (gzip): 4.5 kB (no change)

Bundle sizes are generated with rslib (Rspack). Package-root metrics preserve the historical report. Tracked subpath exports such as ./bundler are measured separately so ENV_TARGET-driven tree-shaking is visible. Bare imports are externalized to keep package-level sizes consistent, and assets are emitted as resources.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 2, 2026

Open in StackBlitz

@module-federation/devtools

pnpm add https://pkg.pr.new/@module-federation/devtools@56cc97e

@module-federation/cli

pnpm add https://pkg.pr.new/@module-federation/cli@56cc97e

create-module-federation

pnpm add https://pkg.pr.new/create-module-federation@56cc97e

@module-federation/data-prefetch

pnpm add https://pkg.pr.new/@module-federation/data-prefetch@56cc97e

@module-federation/dts-plugin

pnpm add https://pkg.pr.new/@module-federation/dts-plugin@56cc97e

@module-federation/enhanced

pnpm add https://pkg.pr.new/@module-federation/enhanced@56cc97e

@module-federation/error-codes

pnpm add https://pkg.pr.new/@module-federation/error-codes@56cc97e

@module-federation/esbuild

pnpm add https://pkg.pr.new/@module-federation/esbuild@56cc97e

@module-federation/managers

pnpm add https://pkg.pr.new/@module-federation/managers@56cc97e

@module-federation/manifest

pnpm add https://pkg.pr.new/@module-federation/manifest@56cc97e

@module-federation/metro

pnpm add https://pkg.pr.new/@module-federation/metro@56cc97e

@module-federation/metro-plugin-rnc-cli

pnpm add https://pkg.pr.new/@module-federation/metro-plugin-rnc-cli@56cc97e

@module-federation/metro-plugin-rnef

pnpm add https://pkg.pr.new/@module-federation/metro-plugin-rnef@56cc97e

@module-federation/modern-js

pnpm add https://pkg.pr.new/@module-federation/modern-js@56cc97e

@module-federation/modern-js-v3

pnpm add https://pkg.pr.new/@module-federation/modern-js-v3@56cc97e

@module-federation/native-federation-tests

pnpm add https://pkg.pr.new/@module-federation/native-federation-tests@56cc97e

@module-federation/native-federation-typescript

pnpm add https://pkg.pr.new/@module-federation/native-federation-typescript@56cc97e

@module-federation/nextjs-mf

pnpm add https://pkg.pr.new/@module-federation/nextjs-mf@56cc97e

@module-federation/node

pnpm add https://pkg.pr.new/@module-federation/node@56cc97e

@module-federation/retry-plugin

pnpm add https://pkg.pr.new/@module-federation/retry-plugin@56cc97e

@module-federation/rsbuild-plugin

pnpm add https://pkg.pr.new/@module-federation/rsbuild-plugin@56cc97e

@module-federation/rspack

pnpm add https://pkg.pr.new/@module-federation/rspack@56cc97e

@module-federation/rspress-plugin

pnpm add https://pkg.pr.new/@module-federation/rspress-plugin@56cc97e

@module-federation/runtime

pnpm add https://pkg.pr.new/@module-federation/runtime@56cc97e

@module-federation/runtime-core

pnpm add https://pkg.pr.new/@module-federation/runtime-core@56cc97e

@module-federation/runtime-tools

pnpm add https://pkg.pr.new/@module-federation/runtime-tools@56cc97e

@module-federation/sdk

pnpm add https://pkg.pr.new/@module-federation/sdk@56cc97e

@module-federation/storybook-addon

pnpm add https://pkg.pr.new/@module-federation/storybook-addon@56cc97e

@module-federation/third-party-dts-extractor

pnpm add https://pkg.pr.new/@module-federation/third-party-dts-extractor@56cc97e

@module-federation/treeshake-frontend

pnpm add https://pkg.pr.new/@module-federation/treeshake-frontend@56cc97e

@module-federation/treeshake-server

pnpm add https://pkg.pr.new/@module-federation/treeshake-server@56cc97e

@module-federation/typescript

pnpm add https://pkg.pr.new/@module-federation/typescript@56cc97e

@module-federation/utilities

pnpm add https://pkg.pr.new/@module-federation/utilities@56cc97e

@module-federation/webpack-bundler-runtime

pnpm add https://pkg.pr.new/@module-federation/webpack-bundler-runtime@56cc97e

@module-federation/bridge-react

pnpm add https://pkg.pr.new/@module-federation/bridge-react@56cc97e

@module-federation/bridge-react-webpack-plugin

pnpm add https://pkg.pr.new/@module-federation/bridge-react-webpack-plugin@56cc97e

@module-federation/bridge-shared

pnpm add https://pkg.pr.new/@module-federation/bridge-shared@56cc97e

@module-federation/bridge-vue3

pnpm add https://pkg.pr.new/@module-federation/bridge-vue3@56cc97e

@module-federation/inject-external-runtime-core-plugin

pnpm add https://pkg.pr.new/@module-federation/inject-external-runtime-core-plugin@56cc97e

commit: 56cc97e

@2heal1
Copy link
Copy Markdown
Member

2heal1 commented Mar 3, 2026

I can not reproduce it . Can you provide the minimal reproduce repo ?

@zhouxinyong
Copy link
Copy Markdown
Contributor Author

zhouxinyong commented Mar 5, 2026

Hi @2heal1, here is the minimal reproduction repo: https://github.com/zhouxinyong/mf-dts-outputdir-repro

Steps to reproduce:

cd remote && npm install && npx webpack --mode production

Then check the output:

dist/
├── @mf-types.zip           ← WRONG: should be in production/
├── @mf-types.d.ts
├── production/
│   └── remoteEntry.js      ← entry is here
├── main.js
└── ...

Key config: MF filename: 'production/remoteEntry.js' — the entry lands in a subdirectory, but @mf-types.zip is emitted to the compiler output root (dist/) instead of alongside the entry (dist/production/).

Root cause: PR #4230 (f8ff0d8cd) removed the zipPrefix logic that used path.dirname(config.filename) to correctly place types alongside the entry file. This is a regression from v0.x.

Tested with @module-federation/enhanced@2.1.0.

Should I revert that change (restore zipPrefix = path.dirname(config.filename)) instead of the current approach in this PR? Happy to adjust the fix direction based on your preference.

@2heal1
Copy link
Copy Markdown
Member

2heal1 commented Mar 9, 2026

i got you . Can you help add docs ?

@ScriptedAlchemy ScriptedAlchemy merged commit 12240bb into module-federation:main Mar 13, 2026
3 of 4 checks passed
@2heal1 2heal1 mentioned this pull request Mar 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants