Skip to content

Commit

Permalink
fix: 修复defer脚本无法关闭fiber模式 (#166)
Browse files Browse the repository at this point in the history
close #166
  • Loading branch information
yiludege authored Sep 22, 2022
1 parent 5c20d56 commit ef58f48
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
14 changes: 8 additions & 6 deletions packages/wujie-core/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function rewriteAppendOrInsertChild(opts: {
const { rawDOMAppendOrInsertBefore, wujieId } = opts;
const sandbox = getWujieById(wujieId);

const { styleSheetElements, replace, fetch, plugins, iframe, lifecycles, proxyLocation } = sandbox;
const { styleSheetElements, replace, fetch, plugins, iframe, lifecycles, proxyLocation, fiber } = sandbox;

if (!isHijackingTag(element.tagName) || !wujieId) {
const res = rawDOMAppendOrInsertBefore.call(this, element, refChild) as T;
Expand Down Expand Up @@ -238,13 +238,13 @@ function rewriteAppendOrInsertChild(opts: {
crossoriginType: crossOrigin || "",
ignore: isMatchUrl(src, getEffectLoaders("jsIgnores", plugins)),
} as ScriptObject;
getExternalScripts([scriptOptions], fetch, lifecycles.loadError).forEach((scriptResult) =>
getExternalScripts([scriptOptions], fetch, lifecycles.loadError, fiber).forEach((scriptResult) =>
scriptResult.contentPromise.then(
(content) => {
if (sandbox.execQueue === null) return warn(WUJIE_TIPS_REPEAT_RENDER);
const execQueueLength = sandbox.execQueue?.length;
sandbox.execQueue.push(() =>
sandbox.fiber
fiber
? requestIdleCallback(() => {
execScript({ ...scriptResult, content });
})
Expand All @@ -262,9 +262,11 @@ function rewriteAppendOrInsertChild(opts: {
} else {
const execQueueLength = sandbox.execQueue?.length;
sandbox.execQueue.push(() =>
requestIdleCallback(() => {
insertScriptToIframe({ src: null, content: text }, sandbox.iframe.contentWindow);
})
fiber
? requestIdleCallback(() => {
insertScriptToIframe({ src: null, content: text }, sandbox.iframe.contentWindow);
})
: insertScriptToIframe({ src: null, content: text }, sandbox.iframe.contentWindow)
);
if (!execQueueLength) sandbox.execQueue.shift()();
}
Expand Down
12 changes: 9 additions & 3 deletions packages/wujie-core/src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface htmlParseResult {

type ImportEntryOpts = {
fetch?: typeof window.fetch;
fiber?: boolean;
plugins?: Array<plugin>;
loadError?: loadErrorHandler;
};
Expand Down Expand Up @@ -161,7 +162,8 @@ export function getExternalStyleSheets(
export function getExternalScripts(
scripts: ScriptObject[],
fetch: (input: RequestInfo, init?: RequestInit) => Promise<Response> = defaultFetch,
loadError: loadErrorHandler
loadError: loadErrorHandler,
fiber: boolean
): ScriptResultList {
// module should be requested in iframe
return scripts.map((script) => {
Expand All @@ -170,7 +172,9 @@ export function getExternalScripts(
// async
if ((async || defer) && src && !module) {
contentPromise = new Promise((resolve, reject) =>
requestIdleCallback(() => fetchAssets(src, scriptCache, fetch, false, loadError).then(resolve, reject))
fiber
? requestIdleCallback(() => fetchAssets(src, scriptCache, fetch, false, loadError).then(resolve, reject))
: fetchAssets(src, scriptCache, fetch, false, loadError).then(resolve, reject)
);
// module || ignore
} else if ((module && src) || ignore) {
Expand All @@ -188,6 +192,7 @@ export function getExternalScripts(

export default function importHTML(url: string, opts?: ImportEntryOpts): Promise<htmlParseResult> {
const fetch = opts.fetch ?? defaultFetch;
const fiber = opts.fiber ?? true;
const { plugins, loadError } = opts;
const htmlLoader = plugins ? compose(plugins.map((plugin) => plugin.htmlLoader)) : defaultGetTemplate;
const jsExcludes = getEffectLoaders("jsExcludes", plugins);
Expand Down Expand Up @@ -217,7 +222,8 @@ export default function importHTML(url: string, opts?: ImportEntryOpts): Promise
.filter((script) => !script.src || !isMatchUrl(script.src, jsExcludes))
.map((script) => ({ ...script, ignore: script.src && isMatchUrl(script.src, jsIgnores) })),
fetch,
loadError
loadError,
fiber
),
getExternalStyleSheets: () =>
getExternalStyleSheets(
Expand Down
3 changes: 3 additions & 0 deletions packages/wujie-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export async function startApp(startOptions: startOptions): Promise<Function | v
fetch: fetch || window.fetch,
plugins: sandbox.plugins,
loadError: sandbox.lifecycles.loadError,
fiber,
});
await sandbox.start(getExternalScripts);
}
Expand Down Expand Up @@ -239,6 +240,7 @@ export async function startApp(startOptions: startOptions): Promise<Function | v
fetch: fetch || window.fetch,
plugins: newSandbox.plugins,
loadError: newSandbox.lifecycles.loadError,
fiber,
});

const processedHtml = await processCssLoader(newSandbox, template, getExternalStyleSheets);
Expand Down Expand Up @@ -271,6 +273,7 @@ export function preloadApp(preOptions: preOptions): void {
fetch: fetch || window.fetch,
plugins: sandbox.plugins,
loadError: sandbox.lifecycles.loadError,
fiber,
});
const processedHtml = await processCssLoader(sandbox, template, getExternalStyleSheets);
await sandbox.active({ url, props, prefix, alive, template: processedHtml, fetch, replace });
Expand Down

0 comments on commit ef58f48

Please sign in to comment.