Skip to content
Open
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
22 changes: 17 additions & 5 deletions libs/mf-runtime/src/lib/loader/dynamic-federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ declare const __webpack_share_scopes__: { default: Scope };
type ContainerMap = { [key: string]: Container };

const containerMap: ContainerMap = {};
const remoteMap = {};
const remoteMap: { [key: string]: boolean } = {};

let isDefaultScopeInitialized = false;

Expand Down Expand Up @@ -70,6 +70,7 @@ export type LoadRemoteEntryScriptOptions = {
type?: 'script';
remoteEntry: string;
remoteName: string;
nonce?: string;
};

export type LoadRemoteEntryEsmOptions = {
Expand All @@ -86,14 +87,19 @@ export async function loadRemoteEntry(
): Promise<void>;
export async function loadRemoteEntry(
remoteEntryOrOptions: string | LoadRemoteEntryOptions,
remoteName?: string
remoteName?: string,
nonce?: string
): Promise<void> {
if (typeof remoteEntryOrOptions === 'string') {
const remoteEntry = remoteEntryOrOptions;
return await loadRemoteScriptEntry(remoteEntry, remoteName);
return await loadRemoteScriptEntry(remoteEntry, remoteName, nonce);
} else if (remoteEntryOrOptions.type === 'script') {
const options = remoteEntryOrOptions;
return await loadRemoteScriptEntry(options.remoteEntry, options.remoteName);
return await loadRemoteScriptEntry(
options.remoteEntry,
options.remoteName,
options.nonce
);
} else if (remoteEntryOrOptions.type === 'module') {
const options = remoteEntryOrOptions;
await loadRemoteModuleEntry(options.remoteEntry);
Expand All @@ -114,7 +120,8 @@ async function loadRemoteModuleEntry(remoteEntry: string): Promise<void> {

async function loadRemoteScriptEntry(
remoteEntry: string,
remoteName: string
remoteName: string,
nonce?: string
): Promise<void> {
return new Promise<void>((resolve, reject) => {
// Is remoteEntry already loaded?
Expand All @@ -125,6 +132,9 @@ async function loadRemoteScriptEntry(

const script = document.createElement('script');
script.src = remoteEntry;
if (nonce) {
script.setAttribute('nonce', nonce);
}

script.onerror = reject;

Expand All @@ -149,6 +159,7 @@ export type LoadRemoteModuleScriptOptions = {
remoteEntry?: string;
remoteName: string;
exposedModule: string;
nonce?: string;
};

export type LoadRemoteModuleEsmOptions = {
Expand Down Expand Up @@ -218,6 +229,7 @@ export async function loadRemoteModule<T = any>(
type: 'script',
remoteEntry: options.remoteEntry,
remoteName: options.remoteName,
nonce: options.nonce,
};
key = options.remoteName;
} else if (options.type === 'module') {
Expand Down