Skip to content

Commit

Permalink
fix(dts-plugin): dev plugin should apply after generateTypesPromise r…
Browse files Browse the repository at this point in the history
…esolved
  • Loading branch information
2heal1 committed Jan 9, 2025
1 parent 29bedb3 commit ce96460
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
21 changes: 15 additions & 6 deletions packages/dts-plugin/src/plugins/DtsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,34 @@ export class DtsPlugin implements WebpackPluginInstance {
return;
}

let resolve;
let consumeTypesPromiseResolve;
const consumeTypesPromise: Promise<void> = new Promise((resolve) => {
consumeTypesPromiseResolve = resolve;
});

const fetchTypesPromise: Promise<void> = new Promise((res, rej) => {
resolve = res;
let generateTypesPromiseResolve;
const generateTypesPromise: Promise<void> = new Promise((resolve) => {
generateTypesPromiseResolve = resolve;
});

new DevPlugin(options, fetchTypesPromise).apply(compiler);
// Because the plugin will delete dist/@mf-types.zip while generating types, which will be used in GenerateTypesPlugin
// So it should apply after GenerateTypesPlugin
new DevPlugin(options, generateTypesPromise).apply(compiler);

// The exposes files may use remote types, so it need to consume types first, otherwise the generate types will fail
new GenerateTypesPlugin(
options,
normalizedDtsOptions,
defaultGenerateTypes,
fetchTypesPromise,
consumeTypesPromise,
generateTypesPromiseResolve,
).apply(compiler);

new ConsumeTypesPlugin(
options,
normalizedDtsOptions,
defaultConsumeTypes,
resolve,
consumeTypesPromiseResolve,
).apply(compiler);
}
}
23 changes: 17 additions & 6 deletions packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export class GenerateTypesPlugin implements WebpackPluginInstance {
pluginOptions: moduleFederationPlugin.ModuleFederationPluginOptions;
dtsOptions: moduleFederationPlugin.PluginDtsOptions;
defaultOptions: moduleFederationPlugin.DtsRemoteOptions;
fetchTypesPromise: Promise<void>;
consumeTypesPromise: Promise<void>;
callback: () => void;

private debounceTimer: NodeJS.Timeout | null = null;
private isProcessing = false;
Expand All @@ -29,17 +30,24 @@ export class GenerateTypesPlugin implements WebpackPluginInstance {
pluginOptions: moduleFederationPlugin.ModuleFederationPluginOptions,
dtsOptions: moduleFederationPlugin.PluginDtsOptions,
defaultOptions: moduleFederationPlugin.DtsRemoteOptions,
fetchTypesPromise: Promise<void>,
consumeTypesPromise: Promise<void>,
callback: () => void,
) {
this.pluginOptions = pluginOptions;
this.dtsOptions = dtsOptions;
this.defaultOptions = defaultOptions;
this.fetchTypesPromise = fetchTypesPromise;
this.consumeTypesPromise = consumeTypesPromise;
this.callback = callback;
}

apply(compiler: Compiler) {
const { dtsOptions, defaultOptions, pluginOptions, fetchTypesPromise } =
this;
const {
dtsOptions,
defaultOptions,
pluginOptions,
consumeTypesPromise,
callback,
} = this;

const normalizedGenerateTypes =
normalizeOptions<moduleFederationPlugin.DtsRemoteOptions>(
Expand All @@ -49,6 +57,7 @@ export class GenerateTypesPlugin implements WebpackPluginInstance {
)(dtsOptions.generateTypes);

if (!normalizedGenerateTypes) {
callback();
return;
}

Expand Down Expand Up @@ -198,7 +207,7 @@ export class GenerateTypesPlugin implements WebpackPluginInstance {
compilation.constructor.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER,
},
async () => {
await fetchTypesPromise;
await consumeTypesPromise;
try {
if (pluginOptions.dev === false && compiledOnce) {
return;
Expand Down Expand Up @@ -254,7 +263,9 @@ export class GenerateTypesPlugin implements WebpackPluginInstance {
);
}
compiledOnce = true;
callback();
} catch (err) {
callback();
console.error('Error in mf:generateTypes processAssets hook:', err);
}
},
Expand Down

0 comments on commit ce96460

Please sign in to comment.