Skip to content

Commit 4562db4

Browse files
chore: type fixes to next mf (#2907)
Co-authored-by: ScriptedAlchemy <[email protected]>
1 parent 9cc1edb commit 4562db4

File tree

6 files changed

+147
-23
lines changed

6 files changed

+147
-23
lines changed

packages/manifest/src/StatsManager.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ class StatsManager {
6464
get fileName(): string {
6565
return getFileName(this._options.manifest).statsFileName;
6666
}
67-
6867
private _getMetaData(
6968
compiler: Compiler,
7069
compilation: Compilation,

packages/nextjs-mf/src/loaders/helpers.ts

Lines changed: 142 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import type { RuleSetRuleUnion, Loader } from '@module-federation/utilities';
1+
import type {
2+
RuleSetRule,
3+
RuleSetCondition,
4+
RuleSetConditionAbsolute,
5+
RuleSetUseItem,
6+
} from 'webpack';
27

3-
/**
4-
* This function injects a loader into the current module rule.
5-
* Note: This function mutates the `rule` argument.
6-
*
7-
* @param {RuleSetRuleUnion} rule - The current module rule.
8-
* @param {Loader} [loader={}] - The loader to be injected.
9-
*/
10-
export function injectRuleLoader(rule: RuleSetRuleUnion, loader: Loader = {}) {
8+
export function injectRuleLoader(rule: any, loader: RuleSetUseItem = {}) {
119
if (rule !== '...') {
1210
const _rule = rule as {
1311
loader?: string;
14-
use?: (Loader | string)[];
12+
use?: (RuleSetUseItem | string)[];
1513
options?: any;
1614
};
1715
if (_rule.loader) {
@@ -27,23 +25,23 @@ export function injectRuleLoader(rule: RuleSetRuleUnion, loader: Loader = {}) {
2725
/**
2826
* This function checks if the current module rule has a loader with the provided name.
2927
*
30-
* @param {RuleSetRuleUnion} rule - The current module rule.
28+
* @param {RuleSetRule} rule - The current module rule.
3129
* @param {string} loaderName - The name of the loader to check.
3230
* @returns {boolean} Returns true if the current module rule has a loader with the provided name, otherwise false.
3331
*/
34-
export function hasLoader(rule: RuleSetRuleUnion, loaderName: string) {
32+
export function hasLoader(rule: RuleSetRule, loaderName: string) {
33+
//@ts-ignore
3534
if (rule !== '...') {
3635
const _rule = rule as {
3736
loader?: string;
38-
use?: (Loader | string)[];
37+
use?: (RuleSetUseItem | string)[];
3938
options?: any;
4039
};
4140
if (_rule.loader === loaderName) {
4241
return true;
4342
} else if (_rule.use && Array.isArray(_rule.use)) {
4443
for (let i = 0; i < _rule.use.length; i++) {
4544
const loader = _rule.use[i];
46-
// check exact name, eg "url-loader" or its path "node_modules/url-loader/dist/cjs.js"
4745
if (
4846
typeof loader !== 'string' &&
4947
typeof loader !== 'function' &&
@@ -62,3 +60,133 @@ export function hasLoader(rule: RuleSetRuleUnion, loaderName: string) {
6260
}
6361
return false;
6462
}
63+
64+
interface Resource {
65+
path: string;
66+
layer?: string;
67+
issuerLayer?: string;
68+
}
69+
70+
function matchesCondition(
71+
condition:
72+
| RuleSetCondition
73+
| RuleSetConditionAbsolute
74+
| RuleSetRule
75+
| undefined,
76+
resource: Resource,
77+
currentPath: string,
78+
): boolean {
79+
if (condition instanceof RegExp) {
80+
return condition.test(resource.path);
81+
} else if (typeof condition === 'string') {
82+
return resource.path.includes(condition);
83+
} else if (typeof condition === 'function') {
84+
return condition(resource.path);
85+
} else if (typeof condition === 'object') {
86+
if ('test' in condition && condition.test) {
87+
const tests = Array.isArray(condition.test)
88+
? condition.test
89+
: [condition.test];
90+
if (
91+
!tests.some((test: RuleSetCondition) =>
92+
matchesCondition(test, resource, currentPath),
93+
)
94+
) {
95+
return false;
96+
}
97+
}
98+
if ('include' in condition && condition.include) {
99+
const includes = Array.isArray(condition.include)
100+
? condition.include
101+
: [condition.include];
102+
if (
103+
!includes.some((include: RuleSetCondition) =>
104+
matchesCondition(include, resource, currentPath),
105+
)
106+
) {
107+
return false;
108+
}
109+
}
110+
if ('exclude' in condition && condition.exclude) {
111+
const excludes = Array.isArray(condition.exclude)
112+
? condition.exclude
113+
: [condition.exclude];
114+
if (
115+
excludes.some((exclude: RuleSetCondition) =>
116+
matchesCondition(exclude, resource, currentPath),
117+
)
118+
) {
119+
return false;
120+
}
121+
}
122+
if ('and' in condition && condition.and) {
123+
return condition.and.every((cond: RuleSetCondition) =>
124+
matchesCondition(cond, resource, currentPath),
125+
);
126+
}
127+
if ('or' in condition && condition.or) {
128+
return condition.or.some((cond: RuleSetCondition) =>
129+
matchesCondition(cond, resource, currentPath),
130+
);
131+
}
132+
if ('not' in condition && condition.not) {
133+
return !matchesCondition(condition.not, resource, currentPath);
134+
}
135+
if ('layer' in condition && condition.layer) {
136+
if (
137+
!resource.layer ||
138+
!matchesCondition(
139+
condition.layer,
140+
{ path: resource.layer },
141+
currentPath,
142+
)
143+
) {
144+
return false;
145+
}
146+
}
147+
if ('issuerLayer' in condition && condition.issuerLayer) {
148+
if (
149+
!resource.issuerLayer ||
150+
!matchesCondition(
151+
condition.issuerLayer,
152+
{ path: resource.issuerLayer },
153+
currentPath,
154+
)
155+
) {
156+
return false;
157+
}
158+
}
159+
}
160+
return true;
161+
}
162+
163+
export function findLoaderForResource(
164+
rules: RuleSetRule[],
165+
resource: Resource,
166+
path: string[] = [],
167+
): RuleSetRule | null {
168+
let lastMatchedRule: RuleSetRule | null = null;
169+
for (let i = 0; i < rules.length; i++) {
170+
const rule = rules[i];
171+
const currentPath = [...path, `rules[${i}]`];
172+
if (rule.oneOf) {
173+
for (let j = 0; j < rule.oneOf.length; j++) {
174+
const subRule = rule.oneOf[j];
175+
const subPath = [...currentPath, `oneOf[${j}]`];
176+
177+
if (
178+
subRule &&
179+
matchesCondition(subRule, resource, subPath.join('->'))
180+
) {
181+
return subRule;
182+
}
183+
}
184+
} else if (
185+
rule &&
186+
matchesCondition(rule, resource, currentPath.join(' -> '))
187+
) {
188+
lastMatchedRule = rule;
189+
}
190+
}
191+
return lastMatchedRule;
192+
}

packages/nextjs-mf/src/plugins/NextFederationPlugin/set-options.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {
2-
ModuleFederationPluginOptions,
32
NextFederationPluginExtraOptions,
43
NextFederationPluginOptions,
54
} from '@module-federation/utilities';
6-
5+
import type { moduleFederationPlugin } from '@module-federation/sdk';
76
/**
87
* This function sets the main and extra options for NextFederationPlugin. It splits the options object into
98
* the main options and extra options, and sets default values for any options that are not defined.
@@ -20,7 +19,7 @@ import {
2019
* - debug: false
2120
*/
2221
export function setOptions(options: NextFederationPluginOptions): {
23-
mainOptions: ModuleFederationPluginOptions;
22+
mainOptions: moduleFederationPlugin.ModuleFederationPluginOptions;
2423
extraOptions: NextFederationPluginExtraOptions;
2524
} {
2625
const { extraOptions, ...mainOpts } = options;

packages/nextjs-mf/src/plugins/NextFederationPlugin/validate-options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Compiler } from 'webpack';
2-
import { ModuleFederationPluginOptions } from '@module-federation/utilities';
2+
import type { moduleFederationPlugin } from '@module-federation/sdk';
33

44
/**
55
* Validates the compiler options.
@@ -25,15 +25,15 @@ export function validateCompilerOptions(compiler: Compiler): boolean {
2525
/**
2626
* Validates the NextFederationPlugin options.
2727
*
28-
* @param {ModuleFederationPluginOptions} options - The ModuleFederationPluginOptions instance.
28+
* @param {moduleFederationPlugin.ModuleFederationPluginOptions} options - The ModuleFederationPluginOptions instance.
2929
*
3030
* @throws Will throw an error if the filename option is not defined in the options or if the name option is not specified.
3131
* @remarks
3232
* This function validates the options passed to NextFederationPlugin. It ensures that the filename and name options are defined,
3333
* as they are required for using Module Federation.
3434
*/
3535
export function validatePluginOptions(
36-
options: ModuleFederationPluginOptions,
36+
options: moduleFederationPlugin.ModuleFederationPluginOptions,
3737
): boolean | void {
3838
// Throw an error if the filename option is not defined in the options
3939
if (!options.filename) {

packages/nextjs-mf/src/plugins/container/InvertedContainerPlugin.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Compiler } from 'webpack';
2-
import { ModuleFederationPluginOptions } from './types';
32
import EmbeddedContainerPlugin from './EmbeddedContainerPlugin';
43
import { AsyncBoundaryPlugin } from '@module-federation/enhanced';
54

packages/nextjs-mf/src/plugins/container/InvertedContainerRuntimeModule.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ class InvertedContainerRuntimeModule extends RuntimeModule {
6565
`return innerRemote;`,
6666
]),
6767
`};`,
68-
`${RuntimeGlobals.require}.federation.attachRemote = attachRemote;`,
6968
`attachRemote();`,
7069
]);
7170
}

0 commit comments

Comments
 (0)