Skip to content

Commit 09b792d

Browse files
fix(sdk): allow create script to change attrs (#2682)
Co-authored-by: ScriptedAlchemy <[email protected]>
1 parent efe716f commit 09b792d

File tree

9 files changed

+54
-12
lines changed

9 files changed

+54
-12
lines changed

.changeset/chilled-dolls-arrive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/sdk': patch
3+
---
4+
5+
allow createScript to change script attributes

.changeset/nine-years-brake.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@module-federation/nextjs-mf': patch
3+
'@module-federation/runtime': patch
4+
'@module-federation/sdk': patch
5+
---
6+
7+
connect attrs to create script hook

apps/website-new/docs/en/plugin/dev/index.mdx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ function createScript(args: CreateScriptOptions): HTMLScriptElement | {script?:
496496

497497
type CreateScriptOptions = {
498498
url: string;
499+
attrs?: Record<string, any>;
499500
};
500501
```
501502

@@ -508,11 +509,13 @@ import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtim
508509
const changeScriptAttributePlugin: () => FederationRuntimePlugin = function () {
509510
return {
510511
name: 'change-script-attribute',
511-
createScript({ url }) {
512+
createScript({ url, attrs }) {
512513
if (url === testRemoteEntry) {
513514
let script = document.createElement('script');
514515
script.src = testRemoteEntry;
515-
script.setAttribute('loader-hooks', 'isTrue');
516+
// can modify the attrs object
517+
attrs['loader-hooks'] = 'isTrue';
518+
// or add them to the script
516519
script.setAttribute('crossorigin', 'anonymous');
517520
return script;
518521
}

apps/website-new/docs/zh/plugin/dev/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ function createScript(args: CreateScriptOptions): HTMLScriptElement | {script?:
496496

497497
type CreateScriptOptions = {
498498
url: string;
499+
attrs?: Record<string, any>;
499500
};
500501
```
501502

packages/nextjs-mf/src/plugins/container/runtimePlugin.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ import { FederationRuntimePlugin } from '@module-federation/runtime/types';
22
export default function (): FederationRuntimePlugin {
33
return {
44
name: 'next-internal-plugin',
5+
//@ts-ignore
6+
createScript({ url, attrs }) {
7+
if (typeof window !== 'undefined') {
8+
const script = document.createElement('script');
9+
script.src = url;
10+
script.async = true;
11+
//@ts-ignore
12+
delete attrs.crossorigin;
13+
14+
return { script, timeout: 8000 };
15+
}
16+
return undefined;
17+
},
518
errorLoadRemote({ id, error, from, origin }) {
619
console.error(id, 'offline');
720
const pg = function () {
@@ -81,9 +94,6 @@ export default function (): FederationRuntimePlugin {
8194
remote.entry = `${remote?.entry}?t=${Date.now()}`;
8295
return args;
8396
},
84-
createScript({ url }) {
85-
return;
86-
},
8797
afterResolve(args) {
8898
return args;
8999
},

packages/runtime/src/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export class FederationHost {
8989
[
9090
{
9191
url: string;
92+
attrs?: Record<string, any>;
9293
},
9394
],
9495
CreateScriptHookReturn

packages/runtime/src/module/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ class Module {
3232
const remoteEntryExports = await getRemoteEntry({
3333
remoteInfo: this.remoteInfo,
3434
remoteEntryExports: this.remoteEntryExports,
35-
createScriptHook: (url: string) => {
36-
const res = this.host.loaderHook.lifecycle.createScript.emit({ url });
35+
createScriptHook: (url: string, attrs: any) => {
36+
const res = this.host.loaderHook.lifecycle.createScript.emit({
37+
url,
38+
attrs,
39+
});
3740

3841
if (!res) return;
3942

packages/runtime/src/utils/load.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ export async function loadEntryScript({
4242
name: string;
4343
globalName: string;
4444
entry: string;
45-
createScriptHook?: (url: string) => CreateScriptHookReturn;
45+
createScriptHook?: (
46+
url: string,
47+
attrs?: Record<string, any> | undefined,
48+
) => CreateScriptHookReturn;
4649
}): Promise<RemoteEntryExports> {
4750
const { entryExports: remoteEntryExports } = getRemoteEntryExports(
4851
name,
@@ -117,7 +120,10 @@ export async function getRemoteEntry({
117120
}: {
118121
remoteInfo: RemoteInfo;
119122
remoteEntryExports?: RemoteEntryExports | undefined;
120-
createScriptHook?: (url: string) => CreateScriptHookReturn;
123+
createScriptHook?: (
124+
url: string,
125+
attrs?: Record<string, any> | undefined,
126+
) => CreateScriptHookReturn;
121127
}): Promise<RemoteEntryExports | void> {
122128
const { entry, name, type, entryGlobalName } = remoteInfo;
123129
const uniqueKey = getRemoteEntryUniqueKey(remoteInfo);

packages/sdk/src/dom.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ export function createScript(info: {
3232
cb?: (value: void | PromiseLike<void>) => void;
3333
attrs?: Record<string, any>;
3434
needDeleteScript?: boolean;
35-
createScriptHook?: (url: string) => CreateScriptHookReturn;
35+
createScriptHook?: (
36+
url: string,
37+
attrs?: Record<string, any> | undefined,
38+
) => CreateScriptHookReturn;
3639
}): { script: HTMLScriptElement; needAttach: boolean } {
3740
// Retrieve the existing script element by its src attribute
3841
let script: HTMLScriptElement | null = null;
@@ -55,7 +58,7 @@ export function createScript(info: {
5558
script.type = 'text/javascript';
5659
script.src = info.url;
5760
if (info.createScriptHook) {
58-
const createScriptRes = info.createScriptHook(info.url);
61+
const createScriptRes = info.createScriptHook(info.url, info.attrs);
5962

6063
if (createScriptRes instanceof HTMLScriptElement) {
6164
script = createScriptRes;
@@ -202,7 +205,10 @@ export function loadScript(
202205
url: string,
203206
info: {
204207
attrs?: Record<string, any>;
205-
createScriptHook?: (url: string) => CreateScriptHookReturn;
208+
createScriptHook?: (
209+
url: string,
210+
attrs?: Record<string, any> | undefined,
211+
) => CreateScriptHookReturn;
206212
},
207213
) {
208214
const { attrs = {}, createScriptHook } = info;

0 commit comments

Comments
 (0)