-
-
Notifications
You must be signed in to change notification settings - Fork 418
feat(dts-plugin): support custom outputDir for DTS type emission #4487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ScriptedAlchemy
merged 14 commits into
module-federation:main
from
zhouxinyong:feat/dts-custom-output-dir
Mar 13, 2026
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f8d8596
feat(dts-plugin): support custom outputDir for DTS type emission
zhouxinyong dc5e1f0
chore: add changeset for dts-plugin outputDir feature
zhouxinyong 7370922
fix(dts-plugin): guard unsafe relative asset paths
zhouxinyong c5d10e5
Merge branch 'main' into feat/dts-custom-output-dir
ScriptedAlchemy 63c2c40
Merge branch 'main' into feat/dts-custom-output-dir
ScriptedAlchemy 43c26b9
Merge branch 'main' into feat/dts-custom-output-dir
ScriptedAlchemy 4cfb1df
Merge branch 'main' into feat/dts-custom-output-dir
ScriptedAlchemy 285125a
Merge branch 'main' into feat/dts-custom-output-dir
ScriptedAlchemy 67e3434
Merge branch 'main' into feat/dts-custom-output-dir
ScriptedAlchemy e375197
docs(website-new): add dts outputDir docs
ScriptedAlchemy 9e80b44
Merge remote-tracking branch 'github/main' into pr-4487
ScriptedAlchemy 0eb1a1c
Merge branch 'main' into feat/dts-custom-output-dir
2heal1 f7d0cc1
Merge branch 'main' into feat/dts-custom-output-dir
ScriptedAlchemy 3c752b4
Merge branch 'main' into feat/dts-custom-output-dir
ScriptedAlchemy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| '@module-federation/dts-plugin': minor | ||
| '@module-federation/sdk': minor | ||
| --- | ||
|
|
||
| feat(dts-plugin): support custom outputDir for DTS type emission | ||
|
|
||
| Expose the `outputDir` option in `DtsRemoteOptions` so users can configure where `@mf-types.zip` and `@mf-types.d.ts` are emitted. Fix `GenerateTypesPlugin` to use `path.relative()` for correct asset placement in subdirectories. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
packages/dts-plugin/src/plugins/GenerateTypesPlugin.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import path from 'path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { normalizeGenerateTypesOptions } from './GenerateTypesPlugin'; | ||
|
|
||
| describe('GenerateTypesPlugin', () => { | ||
| const basePluginOptions = { | ||
| name: 'testRemote', | ||
| filename: 'remoteEntry.js', | ||
| exposes: { | ||
| './button': './src/components/button', | ||
| }, | ||
| shared: {}, | ||
| }; | ||
|
|
||
| describe('normalizeGenerateTypesOptions', () => { | ||
| it('should use compiler outputDir when user does not set outputDir', () => { | ||
| const result = normalizeGenerateTypesOptions({ | ||
| context: '/project', | ||
| outputDir: 'dist', | ||
| dtsOptions: { | ||
| generateTypes: { | ||
| generateAPITypes: true, | ||
| }, | ||
| consumeTypes: false, | ||
| }, | ||
| pluginOptions: basePluginOptions, | ||
| }); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result!.remote.outputDir).toBe('dist'); | ||
| }); | ||
|
|
||
| it('should allow user outputDir to override compiler outputDir', () => { | ||
| const result = normalizeGenerateTypesOptions({ | ||
| context: '/project', | ||
| outputDir: 'dist', | ||
| dtsOptions: { | ||
| generateTypes: { | ||
| generateAPITypes: true, | ||
| outputDir: 'dist/production', | ||
| }, | ||
| consumeTypes: false, | ||
| }, | ||
| pluginOptions: basePluginOptions, | ||
| }); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result!.remote.outputDir).toBe('dist/production'); | ||
| }); | ||
|
|
||
| it('should return undefined when generateTypes is false', () => { | ||
| const result = normalizeGenerateTypesOptions({ | ||
| context: '/project', | ||
| outputDir: 'dist', | ||
| dtsOptions: { | ||
| generateTypes: false, | ||
| consumeTypes: false, | ||
| }, | ||
| pluginOptions: basePluginOptions, | ||
| }); | ||
|
|
||
| expect(result).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('asset emission path calculation', () => { | ||
| // These tests verify the path.relative logic used in emitTypesFiles | ||
| // to ensure correct asset names under various outputDir configurations | ||
|
|
||
| it('should compute relative zip path same as basename when outputDir matches compiler output', () => { | ||
| const compilerOutputPath = path.resolve('/project', 'dist'); | ||
| const zipTypesPath = path.resolve('/project', 'dist', '@mf-types.zip'); | ||
|
|
||
| const relZip = path.relative(compilerOutputPath, zipTypesPath); | ||
| expect(relZip).toBe('@mf-types.zip'); | ||
| }); | ||
|
|
||
| it('should compute relative zip path with subdirectory when custom outputDir is deeper', () => { | ||
| const compilerOutputPath = path.resolve('/project', 'dist'); | ||
| const zipTypesPath = path.resolve( | ||
| '/project', | ||
| 'dist', | ||
| 'production', | ||
| '@mf-types.zip', | ||
| ); | ||
|
|
||
| const relZip = path.relative(compilerOutputPath, zipTypesPath); | ||
| expect(relZip).toBe(path.join('production', '@mf-types.zip')); | ||
| }); | ||
|
|
||
| it('should compute relative api types path with subdirectory', () => { | ||
| const compilerOutputPath = path.resolve('/project', 'dist/react'); | ||
| const apiTypesPath = path.resolve( | ||
| '/project', | ||
| 'dist/react/staging', | ||
| '@mf-types.d.ts', | ||
| ); | ||
|
|
||
| const relApi = path.relative(compilerOutputPath, apiTypesPath); | ||
| expect(relApi).toBe(path.join('staging', '@mf-types.d.ts')); | ||
| }); | ||
|
|
||
| it('should fall back to basename when zip is outside compiler output (starts with ..)', () => { | ||
| const compilerOutputPath = path.resolve('/project', 'dist'); | ||
| const zipTypesPath = path.resolve( | ||
| '/other-project', | ||
| 'dist', | ||
| '@mf-types.zip', | ||
| ); | ||
|
|
||
| const relZip = path.relative(compilerOutputPath, zipTypesPath); | ||
| // When the relative path starts with '..', the plugin should fall back to basename | ||
| expect(relZip.startsWith('..')).toBe(true); | ||
|
|
||
| // Verify fallback behavior | ||
| const emitZipName = relZip.startsWith('..') | ||
| ? path.basename(zipTypesPath) | ||
| : relZip; | ||
| expect(emitZipName).toBe('@mf-types.zip'); | ||
| }); | ||
|
|
||
| it('should handle nested deploy environment subdirectories', () => { | ||
| // Simulates: webpack output = dist/react, entry at dist/react/staging/ | ||
| const compilerOutputPath = path.resolve('/project', 'dist/react'); | ||
| const customOutputDir = 'dist/react/staging'; | ||
| const zipTypesPath = path.resolve( | ||
| '/project', | ||
| customOutputDir, | ||
| '@mf-types.zip', | ||
| ); | ||
|
|
||
| const relZip = path.relative(compilerOutputPath, zipTypesPath); | ||
| expect(relZip).toBe(path.join('staging', '@mf-types.zip')); | ||
| expect(relZip.startsWith('..')).toBe(false); | ||
| }); | ||
|
|
||
| it('should handle custom typesFolder with custom outputDir', () => { | ||
| const compilerOutputPath = path.resolve('/project', 'dist'); | ||
| const zipTypesPath = path.resolve( | ||
| '/project', | ||
| 'dist', | ||
| 'production', | ||
| 'my-types.zip', | ||
| ); | ||
|
|
||
| const relZip = path.relative(compilerOutputPath, zipTypesPath); | ||
| expect(relZip).toBe(path.join('production', 'my-types.zip')); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.