From 0f1f6fcf15cacd6c697a1031aecbe5980b96943a Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Tue, 18 Jun 2024 20:50:44 -0400 Subject: [PATCH] handling all head tag bundle mapping --- .../plugins/resource/plugin-standard-html.js | 21 +++++++++++-------- .../src/pages/index.html | 12 ++++++++++- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/packages/cli/src/plugins/resource/plugin-standard-html.js b/packages/cli/src/plugins/resource/plugin-standard-html.js index 702878a3f..456a52443 100644 --- a/packages/cli/src/plugins/resource/plugin-standard-html.js +++ b/packages/cli/src/plugins/resource/plugin-standard-html.js @@ -208,12 +208,12 @@ class StandardHtmlResource extends ResourceInterface { for (const pageResource of pageResources) { const keyedResource = this.compilation.resources.get(pageResource); - const { contents, src, type, optimizationAttr, optimizedFileContents, optimizedFileName, rawAttributes } = keyedResource; + const { contents, src, type, optimizationAttr, optimizedFileContents, optimizedFileName } = keyedResource; if (src) { - const tag = root.querySelectorAll('script').find(script => script.getAttribute('src') === src); - if (type === 'script') { + const tag = root.querySelectorAll('script').find(script => script.getAttribute('src') === src); + if (!optimizationAttr && optimization === 'default') { const optimizedFilePath = `${basePath}/${optimizedFileName}`; @@ -223,18 +223,19 @@ class StandardHtmlResource extends ResourceInterface { `); } else if (optimizationAttr === 'inline' || optimization === 'inline') { - const isModule = rawAttributes.indexOf('type="module') >= 0 ? ' type="module"' : ''; + const isModule = tag.rawAttrs.indexOf('type="module') >= 0 ? ' type="module"' : ''; - body = body.replace(``, ` + body = body.replace(``, ` `); } else if (optimizationAttr === 'static' || optimization === 'static') { - // TODO could we not get these rawAttrs pre-formatted in modelResource the first time??? body = body.replace(``, ''); } } else if (type === 'link') { + const tag = root.querySelectorAll('link').find(link => link.getAttribute('href') === src); + if (!optimizationAttr && (optimization !== 'none' && optimization !== 'inline')) { const optimizedFilePath = `${basePath}/${optimizedFileName}`; @@ -248,11 +249,11 @@ class StandardHtmlResource extends ResourceInterface { // when pre-rendering, puppeteer normalizes everything to // but if not using pre-rendering, then it could come out as // not great, but best we can do for now until #742 - body = body.replace(``, ` + body = body.replace(``, ` - `).replace(``, ` + `).replace(``, ` @@ -261,8 +262,10 @@ class StandardHtmlResource extends ResourceInterface { } } else { if (type === 'script') { + const tag = root.querySelectorAll('script').find(script => script.innerHTML === contents); + if (optimizationAttr === 'static' || optimization === 'static') { - body = body.replace(``, ''); + body = body.replace(``, ''); } else if (optimizationAttr === 'none') { body = body.replace(contents, contents.replace(/\.\//g, `${basePath}/`).replace(/\$/g, '$$$')); } else { diff --git a/packages/cli/test/cases/build.config.optimization-overrides/src/pages/index.html b/packages/cli/test/cases/build.config.optimization-overrides/src/pages/index.html index acbbbd3d2..dd97780c5 100644 --- a/packages/cli/test/cases/build.config.optimization-overrides/src/pages/index.html +++ b/packages/cli/test/cases/build.config.optimization-overrides/src/pages/index.html @@ -8,7 +8,17 @@ src="../components/header.js" data-gwd-opt="static" > - + +