-
Notifications
You must be signed in to change notification settings - Fork 700
Improve handling of aliased Console provided shared modules #16376
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import type { getSharedScope } from '../plugin-shared-modules'; | ||
| import { monkeyPatchSharedScope } from '../plugin-shared-modules'; | ||
|
|
||
| describe('monkeyPatchSharedScope', () => { | ||
| it('adds aliased modules to share scope object', () => { | ||
| const getModule = jest.fn(); | ||
|
|
||
| const testScope: ReturnType<typeof getSharedScope> = { | ||
| 'react-router': { | ||
| '7.13.1': { from: 'openshift-console', eager: true, loaded: 1, get: getModule }, | ||
| }, | ||
| }; | ||
|
|
||
| monkeyPatchSharedScope(testScope); | ||
|
|
||
| expect(Object.keys(testScope)).toEqual([ | ||
| 'react-router', | ||
| 'react-router-dom', | ||
| 'react-router-dom-v5-compat', | ||
| ]); | ||
|
|
||
| expect(testScope['react-router']).toEqual({ | ||
| '7.13.1': { from: 'openshift-console', eager: true, loaded: 1, get: getModule }, | ||
| }); | ||
|
|
||
| expect(testScope['react-router-dom']).toEqual(testScope['react-router']); | ||
| expect(testScope['react-router-dom-v5-compat']).toEqual(testScope['react-router']); | ||
|
Comment on lines
+16
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assert alias identity, not just structural equality.
🛠️ Suggested test tweak- expect(testScope['react-router-dom']).toEqual(testScope['react-router']);
- expect(testScope['react-router-dom-v5-compat']).toEqual(testScope['react-router']);
+ expect(testScope['react-router-dom']).toBe(testScope['react-router']);
+ expect(testScope['react-router-dom-v5-compat']).toBe(testScope['react-router']);🤖 Prompt for AI Agents |
||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,34 @@ const SHARED_SCOPE_NAME = 'default'; | |||||
| */ | ||||||
| export const initSharedScope = async () => __webpack_init_sharing__(SHARED_SCOPE_NAME); | ||||||
|
|
||||||
| /** | ||||||
| * webpack provided type of `__webpack_share_scopes__` object is incorrect; at runtime, | ||||||
| * each shared module comes with one or more version(s) with each version scoped to its | ||||||
| * own object, for example: | ||||||
| * | ||||||
| * ```js | ||||||
| * { | ||||||
| * [SCOPE_NAME]: { | ||||||
| * 'react': { | ||||||
| * '18.3.1': { | ||||||
| * from: 'openshift-console', | ||||||
| * eager: true, | ||||||
| * loaded: 1, | ||||||
| * get: moduleFactoryFunction, | ||||||
| * } | ||||||
| * } | ||||||
| * } | ||||||
| * } | ||||||
| * ``` | ||||||
| */ | ||||||
| type WebpackShareScopes = { | ||||||
| [scopeName: string]: { | ||||||
| [moduleName: string]: { | ||||||
| [moduleVersion: string]: typeof __webpack_share_scopes__[string][string]; | ||||||
| }; | ||||||
| }; | ||||||
| }; | ||||||
|
|
||||||
| /** | ||||||
| * Get the webpack share scope object. | ||||||
| */ | ||||||
|
|
@@ -20,7 +48,8 @@ export const getSharedScope = () => { | |||||
| throw new Error('Attempt to access share scope object before its initialization'); | ||||||
| } | ||||||
|
|
||||||
| return __webpack_share_scopes__[SHARED_SCOPE_NAME]; | ||||||
| // TODO: Remove this type cast once __webpack_share_scopes__ object type is fixed | ||||||
| return (__webpack_share_scopes__[SHARED_SCOPE_NAME] as unknown) as WebpackShareScopes[string]; | ||||||
| }; | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -29,8 +58,7 @@ export const getSharedScope = () => { | |||||
| * - add `react-router-dom-v5-compat` module aliased to `react-router` | ||||||
| * - add `react-router-dom` module aliased to `react-router` | ||||||
| */ | ||||||
| export const monkeyPatchSharedScope = () => { | ||||||
| const scope = getSharedScope(); | ||||||
| export const monkeyPatchSharedScope = (scope = getSharedScope()) => { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TypeScript will infer the type of The inferred type of {
[moduleName: string]: {
[moduleVersion: string]: { /* module data for name@version */ };
};
}We could explicitly type |
||||||
| scope['react-router-dom'] = scope['react-router']; | ||||||
| scope['react-router-dom-v5-compat'] = scope['react-router']; | ||||||
| }; | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -135,15 +135,17 @@ export const validateConsoleExtensionsFileSchema = ( | |
| return new SchemaValidator(description).validate(schema, extensions); | ||
| }; | ||
|
|
||
| const getDeprecatedSharedModuleWarnings = (pkg: ConsolePluginPackageJSON): string[] => { | ||
| const getCompileTimeSharedModuleWarnings = (pkg: ConsolePluginPackageJSON): string[] => { | ||
| const warnings: string[] = []; | ||
|
|
||
| sharedPluginModules.forEach((moduleName) => { | ||
| const { deprecated } = getSharedModuleMetadata(moduleName); | ||
| const { deprecated, aliased } = getSharedModuleMetadata(moduleName); | ||
|
|
||
| if (deprecated && hasPackageDependency(pkg, moduleName)) { | ||
| if ((deprecated || aliased) && hasPackageDependency(pkg, moduleName)) { | ||
| warnings.push( | ||
| `[DEPRECATION WARNING] Console provided shared module ${moduleName} has been deprecated: ${deprecated}`, | ||
| deprecated | ||
| ? `[WARNING] Console provided shared module ${moduleName} has been deprecated: ${deprecated}` | ||
| : `[WARNING] Console provided shared module ${moduleName} is aliased, beware of potential skew between aliased vs actual module code`, | ||
| ); | ||
|
Comment on lines
+142
to
149
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alias warnings are currently shadowed. These modules still have a truthy If the build should surface the alias message, branch on 🔧 Possible fix- if ((deprecated || aliased) && hasPackageDependency(pkg, moduleName)) {
- warnings.push(
- deprecated
- ? `[WARNING] Console provided shared module ${moduleName} has been deprecated: ${deprecated}`
- : `[WARNING] Console provided shared module ${moduleName} is aliased, beware of potential skew between aliased vs actual module code`,
- );
+ if (aliased && hasPackageDependency(pkg, moduleName)) {
+ warnings.push(
+ `[WARNING] Console provided shared module ${moduleName} is aliased, beware of potential skew between aliased vs actual module code`,
+ );
+ } else if (deprecated && hasPackageDependency(pkg, moduleName)) {
+ warnings.push(
+ `[WARNING] Console provided shared module ${moduleName} has been deprecated: ${deprecated}`,
+ );
}🤖 Prompt for AI Agents
Comment on lines
+146
to
149
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently the aliased shared module warning is never shown (as both
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A shared module that is aliased doesn't necessarily have to be deprecated - I think these are orthogonal concepts. You are correct that This code is meant to print just one warning per each aliased and/or deprecated module, with deprecation having higher priority. I'll think about this some more but the general idea was to keep track of which modules are aliased (in addition to tracking their deprecated status). |
||
| } | ||
| }); | ||
|
|
@@ -476,7 +478,7 @@ export class ConsoleRemotePlugin implements WebpackPluginInstance { | |
| } | ||
| } | ||
|
|
||
| getDeprecatedSharedModuleWarnings(this.pkg).forEach((message) => { | ||
| getCompileTimeSharedModuleWarnings(this.pkg).forEach((message) => { | ||
| compilation.warnings.push(new compiler.webpack.WebpackError(message)); | ||
| }); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do.