-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt webpack 5 related code in dynamic plugin SDK
- Loading branch information
1 parent
b2cebcf
commit f5eb2da
Showing
16 changed files
with
276 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
frontend/packages/console-dynamic-plugin-sdk/src/__tests__/shared-modules-init.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { sharedPluginModules } from '../shared-modules'; | ||
import { initSharedPluginModules } from '../shared-modules-init'; | ||
import { getEntryModuleMocks } from '../utils/test-utils'; | ||
|
||
describe('initSharedPluginModules', () => { | ||
const expectSameValues = (arr1: string[], arr2: string[]) => { | ||
expect(new Set(arr1)).toEqual(new Set(arr2)); | ||
}; | ||
|
||
it('is consistent with sharedPluginModules definition', () => { | ||
const [, entryModule] = getEntryModuleMocks({}); | ||
|
||
initSharedPluginModules(entryModule); | ||
|
||
expect(entryModule.init).toHaveBeenCalledTimes(1); | ||
|
||
expectSameValues(Object.keys(entryModule.init.mock.calls[0][0]), sharedPluginModules); | ||
}); | ||
|
||
it('supports plugins built with an older version of plugin SDK', () => { | ||
const [, entryModule] = getEntryModuleMocks({}); | ||
entryModule.override = jest.fn(); | ||
|
||
initSharedPluginModules(entryModule); | ||
|
||
expect(entryModule.override).toHaveBeenCalledTimes(1); | ||
expect(entryModule.init).not.toHaveBeenCalled(); | ||
|
||
expectSameValues(Object.keys(entryModule.override.mock.calls[0][0]), sharedPluginModules); | ||
}); | ||
}); |
17 changes: 0 additions & 17 deletions
17
frontend/packages/console-dynamic-plugin-sdk/src/__tests__/shared-modules-override.spec.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
frontend/packages/console-dynamic-plugin-sdk/src/shared-modules-init.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* eslint-disable global-require */ | ||
/* eslint-disable @typescript-eslint/no-require-imports */ | ||
|
||
import { SharedModuleResolution, RemoteEntryModule } from './types'; | ||
|
||
const modules: SharedModuleResolution = { | ||
'@openshift-console/dynamic-plugin-sdk': async () => () => | ||
require('@console/dynamic-plugin-sdk/src/lib-core'), | ||
'@openshift-console/dynamic-plugin-sdk-internal': async () => () => | ||
require('@console/dynamic-plugin-sdk/src/lib-internal'), | ||
'@openshift-console/dynamic-plugin-sdk-internal-kubevirt': async () => () => | ||
require('@console/dynamic-plugin-sdk/src/lib-internal-kubevirt'), | ||
'@patternfly/react-core': async () => () => require('@patternfly/react-core'), | ||
'@patternfly/react-table': async () => () => require('@patternfly/react-table'), | ||
react: async () => () => require('react'), | ||
'react-helmet': async () => () => require('react-helmet'), | ||
'react-i18next': async () => () => require('react-i18next'), | ||
'react-router': async () => () => require('react-router'), | ||
'react-router-dom': async () => () => require('react-router-dom'), | ||
}; | ||
|
||
const sharedScope = Object.keys(modules).reduce( | ||
(acc, moduleRequest) => ({ | ||
...acc, | ||
[moduleRequest]: { | ||
// The '*' semver range means "this shared module matches all requested versions", | ||
// i.e. make sure the plugin always uses the Console-provided shared module version | ||
'*': { | ||
get: modules[moduleRequest], | ||
// Indicates that Console has already loaded the shared module | ||
loaded: true, | ||
}, | ||
}, | ||
}), | ||
{}, | ||
); | ||
|
||
/** | ||
* At runtime, the Console application will initialize shared modules for each | ||
* dynamic plugin before loading any of the modules exposed by the given plugin. | ||
* | ||
* Currently, module sharing is strictly unidirectional (Console -> plugins). | ||
* | ||
* Note: `__webpack_init_sharing__` global function is available in webpack 5+ builds. | ||
* Once Console gets built with webpack 5, evaluate if we need this global in order to | ||
* allow plugins to attempt to provide shared modules into the application shared scope. | ||
* | ||
* @see https://webpack.js.org/concepts/module-federation/#dynamic-remote-containers | ||
*/ | ||
export const initSharedPluginModules = (entryModule: RemoteEntryModule) => { | ||
if (typeof entryModule.override === 'function') { | ||
// Support plugins built with webpack 5.0.0-beta.16 | ||
entryModule.override(modules); | ||
return; | ||
} | ||
|
||
entryModule.init(sharedScope); | ||
}; |
44 changes: 0 additions & 44 deletions
44
frontend/packages/console-dynamic-plugin-sdk/src/shared-modules-override.ts
This file was deleted.
Oops, something went wrong.
44 changes: 12 additions & 32 deletions
44
frontend/packages/console-dynamic-plugin-sdk/src/shared-modules.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,15 @@ | ||
/** | ||
* Modules shared between the Console application and its dynamic plugins. | ||
*/ | ||
export const sharedPluginModules = { | ||
'@openshift-console/dynamic-plugin-sdk': { | ||
singleton: true, | ||
}, | ||
'@openshift-console/dynamic-plugin-sdk-internal': { | ||
singleton: true, | ||
}, | ||
'@openshift-console/dynamic-plugin-sdk-internal-kubevirt': { | ||
singleton: true, | ||
}, | ||
'@patternfly/react-core': { | ||
singleton: true, | ||
}, | ||
'@patternfly/react-table': { | ||
singleton: true, | ||
}, | ||
react: { | ||
singleton: true, | ||
}, | ||
'react-helmet': { | ||
singleton: true, | ||
}, | ||
'react-i18next': { | ||
singleton: true, | ||
}, | ||
'react-router': { | ||
singleton: true, | ||
}, | ||
'react-router-dom': { | ||
singleton: true, | ||
}, | ||
}; | ||
export const sharedPluginModules = [ | ||
'@openshift-console/dynamic-plugin-sdk', | ||
'@openshift-console/dynamic-plugin-sdk-internal', | ||
'@openshift-console/dynamic-plugin-sdk-internal-kubevirt', | ||
'@patternfly/react-core', | ||
'@patternfly/react-table', | ||
'react', | ||
'react-helmet', | ||
'react-i18next', | ||
'react-router', | ||
'react-router-dom', | ||
]; |
Oops, something went wrong.