From 258b87f566dbf0fd9b511d617a6007f30b751e5e Mon Sep 17 00:00:00 2001 From: 2heal1 Date: Thu, 9 Jan 2025 18:12:03 +0800 Subject: [PATCH] chore(dts-plugin): change extractRemoteTypes default value as false --- .changeset/fair-waves-push.md | 5 - .changeset/sixty-spoons-act.md | 5 + packages/dts-plugin/src/plugins/DevPlugin.ts | 17 +- packages/dts-plugin/src/plugins/DtsPlugin.ts | 2 +- .../src/plugins/GenerateTypesPlugin.ts | 177 ++++++++---------- 5 files changed, 84 insertions(+), 122 deletions(-) delete mode 100644 .changeset/fair-waves-push.md create mode 100644 .changeset/sixty-spoons-act.md diff --git a/.changeset/fair-waves-push.md b/.changeset/fair-waves-push.md deleted file mode 100644 index 87cfcd19f1..0000000000 --- a/.changeset/fair-waves-push.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@module-federation/dts-plugin': patch ---- - -fix(dts-plugin): add debounce for emitDevFiles diff --git a/.changeset/sixty-spoons-act.md b/.changeset/sixty-spoons-act.md new file mode 100644 index 0000000000..62f4ad8a62 --- /dev/null +++ b/.changeset/sixty-spoons-act.md @@ -0,0 +1,5 @@ +--- +'@module-federation/dts-plugin': patch +--- + +chore(dts-plugin): change extractRemoteTypes default value as false diff --git a/packages/dts-plugin/src/plugins/DevPlugin.ts b/packages/dts-plugin/src/plugins/DevPlugin.ts index bcc87c11b0..5a9a178e75 100644 --- a/packages/dts-plugin/src/plugins/DevPlugin.ts +++ b/packages/dts-plugin/src/plugins/DevPlugin.ts @@ -36,8 +36,6 @@ export class DevPlugin implements WebpackPluginInstance { readonly name = 'MFDevPlugin'; private _options: moduleFederationPlugin.ModuleFederationPluginOptions; private _devWorker?: DevWorker; - private updateDebounceTimer: NodeJS.Timeout | null = null; - private readonly UPDATE_DEBOUNCE_DELAY = 300; fetchTypesPromise: Promise; constructor( @@ -101,21 +99,8 @@ export class DevPlugin implements WebpackPluginInstance { process.exit(exitCode); } - private _debouncedUpdate() { - if (this.updateDebounceTimer) { - clearTimeout(this.updateDebounceTimer); - } - this.updateDebounceTimer = setTimeout(async () => { - try { - this._devWorker?.update(); - } catch (err) { - console.error(err); - } - }, this.UPDATE_DEBOUNCE_DELAY); - } - private _afterEmit(): void { - this._debouncedUpdate(); + this._devWorker?.update(); } apply(compiler: Compiler): void { diff --git a/packages/dts-plugin/src/plugins/DtsPlugin.ts b/packages/dts-plugin/src/plugins/DtsPlugin.ts index 0c2835f806..891095affe 100644 --- a/packages/dts-plugin/src/plugins/DtsPlugin.ts +++ b/packages/dts-plugin/src/plugins/DtsPlugin.ts @@ -21,7 +21,7 @@ export class DtsPlugin implements WebpackPluginInstance { compileInChildProcess: true, abortOnError: false, extractThirdParty: true, - extractRemoteTypes: true, + extractRemoteTypes: false, }; const defaultConsumeTypes = { abortOnError: false, consumeAPITypes: true }; const normalizedDtsOptions = diff --git a/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts b/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts index 5db7284fd0..3738b784b1 100644 --- a/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts +++ b/packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts @@ -21,11 +21,6 @@ export class GenerateTypesPlugin implements WebpackPluginInstance { consumeTypesPromise: Promise; callback: () => void; - private debounceTimer: NodeJS.Timeout | null = null; - private isProcessing = false; - private pendingTask = false; - private readonly DEBOUNCE_DELAY = 300; - constructor( pluginOptions: moduleFederationPlugin.ModuleFederationPluginOptions, dtsOptions: moduleFederationPlugin.PluginDtsOptions, @@ -94,105 +89,87 @@ export class GenerateTypesPlugin implements WebpackPluginInstance { const generateTypesFn = getGenerateTypesFn(); let compiledOnce = false; - const debouncedEmitTypesFilesDev = async () => { - if (this.isProcessing) { - this.pendingTask = true; - return; - } - if (this.debounceTimer) { - clearTimeout(this.debounceTimer); - } - this.debounceTimer = setTimeout(async () => { - try { - this.isProcessing = true; - if (!isDev()) { - return; - } - const { zipTypesPath, apiTypesPath, zipName, apiFileName } = - retrieveTypesAssetsInfo(finalOptions.remote); - - await generateTypesFn(finalOptions); - const config = finalOptions.remote.moduleFederationConfig; - let zipPrefix = ''; - if (typeof config.manifest === 'object' && config.manifest.filePath) { - zipPrefix = config.manifest.filePath; - } else if ( - typeof config.manifest === 'object' && - config.manifest.fileName - ) { - zipPrefix = path.dirname(config.manifest.fileName); - } else if (config.filename) { - zipPrefix = path.dirname(config.filename); - } + const emitTypesFilesDev = async () => { + try { + if (!isDev()) { + return; + } + const { zipTypesPath, apiTypesPath, zipName, apiFileName } = + retrieveTypesAssetsInfo(finalOptions.remote); + + await generateTypesFn(finalOptions); + const config = finalOptions.remote.moduleFederationConfig; + let zipPrefix = ''; + if (typeof config.manifest === 'object' && config.manifest.filePath) { + zipPrefix = config.manifest.filePath; + } else if ( + typeof config.manifest === 'object' && + config.manifest.fileName + ) { + zipPrefix = path.dirname(config.manifest.fileName); + } else if (config.filename) { + zipPrefix = path.dirname(config.filename); + } - if (zipTypesPath) { - const zipContent = fs.readFileSync(zipTypesPath); - const zipOutputPath = path.join( - compiler.outputPath, - zipPrefix, - zipName, + if (zipTypesPath) { + const zipContent = fs.readFileSync(zipTypesPath); + const zipOutputPath = path.join( + compiler.outputPath, + zipPrefix, + zipName, + ); + + await new Promise((resolve, reject) => { + compiler.outputFileSystem.mkdir( + path.dirname(zipOutputPath), + (err) => { + if (err && err.code !== 'EEXIST') { + reject(err); + } else { + compiler.outputFileSystem.writeFile( + zipOutputPath, + zipContent, + (writeErr) => { + if (writeErr) reject(writeErr); + else resolve(); + }, + ); + } + }, ); + }); + } - await new Promise((resolve, reject) => { - compiler.outputFileSystem.mkdir( - path.dirname(zipOutputPath), - (err) => { - if (err && err.code !== 'EEXIST') { - reject(err); - } else { - compiler.outputFileSystem.writeFile( - zipOutputPath, - zipContent, - (writeErr) => { - if (writeErr) reject(writeErr); - else resolve(); - }, - ); - } - }, - ); - }); - } - - if (apiTypesPath) { - const apiContent = fs.readFileSync(apiTypesPath); - const apiOutputPath = path.join( - compiler.outputPath, - zipPrefix, - apiFileName, + if (apiTypesPath) { + const apiContent = fs.readFileSync(apiTypesPath); + const apiOutputPath = path.join( + compiler.outputPath, + zipPrefix, + apiFileName, + ); + await new Promise((resolve, reject) => { + compiler.outputFileSystem.mkdir( + path.dirname(apiOutputPath), + (err) => { + if (err && err.code !== 'EEXIST') { + reject(err); + } else { + compiler.outputFileSystem.writeFile( + apiOutputPath, + apiContent, + (writeErr) => { + if (writeErr) reject(writeErr); + else resolve(); + }, + ); + } + }, ); - await new Promise((resolve, reject) => { - compiler.outputFileSystem.mkdir( - path.dirname(apiOutputPath), - (err) => { - if (err && err.code !== 'EEXIST') { - reject(err); - } else { - compiler.outputFileSystem.writeFile( - apiOutputPath, - apiContent, - (writeErr) => { - if (writeErr) reject(writeErr); - else resolve(); - }, - ); - } - }, - ); - }); - } - } catch (err) { - console.error(err); - } finally { - this.isProcessing = false; - this.debounceTimer = null; - - if (this.pendingTask) { - this.pendingTask = false; - await debouncedEmitTypesFilesDev(); - } + }); } - }, this.DEBOUNCE_DELAY); + } catch (err) { + console.error(err); + } }; compiler.hooks.thisCompilation.tap('mf:generateTypes', (compilation) => { @@ -211,7 +188,7 @@ export class GenerateTypesPlugin implements WebpackPluginInstance { } if (compiledOnce) { - debouncedEmitTypesFilesDev(); + emitTypesFilesDev(); return; }