Skip to content

Commit ce6d238

Browse files
fix(enhanced): preserve treeshake fixture build ids
1 parent 5764c79 commit ce6d238

File tree

3 files changed

+169
-177
lines changed

3 files changed

+169
-177
lines changed

packages/enhanced/src/index.ts

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,18 @@
11
import type { moduleFederationPlugin } from '@module-federation/sdk';
2-
import * as ModuleFederationPluginModule from './wrapper/ModuleFederationPlugin';
3-
import * as ContainerReferencePluginModule from './wrapper/ContainerReferencePlugin';
4-
import * as SharePluginModule from './wrapper/SharePlugin';
5-
import * as ContainerPluginModule from './wrapper/ContainerPlugin';
6-
import * as ConsumeSharedPluginModule from './wrapper/ConsumeSharedPlugin';
7-
import * as ProvideSharedPluginModule from './wrapper/ProvideSharedPlugin';
8-
import * as FederationModulesPluginModule from './wrapper/FederationModulesPlugin';
9-
import * as FederationRuntimePluginModule from './wrapper/FederationRuntimePlugin';
10-
import * as AsyncBoundaryPluginModule from './wrapper/AsyncBoundaryPlugin';
11-
import * as HoistContainerReferencesPluginModule from './wrapper/HoistContainerReferencesPlugin';
12-
import * as TreeShakingSharedPluginModule from './wrapper/TreeShakingSharedPlugin';
13-
14-
const pickDefault = <T>(mod: T): T extends { default: infer U } ? U : T =>
15-
((mod as { default?: unknown }).default ?? mod) as T extends {
16-
default: infer U;
17-
}
18-
? U
19-
: T;
20-
21-
const ModuleFederationPlugin = pickDefault(ModuleFederationPluginModule);
22-
const { PLUGIN_NAME } = ModuleFederationPluginModule;
23-
const ContainerReferencePlugin = pickDefault(ContainerReferencePluginModule);
24-
const SharePlugin = pickDefault(SharePluginModule);
25-
const ContainerPlugin = pickDefault(ContainerPluginModule);
26-
const ConsumeSharedPlugin = pickDefault(ConsumeSharedPluginModule);
27-
const ProvideSharedPlugin = pickDefault(ProvideSharedPluginModule);
28-
const FederationModulesPlugin = pickDefault(FederationModulesPluginModule);
29-
const FederationRuntimePlugin = pickDefault(FederationRuntimePluginModule);
30-
const AsyncBoundaryPlugin = pickDefault(AsyncBoundaryPluginModule);
31-
const HoistContainerReferencesPlugin = pickDefault(
32-
HoistContainerReferencesPluginModule,
33-
);
34-
const TreeShakingSharedPlugin = pickDefault(TreeShakingSharedPluginModule);
35-
362
export {
37-
ModuleFederationPlugin,
3+
default as ModuleFederationPlugin,
384
PLUGIN_NAME,
39-
ContainerReferencePlugin,
40-
SharePlugin,
41-
ContainerPlugin,
42-
ConsumeSharedPlugin,
43-
ProvideSharedPlugin,
44-
FederationModulesPlugin,
45-
FederationRuntimePlugin,
46-
AsyncBoundaryPlugin,
47-
HoistContainerReferencesPlugin,
48-
TreeShakingSharedPlugin,
49-
};
5+
} from './wrapper/ModuleFederationPlugin';
6+
export { default as ContainerReferencePlugin } from './wrapper/ContainerReferencePlugin';
7+
export { default as SharePlugin } from './wrapper/SharePlugin';
8+
export { default as ContainerPlugin } from './wrapper/ContainerPlugin';
9+
export { default as ConsumeSharedPlugin } from './wrapper/ConsumeSharedPlugin';
10+
export { default as ProvideSharedPlugin } from './wrapper/ProvideSharedPlugin';
11+
export { default as FederationModulesPlugin } from './wrapper/FederationModulesPlugin';
12+
export { default as FederationRuntimePlugin } from './wrapper/FederationRuntimePlugin';
13+
export { default as AsyncBoundaryPlugin } from './wrapper/AsyncBoundaryPlugin';
14+
export { default as HoistContainerReferencesPlugin } from './wrapper/HoistContainerReferencesPlugin';
15+
export { default as TreeShakingSharedPlugin } from './wrapper/TreeShakingSharedPlugin';
5016

5117
export const dependencies = {
5218
get ContainerEntryDependency() {

packages/enhanced/src/webpack.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
import * as ModuleFederationPluginModule from './wrapper/ModuleFederationPlugin';
2-
3-
const pickDefault = <T>(mod: T): T extends { default: infer U } ? U : T =>
4-
((mod as { default?: unknown }).default ?? mod) as T extends {
5-
default: infer U;
6-
}
7-
? U
8-
: T;
9-
10-
const ModuleFederationPlugin = pickDefault(ModuleFederationPluginModule);
11-
const { PLUGIN_NAME } = ModuleFederationPluginModule;
1+
import {
2+
default as ModuleFederationPlugin,
3+
PLUGIN_NAME,
4+
} from './wrapper/ModuleFederationPlugin';
125

136
export { ModuleFederationPlugin, PLUGIN_NAME };

packages/enhanced/test/ConfigTestCases.rstest.ts

Lines changed: 153 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,28 @@ export const describeCases = (config: any) => {
346346
ensureTreeShakingFixtures(testDirectory);
347347
}
348348
};
349+
const withLegacyTreeShakingBuildVersion = async <T>(
350+
run: () => Promise<T>,
351+
): Promise<T> => {
352+
if (
353+
!testDirectory.includes(
354+
`${path.sep}tree-shaking-share${path.sep}`,
355+
)
356+
) {
357+
return run();
358+
}
359+
const previousBuildVersion = process.env.MF_BUILD_VERSION;
360+
process.env.MF_BUILD_VERSION = '0.0.0';
361+
try {
362+
return await run();
363+
} finally {
364+
if (previousBuildVersion === undefined) {
365+
delete process.env.MF_BUILD_VERSION;
366+
} else {
367+
process.env.MF_BUILD_VERSION = previousBuildVersion;
368+
}
369+
}
370+
};
349371

350372
beforeAll(() => {
351373
if (
@@ -503,46 +525,49 @@ export const describeCases = (config: any) => {
503525
await new Promise<void>((resolve, reject) => {
504526
try {
505527
// 单次 run
506-
require('webpack')(options, (err: any) => {
507-
const stderrOutput = stderr.toString();
508-
const { unhandled, results: infrastructureLogErrors } =
509-
collectInfrastructureOutputs(
510-
infraStructureLog,
511-
stderrOutput,
512-
{ run: 1, options },
513-
);
514-
stderr.reset();
515-
if (unhandled.length) {
516-
reject(
517-
new Error(
518-
'Errors/Warnings during build:\n' +
519-
unhandled.join('\n'),
520-
),
521-
);
522-
return;
523-
}
524-
if (
525-
infrastructureLogErrors.length &&
526-
checkArrayExpectation(
527-
testDirectory,
528-
{ infrastructureLogs: infrastructureLogErrors },
529-
'infrastructureLog',
530-
'infrastructure-log',
531-
'InfrastructureLog',
532-
(e: any) => {
533-
throw e;
534-
},
535-
)
536-
) {
528+
withLegacyTreeShakingBuildVersion(async () => {
529+
require('webpack')(options, (err: any) => {
530+
const stderrOutput = stderr.toString();
531+
const { unhandled, results: infrastructureLogErrors } =
532+
collectInfrastructureOutputs(
533+
infraStructureLog,
534+
stderrOutput,
535+
{ run: 1, options },
536+
);
537+
stderr.reset();
538+
if (unhandled.length) {
539+
reject(
540+
new Error(
541+
'Errors/Warnings during build:\n' +
542+
unhandled.join('\n'),
543+
),
544+
);
545+
return;
546+
}
547+
if (
548+
infrastructureLogErrors.length &&
549+
checkArrayExpectation(
550+
testDirectory,
551+
{ infrastructureLogs: infrastructureLogErrors },
552+
'infrastructureLog',
553+
'infrastructure-log',
554+
'InfrastructureLog',
555+
(e: any) => {
556+
throw e;
557+
},
558+
)
559+
) {
560+
resolve();
561+
return;
562+
}
563+
if (err) {
564+
reject(err);
565+
return;
566+
}
537567
resolve();
538-
return;
539-
}
540-
if (err) {
541-
reject(err);
542-
return;
543-
}
544-
resolve();
545-
});
568+
});
569+
return undefined as void;
570+
}).catch(reject);
546571
} catch (e: any) {
547572
reject(e);
548573
}
@@ -556,73 +581,78 @@ export const describeCases = (config: any) => {
556581
infraStructureLog.length = 0;
557582
await new Promise<void>((resolve, reject) => {
558583
try {
559-
require('webpack')(options, (err: any, stats: any) => {
560-
if (err) {
561-
reject(err);
562-
return;
563-
}
564-
const { modules, children, errorsCount } = stats.toJson({
565-
all: false,
566-
modules: true,
567-
errorsCount: true,
568-
});
569-
const stderrOutput = stderr.toString();
570-
const { unhandled, results: infrastructureLogErrors } =
571-
collectInfrastructureOutputs(
572-
infraStructureLog,
573-
stderrOutput,
574-
{ run: 2, options },
575-
);
576-
stderr.reset();
577-
if (errorsCount === 0) {
578-
if (unhandled.length) {
579-
reject(
580-
new Error(
581-
'Errors/Warnings during build:\n' +
582-
unhandled.join('\n'),
583-
),
584-
);
584+
withLegacyTreeShakingBuildVersion(async () => {
585+
require('webpack')(options, (err: any, stats: any) => {
586+
if (err) {
587+
reject(err);
585588
return;
586589
}
587-
const allModules = children
588-
? children.reduce(
589-
(all: any[], { modules }: any) =>
590-
all.concat(modules),
591-
modules || [],
590+
const { modules, children, errorsCount } = stats.toJson(
591+
{
592+
all: false,
593+
modules: true,
594+
errorsCount: true,
595+
},
596+
);
597+
const stderrOutput = stderr.toString();
598+
const { unhandled, results: infrastructureLogErrors } =
599+
collectInfrastructureOutputs(
600+
infraStructureLog,
601+
stderrOutput,
602+
{ run: 2, options },
603+
);
604+
stderr.reset();
605+
if (errorsCount === 0) {
606+
if (unhandled.length) {
607+
reject(
608+
new Error(
609+
'Errors/Warnings during build:\n' +
610+
unhandled.join('\n'),
611+
),
612+
);
613+
return;
614+
}
615+
const allModules = children
616+
? children.reduce(
617+
(all: any[], { modules }: any) =>
618+
all.concat(modules),
619+
modules || [],
620+
)
621+
: modules;
622+
if (
623+
allModules.some(
624+
(m: any) =>
625+
m.type !== 'cached modules' && !m.cached,
592626
)
593-
: modules;
627+
) {
628+
reject(
629+
new Error(
630+
`Some modules were not cached:\n${stats.toString({ all: false, modules: true, modulesSpace: 100 })}`,
631+
),
632+
);
633+
return;
634+
}
635+
}
594636
if (
595-
allModules.some(
596-
(m: any) =>
597-
m.type !== 'cached modules' && !m.cached,
637+
infrastructureLogErrors.length &&
638+
checkArrayExpectation(
639+
testDirectory,
640+
{ infrastructureLogs: infrastructureLogErrors },
641+
'infrastructureLog',
642+
'infrastructure-log',
643+
'InfrastructureLog',
644+
(e: any) => {
645+
throw e;
646+
},
598647
)
599648
) {
600-
reject(
601-
new Error(
602-
`Some modules were not cached:\n${stats.toString({ all: false, modules: true, modulesSpace: 100 })}`,
603-
),
604-
);
649+
resolve();
605650
return;
606651
}
607-
}
608-
if (
609-
infrastructureLogErrors.length &&
610-
checkArrayExpectation(
611-
testDirectory,
612-
{ infrastructureLogs: infrastructureLogErrors },
613-
'infrastructureLog',
614-
'infrastructure-log',
615-
'InfrastructureLog',
616-
(e: any) => {
617-
throw e;
618-
},
619-
)
620-
) {
621652
resolve();
622-
return;
623-
}
624-
resolve();
625-
});
653+
});
654+
return undefined as void;
655+
}).catch(reject);
626656
} catch (e: any) {
627657
reject(e);
628658
}
@@ -655,31 +685,34 @@ export const describeCases = (config: any) => {
655685
const runWebpackCompile = async () => {
656686
infraStructureLog.length = 0;
657687
stderr.reset();
658-
return new Promise<{ stats: any }>((resolve, reject) => {
659-
const onCompiled = (err: any, stats: any) => {
660-
if (err) return reject(err);
661-
resolve({ stats });
662-
};
663-
try {
664-
if (config.cache) {
665-
const compiler = require('webpack')(options);
666-
compiler.run((err: any) => {
688+
return withLegacyTreeShakingBuildVersion(
689+
() =>
690+
new Promise<{ stats: any }>((resolve, reject) => {
691+
const onCompiled = (err: any, stats: any) => {
667692
if (err) return reject(err);
668-
compiler.run((error: any, stats: any) => {
669-
compiler.close((cerr: any) => {
670-
if (cerr) return reject(cerr);
671-
if (error) return reject(error);
672-
resolve({ stats });
693+
resolve({ stats });
694+
};
695+
try {
696+
if (config.cache) {
697+
const compiler = require('webpack')(options);
698+
compiler.run((err: any) => {
699+
if (err) return reject(err);
700+
compiler.run((error: any, stats: any) => {
701+
compiler.close((cerr: any) => {
702+
if (cerr) return reject(cerr);
703+
if (error) return reject(error);
704+
resolve({ stats });
705+
});
706+
});
673707
});
674-
});
675-
});
676-
} else {
677-
require('webpack')(options, onCompiled);
678-
}
679-
} catch (e: any) {
680-
reject(e);
681-
}
682-
});
708+
} else {
709+
require('webpack')(options, onCompiled);
710+
}
711+
} catch (e: any) {
712+
reject(e);
713+
}
714+
}),
715+
);
683716
};
684717

685718
cleanOutputDirectory();

0 commit comments

Comments
 (0)