Skip to content

Commit

Permalink
feat: support huge inline-script who might be split into multiple chu…
Browse files Browse the repository at this point in the history
…nks during transfer (#2878)

* feat: support huge inline-script who might be split into multiple chunks during transfer

* Create wise-ravens-prove.md
  • Loading branch information
kuitos authored Jan 8, 2024
1 parent 4c7a773 commit 99bf65f
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 24 deletions.
8 changes: 8 additions & 0 deletions .changeset/wise-ravens-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@qiankunjs/loader": patch
"qiankun": patch
"@qiankunjs/sandbox": patch
"@qiankunjs/shared": patch
---

feat: support huge inline-script who might be split into multiple chunks during transfer
5 changes: 2 additions & 3 deletions packages/loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,14 @@ export async function loadEntry<T>(entry: Entry, container: HTMLElement, opts: L
),
)
.pipeTo(
new WritableDOMStream(container, null, (clone, node) => {
new WritableDOMStream(container, null, (clone) => {
let transformerOpts: AssetsTranspilerOpts = {
fetch,
sandbox,
rawNode: node as unknown as Node,
};

let queueDeferScript: () => void;
const deferScriptMode = isDeferScript(node as unknown as HTMLScriptElement);
const deferScriptMode = isDeferScript(clone as unknown as HTMLScriptElement);
if (deferScriptMode) {
const { deferred, prevDeferred, queue } = prepareDeferredQueue(deferQueue);
transformerOpts = {
Expand Down
33 changes: 24 additions & 9 deletions packages/loader/src/writable-dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ type WritableDOM = {
new (
target: ParentNode,
previousSibling?: ChildNode | null,
assetTransformer?: (clone: Node, node: Node) => Node,
assetTransformer?: (clone: Node) => Node,
): WritableStream<string>;
(
target: ParentNode,
previousSibling?: ChildNode | null,
assetTransformer?: (clone: Node, node: Node) => Node,
assetTransformer?: <T extends Node>(clone: T) => T,
): Writable;
};
function writableDOM(
this: unknown,
target: ParentNode,
previousSibling?: ChildNode | null,
assetTransformer?: <T extends Node>(clone: T, node: T) => T,
assetTransformer?: <T extends Node>(clone: T) => T,
): Writable | WritableStream<string> {
if (this instanceof writableDOM) {
return new WritableStream(writableDOM(target, previousSibling, assetTransformer));
Expand Down Expand Up @@ -85,7 +85,7 @@ function writableDOM(
}
},
close() {
appendInlineTextIfNeeded(pendingText, inlineHostNode);
appendInlineTextIfNeeded(pendingText, inlineHostNode, assetTransformer);

return isBlocked ? new Promise<void>((_) => (resolve = _)) : Promise.resolve();
},
Expand All @@ -102,7 +102,7 @@ function writableDOM(
while ((node = walker.nextNode())) {
const link = getPreloadLink((scanNode = node));
if (link) {
const transformedLink = typeof assetTransformer === 'function' ? assetTransformer(link, link) : link;
const transformedLink = typeof assetTransformer === 'function' ? assetTransformer(link) : link;
transformedLink.onload = transformedLink.onerror = () => target.removeChild(transformedLink);
target.insertBefore(transformedLink, nextSibling);
}
Expand Down Expand Up @@ -141,11 +141,11 @@ function writableDOM(
if (isInlineHost(parentNode!)) {
inlineHostNode = parentNode;
} else {
appendInlineTextIfNeeded(previousPendingText, inlineHostNode);
appendInlineTextIfNeeded(previousPendingText, inlineHostNode, assetTransformer);
inlineHostNode = null;

if (typeof assetTransformer === 'function') {
clone = assetTransformer(clone, node);
clone = assetTransformer(clone);
}

if (parentNode === target) {
Expand Down Expand Up @@ -234,9 +234,24 @@ function getPreloadLink(node: any) {
return link;
}

function appendInlineTextIfNeeded(pendingText: Text | null, inlineTextHostNode: Node | null) {
function appendInlineTextIfNeeded(
pendingText: Text | null,
inlineTextHostNode: Node | null,
assetTransformer?: <T extends Node>(clone: T) => T,
) {
if (pendingText && inlineTextHostNode) {
inlineTextHostNode.appendChild(pendingText);
let textNode = pendingText;

if (typeof assetTransformer === 'function') {
// copy the text node and host node and then get the transformed text node, thus we can append the transformed text node to live host
const graftedHost = document.importNode(inlineTextHostNode, false);
const graftedText = document.importNode(textNode, false);
graftedHost.appendChild(graftedText);
const transformedHost = assetTransformer(graftedHost);
textNode = transformedHost.firstChild as Text;
}

inlineTextHostNode.appendChild(textNode);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/qiankun/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = '3.0.0-rc.15';
export { version } from '../package.json';
38 changes: 38 additions & 0 deletions packages/sandbox/src/core/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export const globalsInBrowser = [
"CloseEvent",
"Comment",
"CompositionEvent",
"CompressionStream",
"confirm",
"console",
"ConstantSourceNode",
Expand Down Expand Up @@ -168,6 +169,7 @@ export const globalsInBrowser = [
"DataTransfer",
"DataTransferItem",
"DataTransferItemList",
"DecompressionStream",
"defaultstatus",
"defaultStatus",
"DelayNode",
Expand Down Expand Up @@ -358,6 +360,7 @@ export const globalsInBrowser = [
"MediaStream",
"MediaStreamAudioDestinationNode",
"MediaStreamAudioSourceNode",
"MediaStreamConstraints",
"MediaStreamEvent",
"MediaStreamTrack",
"MediaStreamTrackEvent",
Expand Down Expand Up @@ -555,7 +558,12 @@ export const globalsInBrowser = [
"queueMicrotask",
"RadioNodeList",
"Range",
"ReadableByteStreamController",
"ReadableStream",
"ReadableStreamBYOBReader",
"ReadableStreamBYOBRequest",
"ReadableStreamDefaultController",
"ReadableStreamDefaultReader",
"registerProcessor",
"RemotePlayback",
"removeEventListener",
Expand Down Expand Up @@ -729,21 +737,25 @@ export const globalsInBrowser = [
"TaskAttributionTiming",
"Text",
"TextDecoder",
"TextDecoderStream",
"TextEncoder",
"TextEncoderStream",
"TextEvent",
"TextMetrics",
"TextTrack",
"TextTrackCue",
"TextTrackCueList",
"TextTrackList",
"TimeRanges",
"ToggleEvent",
"toolbar",
"top",
"Touch",
"TouchEvent",
"TouchList",
"TrackEvent",
"TransformStream",
"TransformStreamDefaultController",
"TransitionEvent",
"TreeWalker",
"UIEvent",
Expand Down Expand Up @@ -778,6 +790,8 @@ export const globalsInBrowser = [
"Window",
"Worker",
"WritableStream",
"WritableStreamDefaultController",
"WritableStreamDefaultWriter",
"XMLDocument",
"XMLHttpRequest",
"XMLHttpRequestEventTarget",
Expand All @@ -786,6 +800,30 @@ export const globalsInBrowser = [
"XPathEvaluator",
"XPathExpression",
"XPathResult",
"XRAnchor",
"XRBoundedReferenceSpace",
"XRCPUDepthInformation",
"XRDepthInformation",
"XRFrame",
"XRInputSource",
"XRInputSourceArray",
"XRInputSourceEvent",
"XRInputSourcesChangeEvent",
"XRPose",
"XRReferenceSpace",
"XRReferenceSpaceEvent",
"XRRenderState",
"XRRigidTransform",
"XRSession",
"XRSessionEvent",
"XRSpace",
"XRSystem",
"XRView",
"XRViewerPose",
"XRViewport",
"XRWebGLBinding",
"XRWebGLDepthInformation",
"XRWebGLLayer",
"XSLTProcessor"
];

2 changes: 0 additions & 2 deletions packages/sandbox/src/patchers/dynamicAppend/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ export function getOverwrittenAppendChildOrInsertBefore(
const transpiledStyleSheetElement = nodeTransformer(stylesheetElement, {
fetch,
sandbox,
rawNode: stylesheetElement,
});

const stylesheetTargetDetached = !document.contains(this);
Expand Down Expand Up @@ -196,7 +195,6 @@ export function getOverwrittenAppendChildOrInsertBefore(
let transformerOpts: AssetsTranspilerOpts = {
fetch,
sandbox,
rawNode: scriptElement,
};

let queueSyncScript: () => void;
Expand Down
10 changes: 2 additions & 8 deletions packages/shared/src/assets-transpilers/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ export const preTranspile = (
}

if (isValidJavaScriptType(type) && sandbox) {
const rawNode = opts.rawNode as HTMLScriptElement;
const scriptNode = script.textContent ? script : rawNode.childNodes[0];

const code = scriptNode.textContent;
const code = script.textContent;
if (code) {
return {
mode: Mode.INLINE_CODE_IN_SANDBOX,
Expand Down Expand Up @@ -152,11 +149,8 @@ export default function transpileScript(
}

case Mode.INLINE_CODE_IN_SANDBOX: {
const rawNode = opts.rawNode as HTMLScriptElement;
const scriptNode = script.textContent ? script : rawNode.childNodes[0];
const { code } = result;

scriptNode.textContent = sandbox!.makeEvaluateFactory(code);
script.textContent = sandbox!.makeEvaluateFactory(code);
// mark the script have consumed
script.dataset.consumed = 'true';

Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/assets-transpilers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type BaseTranspilerOpts = BaseLoaderOpts & {
};
};

export type AssetsTranspilerOpts = BaseTranspilerOpts & { rawNode: Node };
export type AssetsTranspilerOpts = BaseTranspilerOpts;

export type NodeTransformer = <T extends Node>(node: T, opts: Omit<AssetsTranspilerOpts, 'moduleResolver'>) => T;

Expand Down

0 comments on commit 99bf65f

Please sign in to comment.