Skip to content

Commit 667a941

Browse files
chore: debugging
1 parent 5a67bc8 commit 667a941

File tree

2 files changed

+153
-4
lines changed

2 files changed

+153
-4
lines changed

packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ class ConsumeSharedPlugin {
487487
) => this.createConsumeSharedModule(compilation, ctx, req, cfg);
488488

489489
return promise.then(async () => {
490+
const debugAlias = process.env['MF_DEBUG_ALIAS'] === '1';
490491
if (
491492
dependencies[0] instanceof ConsumeSharedFallbackDependency ||
492493
dependencies[0] instanceof ProvideForSharedDependency
@@ -510,6 +511,7 @@ class ConsumeSharedPlugin {
510511
let aliasAfterNodeModules: string | undefined;
511512
const aliasShareKeyCandidates: string[] = [];
512513
if (request && !RELATIVE_OR_ABSOLUTE_PATH_REGEX.test(request)) {
514+
if (debugAlias) console.log('[alias][consume] bare:', request);
513515
try {
514516
const resolveContext = {
515517
fileDependencies: new LazySet<string>(),
@@ -542,13 +544,26 @@ class ConsumeSharedPlugin {
542544
);
543545
if (err || !resPath) return res(undefined);
544546
const resolvedPath = resPath as string;
547+
if (debugAlias)
548+
console.log(
549+
'[alias][consume] resolved ->',
550+
resolvedPath,
551+
);
545552
const nm = extractPathAfterNodeModules(resolvedPath);
546553
if (nm) {
547554
aliasAfterNodeModules = nm;
548555
const nmDir = nm.replace(/\/(index\.[^/]+)$/, '');
549556
if (nmDir && nmDir !== nm)
550557
aliasShareKeyCandidates.push(nmDir);
558+
const nmNoExt = nm.replace(/\.[^/]+$/, '');
559+
if (nmNoExt && nmNoExt !== nm)
560+
aliasShareKeyCandidates.push(nmNoExt);
551561
aliasShareKeyCandidates.push(nm);
562+
if (debugAlias)
563+
console.log(
564+
'[alias][consume] nm candidates:',
565+
[nmDir, nmNoExt, nm].filter(Boolean),
566+
);
552567
}
553568
try {
554569
if (
@@ -572,7 +587,20 @@ class ConsumeSharedPlugin {
572587
);
573588
if (pkgKeyDir && pkgKeyDir !== pkgKey)
574589
aliasShareKeyCandidates.push(pkgKeyDir);
590+
const pkgKeyNoExt = pkgKey.replace(
591+
/\.[^/]+$/,
592+
'',
593+
);
594+
if (pkgKeyNoExt && pkgKeyNoExt !== pkgKey)
595+
aliasShareKeyCandidates.push(pkgKeyNoExt);
575596
aliasShareKeyCandidates.push(pkgKey);
597+
if (debugAlias)
598+
console.log(
599+
'[alias][consume] pkg candidates:',
600+
[pkgKeyDir, pkgKeyNoExt, pkgKey].filter(
601+
Boolean,
602+
),
603+
);
576604
}
577605
} catch {}
578606
res(resolvedPath);
@@ -613,9 +641,40 @@ class ConsumeSharedPlugin {
613641
createLookupKeyForSharing(cand, undefined),
614642
);
615643
if (aliasMatch) {
644+
if (debugAlias)
645+
console.log(
646+
'[alias][consume] direct candidate match:',
647+
cand,
648+
);
616649
return createConsume(context, request, aliasMatch);
617650
}
618651
}
652+
// Fallback: scan unresolved keys for prefix matches when allowed
653+
for (const [lookupKey, opts] of unresolvedConsumes) {
654+
const keyNoLayer = lookupKey.replace(/^\([^)]*\)/, '');
655+
if (!opts.allowNodeModulesSuffixMatch) continue;
656+
for (const cand of aliasShareKeyCandidates) {
657+
const candTrim = cand
658+
.replace(/\/(index\.[^/]+)$/, '')
659+
.replace(/\.[^/]+$/, '');
660+
const keyTrim = keyNoLayer
661+
.replace(/\/(index\.[^/]+)$/, '')
662+
.replace(/\.[^/]+$/, '');
663+
if (
664+
candTrim.startsWith(keyTrim) ||
665+
keyTrim.startsWith(candTrim)
666+
) {
667+
if (debugAlias)
668+
console.log(
669+
'[alias][consume] fallback prefix match:',
670+
keyNoLayer,
671+
'<->',
672+
candTrim,
673+
);
674+
return createConsume(context, request, opts);
675+
}
676+
}
677+
}
619678
}
620679

621680
// 2b) Try unresolved match with path after node_modules (if allowed) from reconstructed relative
@@ -712,6 +771,13 @@ class ConsumeSharedPlugin {
712771
) {
713772
continue;
714773
}
774+
if (debugAlias)
775+
console.log(
776+
'[alias][consume] prefix nm match:',
777+
lookup,
778+
'+',
779+
remainder,
780+
);
715781
return createConsume(context, afterNodeModules, {
716782
...options,
717783
import: options.import
@@ -745,6 +811,13 @@ class ConsumeSharedPlugin {
745811
) {
746812
continue;
747813
}
814+
if (debugAlias)
815+
console.log(
816+
'[alias][consume] prefix alias match:',
817+
lookup,
818+
'+',
819+
remainder,
820+
);
748821
return createConsume(context, request, {
749822
...options,
750823
import: options.import

packages/enhanced/src/lib/sharing/ProvideSharedPlugin.ts

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ class ProvideSharedPlugin {
290290
normalModuleFactory.hooks.module.tap(
291291
'ProvideSharedPlugin',
292292
(module, { resource, resourceResolveData }, resolveData) => {
293+
const debugAlias = process.env['MF_DEBUG_ALIAS'] === '1';
294+
if (debugAlias) console.log('[alias][provide] resource:', resource);
293295
const moduleLayer = module.layer;
294296
const lookupKeyForResource = createLookupKeyForSharing(
295297
resource || '',
@@ -380,6 +382,11 @@ class ProvideSharedPlugin {
380382
configFromReconstructedDirect.allowNodeModulesSuffixMatch &&
381383
!resolvedProvideMap.has(lookupKeyForResource)
382384
) {
385+
if (debugAlias)
386+
console.log(
387+
'[alias][provide] direct nm match:',
388+
reconstructedLookupKey,
389+
);
383390
provide(
384391
modulePathAfterNodeModules,
385392
configFromReconstructedDirect,
@@ -391,15 +398,15 @@ class ProvideSharedPlugin {
391398

392399
// 2a-i. Try direct match using package description data derived key
393400
if (
394-
resourceResolveData?.descriptionFilePath &&
395-
resourceResolveData?.descriptionFileData &&
401+
resourceResolveData?.['descriptionFilePath'] &&
402+
resourceResolveData?.['descriptionFileData'] &&
396403
!resolvedProvideMap.has(lookupKeyForResource)
397404
) {
398405
try {
399-
const pkgName = resourceResolveData.descriptionFileData
406+
const pkgName = resourceResolveData['descriptionFileData']
400407
.name as string;
401408
const pkgDir = path.dirname(
402-
resourceResolveData.descriptionFilePath as string,
409+
resourceResolveData['descriptionFilePath'] as string,
403410
);
404411
const rel = path
405412
.relative(pkgDir, resource)
@@ -416,6 +423,11 @@ class ProvideSharedPlugin {
416423
),
417424
);
418425
if (direct) {
426+
if (debugAlias)
427+
console.log(
428+
'[alias][provide] direct pkg match:',
429+
cand,
430+
);
419431
provide(
420432
cand,
421433
direct,
@@ -447,6 +459,11 @@ class ProvideSharedPlugin {
447459
lookupKeyForResource,
448460
resolveData,
449461
);
462+
if (matched && debugAlias)
463+
console.log(
464+
'[alias][provide] prefix match (mp direct):',
465+
configuredPrefix,
466+
);
450467
if (matched) break;
451468
}
452469
}
@@ -473,8 +490,67 @@ class ProvideSharedPlugin {
473490
lookupKeyForResource,
474491
resolveData,
475492
);
493+
if (matched && debugAlias)
494+
console.log(
495+
'[alias][provide] prefix match (mp prefix):',
496+
configuredPrefix,
497+
);
476498
if (matched) break;
477499
}
500+
// Fallback: scan matchProvides for prefix-like matches when allowed
501+
if (!resolvedProvideMap.has(lookupKeyForResource)) {
502+
for (const [mKey, cfg] of matchProvides) {
503+
if (!cfg.allowNodeModulesSuffixMatch) continue;
504+
const configuredPrefix =
505+
cfg.request || mKey.split('?')[0];
506+
const keyTrim = configuredPrefix
507+
.replace(/\/(index\.[^/]+)$/, '')
508+
.replace(/\.[^/]+$/, '');
509+
const candTrim = modulePathAfterNodeModules
510+
.replace(/\/(index\.[^/]+)$/, '')
511+
.replace(/\.[^/]+$/, '');
512+
if (candTrim.startsWith(keyTrim)) {
513+
const remainder = modulePathAfterNodeModules.slice(
514+
configuredPrefix.length,
515+
);
516+
if (
517+
!testRequestFilters(
518+
remainder,
519+
cfg.include?.request,
520+
cfg.exclude?.request,
521+
)
522+
) {
523+
continue;
524+
}
525+
const finalShareKey = cfg.shareKey
526+
? cfg.shareKey + remainder
527+
: configuredPrefix + remainder;
528+
const configForSpecificModule: ProvidesConfig = {
529+
...cfg,
530+
shareKey: finalShareKey,
531+
request: modulePathAfterNodeModules,
532+
_originalPrefix: configuredPrefix,
533+
include: cfg.include ? { ...cfg.include } : undefined,
534+
exclude: cfg.exclude ? { ...cfg.exclude } : undefined,
535+
};
536+
this.provideSharedModule(
537+
compilation,
538+
resolvedProvideMap,
539+
modulePathAfterNodeModules,
540+
configForSpecificModule,
541+
resource,
542+
resourceResolveData,
543+
);
544+
resolveData.cacheable = false;
545+
if (debugAlias)
546+
console.log(
547+
'[alias][provide] fallback prefix match:',
548+
configuredPrefix,
549+
);
550+
break;
551+
}
552+
}
553+
}
478554
}
479555
}
480556
}

0 commit comments

Comments
 (0)