-
-
Notifications
You must be signed in to change notification settings - Fork 289
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
feat(webpack-bundler-runtime): create container function #2440
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8a8c073
feat(webpack-bundler-runtime): create container function
ScriptedAlchemy 9ac5a4f
Merge branch 'main' into feat/create-container
ScriptedAlchemy 74a7e83
Merge branch 'main' into feat/create-container
ScriptedAlchemy 94e3631
refactor(webpack-bundler-runtime): clean up types
ScriptedAlchemy 221e75a
Merge branch 'main' into feat/create-container
ScriptedAlchemy 916882d
Merge branch 'main' into feat/create-container
ScriptedAlchemy ff6c8d2
chore: changeset
ScriptedAlchemy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,271 @@ | ||
import bundler_runtime_base from './index'; | ||
import type { Options } from '@module-federation/runtime/types'; | ||
|
||
interface ExposesConfig { | ||
/** | ||
* Request to a module that should be exposed by this container. | ||
*/ | ||
import: ExposesItem | ExposesItems; | ||
/** | ||
* Custom chunk name for the exposed module. | ||
*/ | ||
name?: string; | ||
} | ||
|
||
type Exposes = (ExposesItem | ExposesObject)[] | ExposesObject; | ||
/** | ||
* Module that should be exposed by this container. | ||
*/ | ||
type ExposesItem = string; | ||
/** | ||
* Modules that should be exposed by this container. | ||
*/ | ||
type ExposesItems = ExposesItem[]; | ||
|
||
interface ExposesObject { | ||
/** | ||
* Modules that should be exposed by this container. | ||
*/ | ||
[k: string]: ExposesConfig | ExposesItem | ExposesItems; | ||
} | ||
|
||
interface ExtendedOptions extends Options { | ||
exposes: { [key: string]: () => Promise<() => any> }; | ||
} | ||
|
||
export const createContainer = async (federationOptions: ExtendedOptions) => { | ||
// await instantiatePatch(federationOptions, true); | ||
const { exposes, name, remotes = [], shared, plugins } = federationOptions; | ||
|
||
const __webpack_modules__ = { | ||
'./node_modules/.federation/entry.1f2288102e035e2ed66b2efaf60ad043.js': ( | ||
//@ts-ignore | ||
module, | ||
//@ts-ignore | ||
__webpack_exports__, | ||
//@ts-ignore | ||
__webpack_require__, | ||
) => { | ||
__webpack_require__.r(__webpack_exports__); | ||
const bundler_runtime = __webpack_require__.n(bundler_runtime_base); | ||
const prevFederation = __webpack_require__.federation; | ||
__webpack_require__.federation = {}; | ||
for (const key in bundler_runtime()) { | ||
__webpack_require__.federation[key] = bundler_runtime()[key]; | ||
} | ||
for (const key in prevFederation) { | ||
__webpack_require__.federation[key] = prevFederation[key]; | ||
} | ||
if (!__webpack_require__.federation.instance) { | ||
const pluginsToAdd = plugins || []; | ||
__webpack_require__.federation.initOptions.plugins = __webpack_require__ | ||
.federation.initOptions.plugins | ||
? __webpack_require__.federation.initOptions.plugins.concat( | ||
pluginsToAdd, | ||
) | ||
: pluginsToAdd; | ||
__webpack_require__.federation.instance = | ||
__webpack_require__.federation.runtime.init( | ||
__webpack_require__.federation.initOptions, | ||
); | ||
if (__webpack_require__.federation.attachShareScopeMap) { | ||
__webpack_require__.federation.attachShareScopeMap( | ||
__webpack_require__, | ||
); | ||
} | ||
if (__webpack_require__.federation.installInitialConsumes) { | ||
__webpack_require__.federation.installInitialConsumes(); | ||
} | ||
} | ||
}, | ||
//@ts-ignore | ||
'webpack/container/entry/createContainer': ( | ||
//@ts-ignore | ||
|
||
module, | ||
//@ts-ignore | ||
exports, | ||
//@ts-ignore | ||
__webpack_require__, | ||
) => { | ||
const moduleMap = {}; | ||
for (const key in exposes) { | ||
if (Object.prototype.hasOwnProperty.call(exposes, key)) { | ||
//@ts-ignore | ||
moduleMap[key] = () => | ||
Promise.resolve(exposes[key]()).then((m) => () => m); | ||
} | ||
} | ||
//@ts-ignore | ||
const get = (module, getScope) => { | ||
__webpack_require__.R = getScope; | ||
getScope = __webpack_require__.o(moduleMap, module) | ||
? //@ts-ignore | ||
moduleMap[module]() | ||
: Promise.resolve().then(() => { | ||
throw new Error( | ||
`Module "${module}" does not exist in container.`, | ||
); | ||
}); | ||
__webpack_require__.R = undefined; | ||
return getScope; | ||
}; | ||
//@ts-ignore | ||
const init = (shareScope, initScope, remoteEntryInitOptions) => { | ||
return __webpack_require__.federation.bundlerRuntime.initContainerEntry( | ||
{ | ||
webpackRequire: __webpack_require__, | ||
shareScope: shareScope, | ||
initScope: initScope, | ||
remoteEntryInitOptions: remoteEntryInitOptions, | ||
shareScopeKey: 'default', | ||
}, | ||
); | ||
}; | ||
__webpack_require__( | ||
'./node_modules/.federation/entry.1f2288102e035e2ed66b2efaf60ad043.js', | ||
); | ||
|
||
// This exports getters to disallow modifications | ||
__webpack_require__.d(exports, { | ||
get: () => get, | ||
init: () => init, | ||
moduleMap: () => moduleMap, | ||
}); | ||
}, | ||
}; | ||
|
||
const __webpack_module_cache__ = {}; | ||
|
||
//@ts-ignore | ||
const __webpack_require__ = (moduleId) => { | ||
//@ts-ignore | ||
let cachedModule = __webpack_module_cache__[moduleId]; | ||
if (cachedModule !== undefined) { | ||
return cachedModule.exports; | ||
} | ||
//@ts-ignore | ||
let module = (__webpack_module_cache__[moduleId] = { | ||
id: moduleId, | ||
loaded: false, | ||
exports: {}, | ||
}); | ||
|
||
const execOptions = { | ||
id: moduleId, | ||
module: module, | ||
//@ts-ignore | ||
factory: __webpack_modules__[moduleId], | ||
require: __webpack_require__, | ||
}; | ||
__webpack_require__.i.forEach((handler) => { | ||
handler(execOptions); | ||
}); | ||
module = execOptions.module; | ||
execOptions.factory.call( | ||
module.exports, | ||
module, | ||
module.exports, | ||
execOptions.require, | ||
); | ||
|
||
module.loaded = true; | ||
|
||
return module.exports; | ||
}; | ||
|
||
__webpack_require__.m = __webpack_modules__; | ||
__webpack_require__.c = __webpack_module_cache__; | ||
//@ts-ignore | ||
__webpack_require__.i = []; | ||
|
||
//@ts-ignore | ||
if (!__webpack_require__.federation) { | ||
__webpack_require__.federation = { | ||
initOptions: { | ||
name: name, | ||
//@ts-ignore | ||
remotes: remotes.map((remote) => ({ | ||
type: remote.type, | ||
alias: remote.alias, | ||
name: remote.name, | ||
//@ts-ignore | ||
entry: remote.entry, | ||
shareScope: remote.shareScope || 'default', | ||
})), | ||
}, | ||
chunkMatcher: () => true, | ||
rootOutputDir: '', | ||
initialConsumes: undefined, | ||
bundlerRuntimeOptions: {}, | ||
}; | ||
} | ||
//@ts-ignore | ||
__webpack_require__.n = (module) => { | ||
const getter = | ||
module && module.__esModule ? () => module['default'] : () => module; | ||
__webpack_require__.d(getter, { a: getter }); | ||
return getter; | ||
}; | ||
|
||
//@ts-ignore | ||
__webpack_require__.d = (exports, definition) => { | ||
for (const key in definition) { | ||
if ( | ||
__webpack_require__.o(definition, key) && | ||
!__webpack_require__.o(exports, key) | ||
) { | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: definition[key], | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
__webpack_require__.f = {}; | ||
|
||
__webpack_require__.g = (() => { | ||
if (typeof globalThis === 'object') return globalThis; | ||
try { | ||
return this || new Function('return this')(); | ||
} catch (e) { | ||
if (typeof window === 'object') return window; | ||
} | ||
})(); | ||
|
||
//@ts-ignore | ||
__webpack_require__.o = (obj, prop) => | ||
Object.prototype.hasOwnProperty.call(obj, prop); | ||
//@ts-ignore | ||
__webpack_require__.r = (exports) => { | ||
if (typeof Symbol !== 'undefined' && Symbol.toStringTag) { | ||
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); | ||
} | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}; | ||
|
||
//@ts-ignore | ||
__webpack_require__.federation.initOptions.shared = shared; | ||
__webpack_require__.S = {}; | ||
const initPromises = {}; | ||
const initTokens = {}; | ||
//@ts-ignore | ||
__webpack_require__.I = (name, initScope) => { | ||
//@ts-ignore | ||
return __webpack_require__.federation.bundlerRuntime.I({ | ||
shareScopeName: name, | ||
webpackRequire: __webpack_require__, | ||
initPromises: initPromises, | ||
initTokens: initTokens, | ||
initScope: initScope, | ||
}); | ||
}; | ||
|
||
const __webpack_exports__ = __webpack_require__( | ||
'webpack/container/entry/createContainer', | ||
); | ||
const __webpack_exports__get = __webpack_exports__.get; | ||
const __webpack_exports__init = __webpack_exports__.init; | ||
return __webpack_exports__; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Commented out, used to support import maps, but should be implemented via sdk/import-maps instead of a patch on function
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.
Removing patch since something else should be cone for async container / initialization.