Skip to content

fix: webpackRequire.S has been attached with instance.shareScopeMap #1962

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

Merged
merged 4 commits into from
Jan 15, 2024
Merged
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
9 changes: 9 additions & 0 deletions .changeset/shy-spoons-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@module-federation/webpack-bundler-runtime': patch
'@module-federation/enhanced': patch
'@module-federation/runtime': patch
---

fix: remove duplicate init shareScopeMap
fix: normalize schemas path
fix: shared is loaded if it has lib attr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import type { Compiler, WebpackPluginInstance } from 'webpack';
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
import isValidExternalsType from 'webpack/schemas/plugins/container/ExternalsType.check.js';
import type { ModuleFederationPluginOptions } from './ModuleFederationPluginTypes';
import SharePlugin from '../sharing/SharePlugin';
import ContainerPlugin from './ContainerPlugin';
Expand All @@ -16,6 +15,12 @@ import checkOptions from '../../schemas/container/ModuleFederationPlugin.check';
import schema from '../../schemas/container/ModuleFederationPlugin';
import FederationRuntimePlugin from './runtime/FederationRuntimePlugin';

const isValidExternalsType = require(
normalizeWebpackPath(
'webpack/schemas/plugins/container/ExternalsType.check.js',
),
) as typeof import('webpack/schemas/plugins/container/ExternalsType.check.js');

const createSchemaValidation = require(
normalizeWebpackPath('webpack/lib/util/create-schema-validation'),
) as typeof import('webpack/lib/util/create-schema-validation');
Expand Down
13 changes: 11 additions & 2 deletions packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,17 @@ const createSchemaValidation = require(

const validate = createSchemaValidation(
//eslint-disable-next-line
require('webpack/schemas/plugins/sharing/ConsumeSharedPlugin.check.js'),
() => require('webpack/schemas/plugins/sharing/ConsumeSharedPlugin.json'),
require(
normalizeWebpackPath(
'webpack/schemas/plugins/sharing/ConsumeSharedPlugin.check.js',
),
),
() =>
require(
normalizeWebpackPath(
'webpack/schemas/plugins/sharing/ConsumeSharedPlugin.json',
),
),
{
name: 'Consume Shared Plugin',
baseDataPath: 'options',
Expand Down
13 changes: 9 additions & 4 deletions packages/runtime/src/utils/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,18 @@ const findVersion = (
}, 0) as string;
};

const isLoaded = (shared: Shared) => {
return Boolean(shared.loaded) || typeof shared.lib === 'function';
};

function findSingletonVersionOrderByVersion(
shareScopeMap: ShareScopeMap,
scope: string,
pkgName: string,
): string {
const versions = shareScopeMap[scope][pkgName];
const callback = function (prev: string, cur: string): boolean {
return !versions[prev].loaded && versionLt(prev, cur);
return !isLoaded(versions[prev]) && versionLt(prev, cur);
};

return findVersion(shareScopeMap, scope, pkgName, callback);
Expand All @@ -124,15 +128,16 @@ function findSingletonVersionOrderByLoaded(
pkgName: string,
): string {
const versions = shareScopeMap[scope][pkgName];

const callback = function (prev: string, cur: string): boolean {
if (versions[cur].loaded) {
if (versions[prev].loaded) {
if (isLoaded(versions[cur])) {
if (isLoaded(versions[prev])) {
return Boolean(versionLt(prev, cur));
} else {
return true;
}
}
if (versions[prev].loaded) {
if (isLoaded(versions[prev])) {
return false;
}
return versionLt(prev, cur);
Expand Down
1 change: 0 additions & 1 deletion packages/webpack-bundler-runtime/src/initContainerEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export function initContainerEntry(

federationInstance.initShareScopeMap(name, shareScope);

webpackRequire.S[name] = shareScope;
if (webpackRequire.federation.attachShareScopeMap) {
webpackRequire.federation.attachShareScopeMap(webpackRequire);
}
Expand Down