Skip to content

Commit 6e3afc6

Browse files
authored
fix(dts-plugin): optimize dev behavior (#3424)
1 parent 1f82d59 commit 6e3afc6

20 files changed

+245
-198
lines changed

.changeset/afraid-students-help.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/dts-plugin': patch
3+
---
4+
5+
feat(dts-plugin): support pass headers when request types url

.changeset/large-dogs-laugh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/dts-plugin': patch
3+
---
4+
5+
fix(dts-plugin): set outputDir default value

.changeset/light-islands-juggle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/dts-plugin': patch
3+
---
4+
5+
fix(dts-plugin): use remoteTarPath first to fetch hot types

.changeset/nine-toes-smell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/dts-plugin': patch
3+
---
4+
5+
fix(dts-plugin): only consume types in dev

.changeset/rare-cobras-beg.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/dts-plugin': patch
3+
---
4+
5+
fix(dts-plugin): throw error while downloading types archive hit historyApiFallback

.changeset/sour-dolls-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/dts-plugin': patch
3+
---
4+
5+
fix(dts-plugin): generateTypes should after consumeTypes finished

.changeset/swift-deers-clap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/enhanced': patch
3+
---
4+
5+
fix(enhanced): no push ModuleFederationPlugin self

.changeset/tame-numbers-sneeze.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/dts-plugin': patch
3+
---
4+
5+
fix(dts-plugin): dev plugin should apply after fetchPromise resolved

packages/dts-plugin/src/core/lib/DTSManager.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class DTSManager {
166166
logger.success('Federated types created correctly');
167167
} catch (error) {
168168
if (this.options.remote?.abortOnError === false) {
169-
logger.error(`Unable to compile federated types, ${error}`);
169+
logger.error(`Unable to compile federated types${error}`);
170170
} else {
171171
throw error;
172172
}
@@ -263,10 +263,11 @@ class DTSManager {
263263
const filePath = path.join(destinationPath, REMOTE_API_TYPES_FILE_NAME);
264264
fs.writeFileSync(filePath, apiTypeFile);
265265
this.loadedRemoteAPIAlias.add(remoteInfo.alias);
266+
fileLog(`success`, 'downloadAPITypes', 'info');
266267
} catch (err) {
267268
fileLog(
268269
`Unable to download "${remoteInfo.name}" api types, ${err}`,
269-
'consumeTargetRemotes',
270+
'downloadAPITypes',
270271
'error',
271272
);
272273
}
@@ -420,17 +421,17 @@ class DTSManager {
420421

421422
async updateTypes(options: UpdateTypesOptions): Promise<void> {
422423
try {
423-
// can use remoteTarPath directly in the future
424424
const {
425425
remoteName,
426426
updateMode,
427+
remoteTarPath,
427428
remoteInfo: updatedRemoteInfo,
428429
once,
429430
} = options;
430431
const hostName = this.options?.host?.moduleFederationConfig?.name;
431432
fileLog(
432-
`updateTypes options:, ${JSON.stringify(options, null, 2)}`,
433-
'consumeTypes',
433+
`options: ${JSON.stringify(options, null, 2)};\nhostName: ${hostName}`,
434+
'updateTypes',
434435
'info',
435436
);
436437
if (updateMode === UpdateMode.POSITIVE && remoteName === hostName) {
@@ -454,19 +455,39 @@ class DTSManager {
454455
const consumeTypes = async (
455456
requiredRemoteInfo: Required<RemoteInfo>,
456457
) => {
458+
fileLog(`consumeTypes start`, 'updateTypes', 'info');
459+
if (!requiredRemoteInfo.zipUrl) {
460+
throw new Error(
461+
`Can not get ${requiredRemoteInfo.name}'s types archive url!`,
462+
);
463+
}
457464
const [_alias, destinationPath] = await this.consumeTargetRemotes(
458465
hostOptions,
459-
requiredRemoteInfo,
466+
{
467+
...requiredRemoteInfo,
468+
// use remoteTarPath first
469+
zipUrl: remoteTarPath || requiredRemoteInfo.zipUrl,
470+
},
460471
);
461472
await this.downloadAPITypes(requiredRemoteInfo, destinationPath);
473+
fileLog(`consumeTypes end`, 'updateTypes', 'info');
462474
};
463-
475+
fileLog(
476+
`loadedRemoteInfo: ${JSON.stringify(loadedRemoteInfo, null, 2)}`,
477+
'updateTypes',
478+
'info',
479+
);
464480
if (!loadedRemoteInfo) {
465481
const remoteInfo = Object.values(mapRemotesToDownload).find(
466482
(item) => {
467483
return item.name === remoteName;
468484
},
469485
);
486+
fileLog(
487+
`remoteInfo: ${JSON.stringify(remoteInfo, null, 2)}`,
488+
'updateTypes',
489+
'info',
490+
);
470491
if (remoteInfo) {
471492
if (!this.remoteAliasMap[remoteInfo.alias]) {
472493
const requiredRemoteInfo =
@@ -496,7 +517,7 @@ class DTSManager {
496517
null,
497518
2,
498519
)}`,
499-
'consumeTypes',
520+
'updateTypes',
500521
'info',
501522
);
502523
await consumeDynamicRemoteTypes();

packages/dts-plugin/src/core/lib/archiveHandler.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ export const downloadTypesArchive = (hostOptions: Required<HostOptions>) => {
6363
const response = await axiosGet(url, {
6464
responseType: 'arraybuffer',
6565
}).catch(downloadErrorLogger(destinationFolder, url));
66+
if (
67+
typeof response.headers?.['content-type'] === 'string' &&
68+
response.headers['content-type'].includes('text/html')
69+
) {
70+
throw new Error(
71+
`${url} receives invalid content-type: ${response.headers['content-type']}`,
72+
);
73+
}
6674

6775
try {
6876
if (hostOptions.deleteTypesFolder) {
@@ -81,6 +89,11 @@ export const downloadTypesArchive = (hostOptions: Required<HostOptions>) => {
8189

8290
const zip = new AdmZip(Buffer.from(response.data));
8391
zip.extractAllTo(destinationPath, true);
92+
fileLog(
93+
`zip.extractAllTo success destinationPath: ${destinationPath}; url: ${url}`,
94+
'downloadTypesArchive',
95+
'info',
96+
);
8497
return [destinationFolder, destinationPath];
8598
} catch (error: any) {
8699
fileLog(

0 commit comments

Comments
 (0)