Skip to content

Commit

Permalink
handling all head tag bundle mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Jun 26, 2024
1 parent 9d59f95 commit 0f1f6fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
21 changes: 12 additions & 9 deletions packages/cli/src/plugins/resource/plugin-standard-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

Expand All @@ -223,18 +223,19 @@ class StandardHtmlResource extends ResourceInterface {
<link rel="modulepreload" href="${optimizedFilePath}" as="script">
`);
} 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(`<script ${rawAttributes}></script>`, `
body = body.replace(`<script ${tag.rawAttrs}></script>`, `
<script ${isModule}>
${optimizedFileContents.replace(/\.\//g, `${basePath}/`).replace(/\$/g, '$$$')}
</script>
`);
} else if (optimizationAttr === 'static' || optimization === 'static') {
// TODO could we not get these rawAttrs pre-formatted in modelResource the first time???
body = body.replace(`<script ${tag.rawAttrs}></script>`, '');
}
} 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}`;

Expand All @@ -248,11 +249,11 @@ class StandardHtmlResource extends ResourceInterface {
// when pre-rendering, puppeteer normalizes everything to <link .../>
// but if not using pre-rendering, then it could come out as <link ...></link>
// not great, but best we can do for now until #742
body = body.replace(`<link ${rawAttributes}>`, `
body = body.replace(`<link ${tag.rawAttrs}>`, `
<style>
${optimizedFileContents}
</style>
`).replace(`<link ${rawAttributes}/>`, `
`).replace(`<link ${tag.rawAttrs}/>`, `
<style>
${optimizedFileContents}
</style>
Expand All @@ -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(`<script ${rawAttributes}>${contents.replace(/\.\//g, '/').replace(/\$/g, '$$$')}</script>`, '');
body = body.replace(`<script ${tag.rawAttrs}>${contents.replace(/\.\//g, '/').replace(/\$/g, '$$$')}</script>`, '');
} else if (optimizationAttr === 'none') {
body = body.replace(contents, contents.replace(/\.\//g, `${basePath}/`).replace(/\$/g, '$$$'));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@
src="../components/header.js"
data-gwd-opt="static"
></script>
<link rel="stylesheet" href="/styles/theme.css" data-gwd-opt="inline"/>
<link
rel="stylesheet"
href="/styles/theme.css"
data-gwd-opt="inline"
/>
<script
data-gwd-opt="static"
type="module"
>
console.log('I should not be here');
</script>
</head>

<body>
Expand Down

0 comments on commit 0f1f6fc

Please sign in to comment.