From 81ed0bf3e30088c07cd348117c8832fa5d446b22 Mon Sep 17 00:00:00 2001 From: maoxiaoke Date: Sun, 22 Aug 2021 10:04:26 +0800 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20compatible=20angular?= =?UTF-8?q?12?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/util/handleAssets.ts | 23 ++++++++++++----------- tests/handleAssets.spec.tsx | 2 ++ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/util/handleAssets.ts b/src/util/handleAssets.ts index 86d5fc72..31718585 100644 --- a/src/util/handleAssets.ts +++ b/src/util/handleAssets.ts @@ -15,6 +15,7 @@ const STYLESHEET_LINK_TYPE = 'stylesheet'; const cachedScriptsContent: object = {}; const cachedStyleContent: object = {}; +const cachedProcessedContent: object = {}; const defaultFetch = window?.fetch.bind(window); @@ -340,7 +341,10 @@ export function processHtml(html: string, entry?: string): ProcessedContent { const domContent = (new DOMParser()).parseFromString(html.replace(COMMENT_REGEX, ''), 'text/html'); - if (entry) { + /** + * If `` is alread exists. + */ + if (entry && !domContent.getElementsByTagName('base').length) { // add base URI for absolute resource. see more https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base const base = document.createElement('base'); base.href = entry; @@ -402,8 +406,6 @@ export function processHtml(html: string, entry?: string): ProcessedContent { }; } -const cachedProcessedContent: object = {}; - export async function getEntryAssets({ root, entry, @@ -420,9 +422,10 @@ export async function getEntryAssets({ fetch?: Fetch; assertsCached?: boolean; }) { - let cachedContent = cachedProcessedContent[assetsCacheKey]; + const cachedContent = cachedProcessedContent[assetsCacheKey]; + let htmlContent = entryContent; + if (!cachedContent) { - let htmlContent = entryContent; if (!htmlContent && entry) { if (!fetch) { warn('Current environment does not support window.fetch, please use custom fetch'); @@ -431,20 +434,18 @@ export async function getEntryAssets({ ); } - const res = await fetch(entry); - htmlContent = await res.text(); + htmlContent = await fetch(entry).then((res) => res.text()); } - cachedContent = processHtml(htmlContent, entry || href); - cachedProcessedContent[assetsCacheKey] = cachedContent; + cachedProcessedContent[assetsCacheKey] = htmlContent; } - const { html } = cachedContent; + const { html, assets } = processHtml(cachedContent ?? htmlContent, entry || href); if (root) { root.appendChild(html); } - return cachedContent.assets; + return assets; } export function getAssetsNode(): Array { diff --git a/tests/handleAssets.spec.tsx b/tests/handleAssets.spec.tsx index adcd0dc9..a588c12b 100644 --- a/tests/handleAssets.spec.tsx +++ b/tests/handleAssets.spec.tsx @@ -297,6 +297,8 @@ describe('getEntryAssets', () => { assetsCacheKey: '/test', }); + console.log('fsfsfsfsdf', assets) + expect(assets).toStrictEqual({ cssList: [ { From 4defed5e91023e2ae3eda661737e6915cf89d222 Mon Sep 17 00:00:00 2001 From: maoxiaoke Date: Wed, 1 Sep 2021 15:27:11 +0800 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20handle=20base=20elem?= =?UTF-8?q?ent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/util/handleAssets.ts | 38 +++++++++++++++++++++++++++---------- tests/handleAssets.spec.tsx | 36 +++++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/util/handleAssets.ts b/src/util/handleAssets.ts index 31718585..891be778 100644 --- a/src/util/handleAssets.ts +++ b/src/util/handleAssets.ts @@ -341,14 +341,30 @@ export function processHtml(html: string, entry?: string): ProcessedContent { const domContent = (new DOMParser()).parseFromString(html.replace(COMMENT_REGEX, ''), 'text/html'); - /** - * If `` is alread exists. - */ - if (entry && !domContent.getElementsByTagName('base').length) { - // add base URI for absolute resource. see more https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base - const base = document.createElement('base'); - base.href = entry; - domContent.getElementsByTagName('head')[0].appendChild(base); + /* + * When using DOMParser,the origin of relative path of `