Skip to content

Commit 564f378

Browse files
chore: fix webpack options types (module-federation#3039)
1 parent b6eb398 commit 564f378

File tree

9 files changed

+62
-188
lines changed

9 files changed

+62
-188
lines changed

packages/enhanced/src/declarations/plugins/container/WebpackOptions.d.ts

-140
This file was deleted.

packages/enhanced/src/lib/container/ContainerPlugin.ts

+12-13
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,25 @@ import ContainerEntryModuleFactory from './ContainerEntryModuleFactory';
88
import ContainerExposedDependency from './ContainerExposedDependency';
99
import { parseOptions } from './options';
1010
import type {
11-
optimize,
1211
Compiler,
1312
Compilation,
1413
WebpackError,
1514
WebpackPluginInstance,
15+
WebpackPluginFunction,
1616
} from 'webpack';
1717
import type { containerPlugin } from '@module-federation/sdk';
1818
import FederationRuntimePlugin from './runtime/FederationRuntimePlugin';
1919
import FederationModulesPlugin from './runtime/FederationModulesPlugin';
2020
import checkOptions from '../../schemas/container/ContainerPlugin.check';
2121
import schema from '../../schemas/container/ContainerPlugin';
2222
import FederationRuntimeDependency from './runtime/FederationRuntimeDependency';
23+
import type { OptimizationSplitChunksCacheGroup } from 'webpack/lib/optimize/SplitChunksPlugin';
24+
import type { Falsy } from 'webpack/declarations/WebpackOptions';
2325

2426
const ModuleDependency = require(
2527
normalizeWebpackPath('webpack/lib/dependencies/ModuleDependency'),
2628
) as typeof import('webpack/lib/dependencies/ModuleDependency');
2729

28-
type ExcludeUndefined<T> = T extends undefined ? never : T;
29-
type NonUndefined<T> = ExcludeUndefined<T>;
30-
31-
type OptimizationSplitChunksOptions = NonUndefined<
32-
ConstructorParameters<typeof optimize.SplitChunksPlugin>[0]
33-
>;
34-
35-
type CacheGroups = OptimizationSplitChunksOptions['cacheGroups'];
36-
type CacheGroup = NonUndefined<CacheGroups>[string];
37-
3830
const createSchemaValidation = require(
3931
normalizeWebpackPath('webpack/lib/util/create-schema-validation'),
4032
) as typeof import('webpack/lib/util/create-schema-validation');
@@ -83,7 +75,14 @@ class ContainerPlugin {
8375
// container should not be affected by splitChunks
8476
static patchChunkSplit(compiler: Compiler, name: string): void {
8577
const { splitChunks } = compiler.options.optimization;
86-
const patchChunkSplit = (cacheGroup: CacheGroup) => {
78+
const patchChunkSplit = (
79+
cacheGroup:
80+
| string
81+
| false
82+
| ((...args: any[]) => any)
83+
| RegExp
84+
| OptimizationSplitChunksCacheGroup,
85+
) => {
8786
switch (typeof cacheGroup) {
8887
case 'boolean':
8988
case 'string':
@@ -160,7 +159,7 @@ class ContainerPlugin {
160159

161160
apply(compiler: Compiler): void {
162161
const useModuleFederationPlugin = compiler.options.plugins.find(
163-
(p: WebpackPluginInstance) => {
162+
(p: Falsy | WebpackPluginInstance | WebpackPluginFunction) => {
164163
if (typeof p !== 'object' || !p) {
165164
return false;
166165
}

packages/enhanced/src/lib/container/ModuleFederationPlugin.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class ModuleFederationPlugin implements WebpackPluginInstance {
4242
private _patchBundlerConfig(compiler: Compiler): void {
4343
const { name } = this._options;
4444
const MFPluginNum = compiler.options.plugins.filter(
45-
(p: WebpackPluginInstance) => p && p['name'] === 'ModuleFederationPlugin',
45+
(p): p is WebpackPluginInstance =>
46+
!!p && (p as any).name === 'ModuleFederationPlugin',
4647
).length;
4748
if (name && MFPluginNum < 2) {
4849
new compiler.webpack.DefinePlugin({

packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ class FederationRuntimePlugin {
369369
runtimePath = runtimePath.replace('.cjs', '.esm');
370370
}
371371

372-
const alias = compiler.options.resolve.alias || {};
372+
const alias: any = compiler.options.resolve.alias || {};
373373
alias['@module-federation/runtime$'] =
374374
alias['@module-federation/runtime$'] || runtimePath;
375375
alias['@module-federation/runtime-tools$'] =
@@ -384,7 +384,7 @@ class FederationRuntimePlugin {
384384

385385
apply(compiler: Compiler) {
386386
const useModuleFederationPlugin = compiler.options.plugins.find(
387-
(p: WebpackPluginInstance) => {
387+
(p): p is WebpackPluginInstance & { _options?: any } => {
388388
if (typeof p !== 'object' || !p) {
389389
return false;
390390
}
@@ -398,7 +398,7 @@ class FederationRuntimePlugin {
398398
}
399399

400400
const useContainerPlugin = compiler.options.plugins.find(
401-
(p: WebpackPluginInstance) => {
401+
(p): p is WebpackPluginInstance & { _options?: any } => {
402402
if (typeof p !== 'object' || !p) {
403403
return false;
404404
}

packages/enhanced/src/wrapper/ModuleFederationPlugin.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import type { WebpackPluginInstance, Compiler } from 'webpack';
1+
import type {
2+
WebpackPluginInstance,
3+
Compiler,
4+
WebpackPluginFunction,
5+
} from 'webpack';
26
import type { moduleFederationPlugin } from '@module-federation/sdk';
37
import type IModuleFederationPlugin from '../lib/container/ModuleFederationPlugin';
48
import type { ResourceInfo } from '@module-federation/manifest';
9+
import type { Falsy } from 'webpack/declarations/WebpackOptions';
510

611
import { getWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
712
import path from 'node:path';
@@ -23,7 +28,8 @@ export default class ModuleFederationPlugin implements WebpackPluginInstance {
2328
apply(compiler: Compiler) {
2429
if (
2530
!compiler.options.plugins.find(
26-
(p: WebpackPluginInstance) => p && p['name'] === PLUGIN_NAME,
31+
(p: WebpackPluginInstance | WebpackPluginFunction | Falsy) =>
32+
p && (p as WebpackPluginInstance)['name'] === PLUGIN_NAME,
2733
)
2834
) {
2935
compiler.options.plugins.push(this);

packages/nextjs-mf/src/plugins/NextFederationPlugin/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ export class NextFederationPlugin {
126126

127127
private validateOptions(compiler: Compiler): boolean {
128128
const manifestPlugin = compiler.options.plugins.find(
129-
(p: WebpackPluginInstance) =>
130-
p?.constructor.name === 'BuildManifestPlugin',
129+
(p): p is WebpackPluginInstance =>
130+
p?.constructor?.name === 'BuildManifestPlugin',
131131
);
132132

133133
if (manifestPlugin) {

packages/nextjs-mf/src/plugins/NextFederationPlugin/next-fragments.ts

+28-22
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,33 @@ export const applyPathFixes = (
3434
pluginOptions: moduleFederationPlugin.ModuleFederationPluginOptions,
3535
options: any,
3636
) => {
37-
const match = findLoaderForResource(compiler.options.module.rules, {
38-
path: path.join(compiler.context, '/something/thing.js'),
39-
issuerLayer: undefined,
40-
layer: undefined,
41-
});
37+
const match = findLoaderForResource(
38+
compiler.options.module.rules as RuleSetRule[],
39+
{
40+
path: path.join(compiler.context, '/something/thing.js'),
41+
issuerLayer: undefined,
42+
layer: undefined,
43+
},
44+
);
4245

43-
compiler.options.module.rules.forEach((rule: RuleSetRule) => {
44-
// next-image-loader fix which adds remote's hostname to the assets url
45-
if (options.enableImageLoaderFix && hasLoader(rule, 'next-image-loader')) {
46-
injectRuleLoader(rule, {
47-
loader: require.resolve('../../loaders/fixImageLoader'),
48-
});
49-
}
46+
compiler.options.module.rules.forEach((rule) => {
47+
if (typeof rule === 'object' && rule !== null) {
48+
const typedRule = rule as RuleSetRule;
49+
// next-image-loader fix which adds remote's hostname to the assets url
50+
if (
51+
options.enableImageLoaderFix &&
52+
hasLoader(typedRule, 'next-image-loader')
53+
) {
54+
injectRuleLoader(typedRule, {
55+
loader: require.resolve('../../loaders/fixImageLoader'),
56+
});
57+
}
5058

51-
// url-loader fix for which adds remote's hostname to the assets url
52-
if (options.enableUrlLoaderFix && hasLoader(rule, 'url-loader')) {
53-
injectRuleLoader(rule, {
54-
loader: require.resolve('../../loaders/fixUrlLoader'),
55-
});
59+
if (options.enableUrlLoaderFix && hasLoader(typedRule, 'url-loader')) {
60+
injectRuleLoader(typedRule, {
61+
loader: require.resolve('../../loaders/fixUrlLoader'),
62+
});
63+
}
5664
}
5765
});
5866

@@ -80,7 +88,6 @@ export const applyPathFixes = (
8088
matchCopy = { ...match };
8189
}
8290

83-
// Create the first new rule using descriptionData
8491
const descriptionDataRule: RuleSetRule = {
8592
...matchCopy,
8693
descriptionData: {
@@ -90,7 +97,6 @@ export const applyPathFixes = (
9097
include: undefined,
9198
};
9299

93-
// Create the second new rule using test on regex for /runtimePlugin/
94100
const testRule: RuleSetRule = {
95101
...matchCopy,
96102
resourceQuery: /runtimePlugin/,
@@ -99,10 +105,10 @@ export const applyPathFixes = (
99105
};
100106

101107
const oneOfRule = compiler.options.module.rules.find(
102-
(rule: RuleSetRule) => {
103-
return rule && typeof rule === 'object' && 'oneOf' in rule;
108+
(rule): rule is RuleSetRule => {
109+
return !!rule && typeof rule === 'object' && 'oneOf' in rule;
104110
},
105-
) as RuleSetRule;
111+
) as RuleSetRule | undefined;
106112

107113
if (!oneOfRule) {
108114
compiler.options.module.rules.unshift({

packages/typescript/src/plugins/FederatedTypesPlugin.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ export class FederatedTypesPlugin {
3939
);
4040

4141
if (
42-
!compiler.options.plugins.some(
43-
(p: WebpackPluginInstance) =>
44-
SUPPORTED_PLUGINS.indexOf(p?.constructor.name ?? '') !== -1,
45-
)
42+
!compiler.options.plugins
43+
.filter((p): p is WebpackPluginInstance => !!p)
44+
.some(
45+
(p: WebpackPluginInstance) =>
46+
SUPPORTED_PLUGINS.indexOf(p?.constructor.name ?? '') !== -1,
47+
)
4648
) {
4749
this.logger.error(
4850
'Unable to find the Module Federation Plugin, this is plugin no longer provides it by default. Please add it to your webpack config.',

webpack/lib/Compiler.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ declare class Compiler {
6262
resolverFactory: ResolverFactory;
6363
infrastructureLogger?: (arg0: string, arg1: LogTypeEnum, arg2: any[]) => void;
6464
platform: Readonly<PlatformTargetProperties>;
65-
options: WebpackOptionsNormalized;
65+
options: WebpackOptions;
6666
context: string;
6767
requestShortener: RequestShortener;
6868
cache: Cache;

0 commit comments

Comments
 (0)