Skip to content

Commit e10725f

Browse files
authored
fix: override watchOptions.ignored if the modernjs internal value is regexp (#3291)
1 parent 734df18 commit e10725f

File tree

9 files changed

+90
-62
lines changed

9 files changed

+90
-62
lines changed

.changeset/beige-terms-check.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@module-federation/modern-js': patch
33
---
44

5-
fix: print warn if user set watchOptions.ignored value as regexp type
5+
fix: override watchOptions.ignored if the modernjs internal value is regexp

.changeset/fuzzy-dots-beam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/modern-js': patch
3+
---
4+
5+
chore: no auto add watchOptions.ignored

apps/modernjs-ssr/host/modern.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ export default defineConfig({
1414
mode: 'stream',
1515
},
1616
},
17-
1817
plugins: [appTools(), moduleFederationPlugin()],
1918
});

apps/website-new/docs/en/guide/basic/type-prompt.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ After modifying the producer code, the consumer will automatically pull the prod
4040

4141
### Federation Runtime API type prompt
4242

43+
:::info
44+
If the builder is `webpack`, you also need to add `**/@mf-types/**` to [watchOptions.ignored](https://webpack.js.org/configuration/watch/#watchoptionsignored) to avoid Circular compilation issues caused by type updates
45+
:::
46+
4347
It needs to add `./@mf-types/*` in the `include` field to enjoy `Federation Runtime` `loadRemote` type hints and type hot reloading
4448

4549
```json title="tsconfig.json"

apps/website-new/docs/zh/guide/basic/type-prompt.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040

4141
### Federation Runtime API 类型提示
4242

43+
:::info
44+
如果构建器为 `webpack` ,还需要再 [watchOptions.ignored](https://webpack.js.org/configuration/watch/#watchoptionsignored) 增加 `**/@mf-types/**`,以避免类型更新导致的循环编译问题
45+
:::
46+
4347
需要在 `include` 字段增加 `./@mf-types/*` 以享有 `Federation Runtime` `loadRemote` 类型提示以及类型热重载
4448

4549
```json title="tsconfig.json"

packages/modernjs/src/cli/configPlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getIPV4,
1313
getMFConfig,
1414
patchMFConfig,
15+
addMyTypes2Ignored,
1516
} from './utils';
1617
import { moduleFederationPlugin } from '@module-federation/sdk';
1718

@@ -83,6 +84,9 @@ export const moduleFederationConfigPlugin = (
8384

8485
return {
8586
tools: {
87+
bundlerChain(chain, { isServer }) {
88+
addMyTypes2Ignored(chain, isServer ? ssrConfig : csrConfig);
89+
},
8690
rspack(config, { isServer }) {
8791
modifyBundlerConfig({
8892
bundlerType,

packages/modernjs/src/cli/utils.spec.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import { it, expect, describe } from 'vitest';
2-
import path from 'path';
3-
import { BundlerConfig } from '../interfaces/bundler';
4-
import {
5-
patchMFConfig,
6-
patchBundlerConfig,
7-
getIPV4,
8-
patchIgnoreWarning,
9-
} from './utils';
2+
import { patchMFConfig, patchBundlerConfig, getIPV4 } from './utils';
103

114
const mfConfig = {
125
name: 'host',
@@ -117,9 +110,6 @@ describe('patchBundlerConfig', async () => {
117110
publicPath: 'auto',
118111
uniqueName: 'host',
119112
},
120-
watchOptions: {
121-
ignored: ['**/@mf-types/**'],
122-
},
123113
};
124114
// @ts-ignore temp ignore
125115

@@ -154,9 +144,6 @@ describe('patchBundlerConfig', async () => {
154144
publicPath: 'auto',
155145
uniqueName: 'host',
156146
},
157-
watchOptions: {
158-
ignored: ['**/@mf-types/**'],
159-
},
160147
};
161148
// @ts-ignore temp ignore
162149
delete bundlerConfig?.ignoreWarnings;

packages/modernjs/src/cli/utils.ts

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
import os from 'os';
2+
import path from 'path';
3+
import { moduleFederationPlugin, encodeName } from '@module-federation/sdk';
4+
import { bundle } from '@modern-js/node-bundle-require';
5+
import { PluginOptions } from '../types';
6+
import { LOCALHOST, PLUGIN_IDENTIFIER } from '../constant';
7+
import logger from './logger';
8+
9+
import type { BundlerConfig, BundlerChainConfig } from '../interfaces/bundler';
110
import type {
211
webpack,
312
UserConfig,
413
AppTools,
514
Rspack,
615
Bundler,
716
} from '@modern-js/app-tools';
8-
import { moduleFederationPlugin, encodeName } from '@module-federation/sdk';
9-
import path from 'path';
10-
import os from 'os';
11-
import { bundle } from '@modern-js/node-bundle-require';
12-
import { PluginOptions } from '../types';
13-
import { LOCALHOST, PLUGIN_IDENTIFIER } from '../constant';
14-
import { BundlerConfig } from '../interfaces/bundler';
15-
import logger from './logger';
1617

1718
const defaultPath = path.resolve(process.cwd(), 'module-federation.config.ts');
1819
const isDev = process.env.NODE_ENV === 'development';
@@ -222,6 +223,59 @@ export function patchIgnoreWarning<T extends Bundler>(
222223
return false;
223224
});
224225
}
226+
227+
export function addMyTypes2Ignored(
228+
chain: BundlerChainConfig,
229+
mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions,
230+
) {
231+
const watchOptions = chain.get(
232+
'watchOptions',
233+
) as webpack.Configuration['watchOptions'];
234+
if (!watchOptions || !watchOptions.ignored) {
235+
chain.watchOptions({
236+
ignored: /[\\/](?:\.git|node_modules|@mf-types)[\\/]/,
237+
});
238+
return;
239+
}
240+
const ignored = watchOptions.ignored;
241+
const DEFAULT_IGNORED_GLOB = '**/@mf-types/**';
242+
243+
if (Array.isArray(ignored)) {
244+
if (
245+
mfConfig.dts !== false &&
246+
typeof mfConfig.dts === 'object' &&
247+
typeof mfConfig.dts.consumeTypes === 'object' &&
248+
mfConfig.dts.consumeTypes.remoteTypesFolder
249+
) {
250+
chain.watchOptions({
251+
...watchOptions,
252+
ignored: ignored.concat(
253+
`**/${mfConfig.dts.consumeTypes.remoteTypesFolder}/**`,
254+
),
255+
});
256+
} else {
257+
chain.watchOptions({
258+
...watchOptions,
259+
ignored: ignored.concat(DEFAULT_IGNORED_GLOB),
260+
});
261+
}
262+
263+
return;
264+
}
265+
266+
if (typeof ignored !== 'string') {
267+
chain.watchOptions({
268+
...watchOptions,
269+
ignored: /[\\/](?:\.git|node_modules|@mf-types)[\\/]/,
270+
});
271+
return;
272+
}
273+
274+
chain.watchOptions({
275+
...watchOptions,
276+
ignored: ignored.concat(DEFAULT_IGNORED_GLOB),
277+
});
278+
}
225279
export function patchBundlerConfig<T extends Bundler>(options: {
226280
bundlerConfig: BundlerConfig<T>;
227281
isServer: boolean;
@@ -237,43 +291,6 @@ export function patchBundlerConfig<T extends Bundler>(options: {
237291

238292
patchIgnoreWarning(bundlerConfig);
239293

240-
if (bundlerType === 'webpack') {
241-
bundlerConfig.watchOptions = bundlerConfig.watchOptions || {};
242-
if (!Array.isArray(bundlerConfig.watchOptions.ignored)) {
243-
if (bundlerConfig.watchOptions.ignored) {
244-
if (typeof bundlerConfig.watchOptions.ignored !== 'string') {
245-
logger.warn(
246-
`Detect you have set watchOptions.ignore as regexp, please transform it to glob string array and add "**/@mf-types/**" to the array.`,
247-
);
248-
} else {
249-
bundlerConfig.watchOptions.ignored = [
250-
bundlerConfig.watchOptions.ignored,
251-
];
252-
}
253-
} else {
254-
bundlerConfig.watchOptions.ignored = [];
255-
}
256-
}
257-
258-
if (Array.isArray(bundlerConfig.watchOptions.ignored)) {
259-
if (mfConfig.dts !== false) {
260-
if (
261-
typeof mfConfig.dts === 'object' &&
262-
typeof mfConfig.dts.consumeTypes === 'object' &&
263-
mfConfig.dts.consumeTypes.remoteTypesFolder
264-
) {
265-
bundlerConfig.watchOptions.ignored.push(
266-
`**/${mfConfig.dts.consumeTypes.remoteTypesFolder}/**`,
267-
);
268-
} else {
269-
bundlerConfig.watchOptions.ignored.push('**/@mf-types/**');
270-
}
271-
} else {
272-
bundlerConfig.watchOptions.ignored.push('**/@mf-types/**');
273-
}
274-
}
275-
}
276-
277294
if (bundlerConfig.output) {
278295
if (!bundlerConfig.output?.chunkLoadingGlobal) {
279296
bundlerConfig.output.chunkLoadingGlobal = `chunk_${mfConfig.name}`;

packages/modernjs/src/interfaces/bundler.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AppTools, Bundler } from '@modern-js/app-tools';
1+
import type { AppTools, Bundler, UserConfig } from '@modern-js/app-tools';
22

33
type AppToolsUserConfig<T extends Bundler> = AppTools<T>['userConfig']['tools'];
44

@@ -21,6 +21,14 @@ type RspackConfigs =
2121
: never;
2222
type ObjectRspack = ExtractObjectType<OmitArrayConfiguration<RspackConfigs>>;
2323

24+
type BundlerChain = ExcludeUndefined<
25+
ExcludeUndefined<UserConfig<AppTools>['tools']>['bundlerChain']
26+
>;
27+
28+
type BundlerChainFunc = Extract<BundlerChain, (chain: any, utils: any) => any>;
29+
30+
export type BundlerChainConfig = Parameters<BundlerChainFunc>[0];
31+
2432
export type BundlerConfig<T extends Bundler> = T extends 'rspack'
2533
? ObjectRspack
2634
: ObjectWebpack;

0 commit comments

Comments
 (0)