Skip to content

feat: implement nodeModulesReconstructedLookup feature for PR7 #3913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: pr6-fallback-version-support
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __mocks__/remotes/fsevents-X6WP4TKM.node
Binary file not shown.
405,338 changes: 405,338 additions & 0 deletions __mocks__/remotes/index.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,8 @@ export interface ProvidesConfig {
* Node modules reconstructed lookup.
*/
nodeModulesReconstructedLookup?: any;
/**
* Original prefix for prefix matches (internal use).
*/
_originalPrefix?: string;
}
116 changes: 116 additions & 0 deletions packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
addSingletonFilterWarning,
testRequestFilters,
createLookupKeyForSharing,
extractPathAfterNodeModules,
} from './utils';

const ModuleNotFoundError = require(
Expand Down Expand Up @@ -105,6 +106,7 @@ class ConsumeSharedPlugin {
request: key,
include: undefined,
exclude: undefined,
nodeModulesReconstructedLookup: undefined,
}
: // key is a request/key
// item is a version
Expand All @@ -123,6 +125,7 @@ class ConsumeSharedPlugin {
request: key,
include: undefined,
exclude: undefined,
nodeModulesReconstructedLookup: undefined,
};
return result;
},
Expand All @@ -149,6 +152,7 @@ class ConsumeSharedPlugin {
issuerLayer: item.issuerLayer ? item.issuerLayer : undefined,
layer: item.layer ? item.layer : undefined,
request,
nodeModulesReconstructedLookup: item.nodeModulesReconstructedLookup,
} as ConsumeOptions;
},
);
Expand Down Expand Up @@ -496,6 +500,69 @@ class ConsumeSharedPlugin {
);
}

// Then try relative path handling and node_modules paths
let reconstructed: string | null = null;
let modulePathAfterNodeModules: string | null = null;

if (
request &&
!path.isAbsolute(request) &&
RELATIVE_OR_ABSOLUTE_PATH_REGEX.test(request)
) {
reconstructed = path.join(context, request);
modulePathAfterNodeModules =
extractPathAfterNodeModules(reconstructed);

// Try to match with module path after node_modules
if (modulePathAfterNodeModules) {
const moduleMatch =
unresolvedConsumes.get(
createLookupKeyForSharing(
modulePathAfterNodeModules,
contextInfo.issuerLayer,
),
) ||
unresolvedConsumes.get(
createLookupKeyForSharing(
modulePathAfterNodeModules,
undefined,
),
);

if (
moduleMatch !== undefined &&
moduleMatch.nodeModulesReconstructedLookup
) {
return boundCreateConsumeSharedModule(
compilation,
context,
modulePathAfterNodeModules,
moduleMatch,
);
}
}

// Try to match with the full reconstructed path
const reconstructedMatch =
unresolvedConsumes.get(
createLookupKeyForSharing(
reconstructed,
contextInfo.issuerLayer,
),
) ||
unresolvedConsumes.get(
createLookupKeyForSharing(reconstructed, undefined),
);

if (reconstructedMatch !== undefined) {
return boundCreateConsumeSharedModule(
compilation,
context,
reconstructed,
reconstructedMatch,
);
}
}
// Check for prefixed consumes with original request
for (const [prefix, options] of prefixedConsumes) {
const lookup = options.request || prefix;
Expand Down Expand Up @@ -538,6 +605,55 @@ class ConsumeSharedPlugin {
}
}

// Also check prefixed consumes with modulePathAfterNodeModules
if (modulePathAfterNodeModules) {
for (const [prefix, options] of prefixedConsumes) {
if (!options.nodeModulesReconstructedLookup) {
continue;
}
// Refined issuerLayer matching logic for reconstructed path
if (options.issuerLayer) {
if (!contextInfo.issuerLayer) {
continue; // Option is layered, request is not: skip
}
if (contextInfo.issuerLayer !== options.issuerLayer) {
continue; // Both are layered but do not match: skip
}
}
// If contextInfo.issuerLayer exists but options.issuerLayer does not, allow (non-layered option matches layered request)
const lookup = options.request || prefix;
if (modulePathAfterNodeModules.startsWith(lookup)) {
const remainder = modulePathAfterNodeModules.slice(
lookup.length,
);

if (
!testRequestFilters(
remainder,
options.include?.request,
options.exclude?.request,
)
) {
continue;
}

return boundCreateConsumeSharedModule(
compilation,
context,
modulePathAfterNodeModules,
{
...options,
import: options.import
? options.import + remainder
: undefined,
shareKey: options.shareKey + remainder,
layer: options.layer,
},
);
}
}
}

return;
});
},
Expand Down
Loading
Loading