Skip to content

Commit

Permalink
chore(dts-plugin): change extractRemoteTypes default value as false
Browse files Browse the repository at this point in the history
  • Loading branch information
2heal1 committed Jan 9, 2025
1 parent f7c7d0b commit 258b87f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 122 deletions.
5 changes: 0 additions & 5 deletions .changeset/fair-waves-push.md

This file was deleted.

5 changes: 5 additions & 0 deletions .changeset/sixty-spoons-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/dts-plugin': patch
---

chore(dts-plugin): change extractRemoteTypes default value as false
17 changes: 1 addition & 16 deletions packages/dts-plugin/src/plugins/DevPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;

constructor(
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/dts-plugin/src/plugins/DtsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
177 changes: 77 additions & 100 deletions packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ export class GenerateTypesPlugin implements WebpackPluginInstance {
consumeTypesPromise: Promise<void>;
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,
Expand Down Expand Up @@ -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<void>((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<void>((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<void>((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<void>((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) => {
Expand All @@ -211,7 +188,7 @@ export class GenerateTypesPlugin implements WebpackPluginInstance {
}

if (compiledOnce) {
debouncedEmitTypesFilesDev();
emitTypesFilesDev();
return;
}

Expand Down

0 comments on commit 258b87f

Please sign in to comment.