Skip to content

Commit ed62b23

Browse files
Improve infrastructure logging across federation plugins (#4115)
1 parent 4438fed commit ed62b23

File tree

26 files changed

+482
-91
lines changed

26 files changed

+482
-91
lines changed

packages/data-prefetch/src/cli/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import path from 'path';
22
import fs from 'fs-extra';
33

44
import {
5+
bindLoggerToCompiler,
6+
createInfrastructureLogger,
7+
createLogger,
58
encodeName,
69
moduleFederationPlugin,
710
MFPrefetchCommon,
@@ -18,6 +21,15 @@ const { RuntimeGlobals, Template } = require(
1821
normalizeWebpackPath('webpack'),
1922
) as typeof import('webpack');
2023

24+
const createBundlerLogger: typeof createLogger =
25+
typeof createInfrastructureLogger === 'function'
26+
? (createInfrastructureLogger as unknown as typeof createLogger)
27+
: createLogger;
28+
29+
const logger = createBundlerLogger(
30+
'[ Module Federation Data Prefetch Plugin ]',
31+
);
32+
2133
export function getFederationGlobalScope(
2234
runtimeGlobals: typeof RuntimeGlobals,
2335
): string {
@@ -35,6 +47,7 @@ export class PrefetchPlugin implements WebpackPluginInstance {
3547

3648
// eslint-disable-next-line max-lines-per-function
3749
apply(compiler: Compiler) {
50+
bindLoggerToCompiler(logger, compiler, 'PrefetchPlugin');
3851
const { name, exposes } = this.options;
3952
if (!exposes) {
4053
return;
@@ -54,7 +67,7 @@ export class PrefetchPlugin implements WebpackPluginInstance {
5467
}
5568
if (this.options.shareStrategy !== SHARED_STRATEGY) {
5669
this.options.shareStrategy = SHARED_STRATEGY;
57-
console.warn(
70+
logger.warn(
5871
`[Module Federation Data Prefetch]: Your shared strategy is set to '${SHARED_STRATEGY}', this is a necessary condition for data prefetch`,
5972
);
6073
}

packages/dts-plugin/src/plugins/ConsumeTypesPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { logger } from '@module-federation/sdk';
1+
import { infrastructureLogger as logger } from '@module-federation/sdk';
22
import {
33
normalizeOptions,
44
type moduleFederationPlugin,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
'use strict';
77
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
8-
import { logger } from '@module-federation/sdk';
8+
import { infrastructureLogger as logger } from '@module-federation/sdk';
99
import {
1010
getShortErrorMsg,
1111
buildDescMap,

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import { DtsPlugin } from '@module-federation/dts-plugin';
88
import { ContainerManager, utils } from '@module-federation/managers';
99
import { StatsPlugin } from '@module-federation/manifest';
1010
import {
11+
bindLoggerToCompiler,
1112
composeKeyWithSeparator,
1213
type moduleFederationPlugin,
13-
logger,
14+
infrastructureLogger,
1415
} from '@module-federation/sdk';
1516
import { PrefetchPlugin } from '@module-federation/data-prefetch/cli';
1617
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
@@ -102,6 +103,11 @@ class ModuleFederationPlugin implements WebpackPluginInstance {
102103
* @returns {void}
103104
*/
104105
apply(compiler: Compiler): void {
106+
bindLoggerToCompiler(
107+
infrastructureLogger,
108+
compiler,
109+
'EnhancedModuleFederationPlugin',
110+
);
105111
const { _options: options } = this;
106112
// must before ModuleFederationPlugin
107113
(new RemoteEntryPlugin(options) as unknown as WebpackPluginInstance).apply(
@@ -175,7 +181,7 @@ class ModuleFederationPlugin implements WebpackPluginInstance {
175181
if (err instanceof Error) {
176182
err.message = `[ ModuleFederationPlugin ]: Manifest will not generate, because: ${err.message}`;
177183
}
178-
logger.warn(err);
184+
infrastructureLogger.warn(err);
179185
disableManifest = true;
180186
}
181187
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import fs from 'fs';
1313
import path from 'path';
1414
import { ConcatSource } from 'webpack-sources';
1515
import { transformSync } from '@swc/core';
16-
import { logger } from '@module-federation/sdk';
16+
import { infrastructureLogger as logger } from '@module-federation/sdk';
1717

1818
const { RuntimeModule, Template, RuntimeGlobals } = require(
1919
normalizeWebpackPath('webpack'),

packages/enhanced/test/ConfigTestCases.template.js

Lines changed: 92 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,59 @@ const captureStdio = require('./helpers/captureStdio');
1818
const asModule = require('./helpers/asModule');
1919
const filterInfraStructureErrors = require('./helpers/infrastructureLogErrors');
2020

21+
const dedupeByMessage = (items) => {
22+
if (!Array.isArray(items) || items.length === 0) {
23+
return [];
24+
}
25+
const seen = new Set();
26+
const deduped = [];
27+
for (const item of items) {
28+
const key = item.message;
29+
if (key) {
30+
if (seen.has(key)) {
31+
continue;
32+
}
33+
seen.add(key);
34+
}
35+
deduped.push(item);
36+
}
37+
return deduped;
38+
};
39+
40+
const collectInfrastructureOutputs = (infraLogs, stderrOutput, config) => {
41+
const infrastructureCollection = filterInfraStructureErrors.collect(
42+
infraLogs,
43+
config,
44+
);
45+
const stderrLines = stderrOutput
46+
? stderrOutput
47+
.split(/\r?\n/)
48+
.map((line) => line.trim())
49+
.filter(Boolean)
50+
: [];
51+
const stderrCollection = filterInfraStructureErrors.collect(
52+
stderrLines,
53+
config,
54+
);
55+
const unhandled = [
56+
...infrastructureCollection.entries
57+
.filter(
58+
(entry) => !infrastructureCollection.handled.has(entry.normalized),
59+
)
60+
.map((entry) => entry.normalized),
61+
...stderrCollection.entries
62+
.filter((entry) => !stderrCollection.handled.has(entry.normalized))
63+
.map((entry) => entry.normalized),
64+
];
65+
66+
const combinedResults = dedupeByMessage([
67+
...infrastructureCollection.results,
68+
...stderrCollection.results,
69+
]);
70+
71+
return { unhandled, results: combinedResults };
72+
};
73+
2174
const casesPath = path.join(__dirname, 'configCases');
2275
const categories = fs.readdirSync(casesPath).map((cat) => {
2376
return {
@@ -216,22 +269,25 @@ const describeCases = (config) => {
216269
fs.mkdirSync(outputDirectory, { recursive: true });
217270
infraStructureLog.length = 0;
218271
require('webpack')(options, (err) => {
219-
const infrastructureLogging = stderr.toString();
220-
if (infrastructureLogging) {
272+
const stderrOutput = stderr.toString();
273+
const { unhandled, results: infrastructureLogErrors } =
274+
collectInfrastructureOutputs(
275+
infraStructureLog,
276+
stderrOutput,
277+
{
278+
run: 1,
279+
options,
280+
},
281+
);
282+
stderr.reset();
283+
if (unhandled.length) {
221284
return done(
222285
new Error(
223286
'Errors/Warnings during build:\n' +
224-
infrastructureLogging,
287+
unhandled.join('\n'),
225288
),
226289
);
227290
}
228-
const infrastructureLogErrors = filterInfraStructureErrors(
229-
infraStructureLog,
230-
{
231-
run: 1,
232-
options,
233-
},
234-
);
235291
if (
236292
infrastructureLogErrors.length &&
237293
checkArrayExpectation(
@@ -260,13 +316,23 @@ const describeCases = (config) => {
260316
modules: true,
261317
errorsCount: true,
262318
});
319+
const stderrOutput = stderr.toString();
320+
const { unhandled, results: infrastructureLogErrors } =
321+
collectInfrastructureOutputs(
322+
infraStructureLog,
323+
stderrOutput,
324+
{
325+
run: 2,
326+
options,
327+
},
328+
);
329+
stderr.reset();
263330
if (errorsCount === 0) {
264-
const infrastructureLogging = stderr.toString();
265-
if (infrastructureLogging) {
331+
if (unhandled.length) {
266332
return done(
267333
new Error(
268334
'Errors/Warnings during build:\n' +
269-
infrastructureLogging,
335+
unhandled.join('\n'),
270336
),
271337
);
272338
}
@@ -292,13 +358,6 @@ const describeCases = (config) => {
292358
);
293359
}
294360
}
295-
const infrastructureLogErrors = filterInfraStructureErrors(
296-
infraStructureLog,
297-
{
298-
run: 2,
299-
options,
300-
},
301-
);
302361
if (
303362
infrastructureLogErrors.length &&
304363
checkArrayExpectation(
@@ -363,11 +422,21 @@ const describeCases = (config) => {
363422
) {
364423
return;
365424
}
366-
const infrastructureLogging = stderr.toString();
367-
if (infrastructureLogging) {
425+
const stderrOutput = stderr.toString();
426+
const { unhandled, results: infrastructureLogErrors } =
427+
collectInfrastructureOutputs(
428+
infraStructureLog,
429+
stderrOutput,
430+
{
431+
run: 3,
432+
options,
433+
},
434+
);
435+
stderr.reset();
436+
if (unhandled.length) {
368437
return done(
369438
new Error(
370-
'Errors/Warnings during build:\n' + infrastructureLogging,
439+
'Errors/Warnings during build:\n' + unhandled.join('\n'),
371440
),
372441
);
373442
}
@@ -382,13 +451,6 @@ const describeCases = (config) => {
382451
) {
383452
return;
384453
}
385-
const infrastructureLogErrors = filterInfraStructureErrors(
386-
infraStructureLog,
387-
{
388-
run: 3,
389-
options,
390-
},
391-
);
392454
if (
393455
infrastructureLogErrors.length &&
394456
checkArrayExpectation(

packages/enhanced/test/configCases/container/2-transitive-overriding/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
},
88
plugins: [
99
new ModuleFederationPlugin({
10+
name: 'container-transitive-override-consumer',
1011
remoteType: 'commonjs-module',
1112
remotes: {
1213
'container-no-shared':

packages/enhanced/test/configCases/container/3-container-full/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { ModuleFederationPlugin } = require('../../../../dist/src');
33
module.exports = {
44
plugins: [
55
new ModuleFederationPlugin({
6+
name: 'container-full-consumer',
67
remoteType: 'commonjs-module',
78
remotes: {
89
containerB: '../1-container-full/container.js',

packages/enhanced/test/configCases/sharing/consume-module-ignore-warnings/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
let warnings = [];
22
let oldWarn;
33

4+
const shouldIgnoreWarning = (warning) =>
5+
typeof warning === 'string' && warning.startsWith('[ Federation Runtime ]');
6+
7+
const captureWarning = (...args) => {
8+
const [message] = args;
9+
if (shouldIgnoreWarning(message)) {
10+
return;
11+
}
12+
warnings.push(message);
13+
};
14+
415
beforeEach((done) => {
516
oldWarn = console.warn;
6-
console.warn = (m) => warnings.push(m);
17+
console.warn = captureWarning;
718
done();
819
});
920

packages/enhanced/test/configCases/sharing/consume-module/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
let warnings = [];
22
let oldWarn;
33

4+
const shouldIgnoreWarning = (warning) =>
5+
typeof warning === 'string' && warning.startsWith('[ Federation Runtime ]');
6+
7+
const captureWarning = (...args) => {
8+
const [message] = args;
9+
if (shouldIgnoreWarning(message)) {
10+
return;
11+
}
12+
warnings.push(message);
13+
};
14+
415
beforeEach((done) => {
516
oldWarn = console.warn;
6-
console.warn = (m) => warnings.push(m);
17+
console.warn = captureWarning;
718
done();
819
});
920

0 commit comments

Comments
 (0)