Skip to content

Commit 5895a18

Browse files
committed
fix(compile-web): added the ability to update link tags with rel='modulepreload'
1 parent f7765c6 commit 5895a18

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

src/commands/web/internal/getTags.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ export const getSasjsTags = (parsedHtml: JSDOM) =>
66
export const getScriptTags = (parsedHtml: JSDOM) =>
77
Array.from(parsedHtml.window.document.getElementsByTagName('script'))
88

9+
export const getModulePreload = (parsedHtml: JSDOM) =>
10+
Array.from(parsedHtml.window.document.getElementsByTagName('link')).filter(
11+
(s) => s.getAttribute('rel') === 'modulepreload'
12+
)
13+
914
export const getStyleInPageTags = (parsedHtml: JSDOM) =>
1015
Array.from(parsedHtml.window.document.getElementsByTagName('style'))
1116

src/commands/web/internal/updateAllTags.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
getSourceTags,
99
getStyleInPageTags,
1010
getSasjsTags,
11-
getManifestTags
11+
getManifestTags,
12+
getModulePreload
1213
} from './getTags'
1314
import { updateScriptTag } from './updateScriptTag'
1415
import { updateLinkTag } from './updateLinkTag'
@@ -51,6 +52,15 @@ export const updateAllTags = async (
5152
).catch((e) => assetsNotFound.push(e))
5253
})
5354

55+
const modulePreloadTags = getModulePreload(parsedHtml)
56+
modulePreloadTags.forEach((tag) => {
57+
try {
58+
updateLinkTag(tag, assetPathMap)
59+
} catch (error) {
60+
assetsNotFound.push(error as Error)
61+
}
62+
})
63+
5464
const styleExternalTags = getStyleTags(parsedHtml)
5565
const styleInPageTags = getStyleInPageTags(parsedHtml)
5666
const styleTags = [...styleExternalTags, ...styleInPageTags]

src/commands/web/internal/updateLinkTag.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ export const updateLinkTag = (
1818

1919
const assetPath = assetPathMap.find(
2020
(entry) =>
21-
entry.source === linkSourcePath || `./${entry.source}` === linkSourcePath
21+
entry.source === linkSourcePath ||
22+
`./${entry.source}` === linkSourcePath ||
23+
`/${entry.source}` === linkSourcePath
2224
)
2325

2426
if (!assetPath?.target) {

src/commands/web/internal/updateScriptTag.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export const updateScriptTag = async (
5454
'src',
5555
assetPathMap.find(
5656
(entry) =>
57-
entry.source === scriptPath || `./${entry.source}` === scriptPath
57+
entry.source === scriptPath ||
58+
`./${entry.source}` === scriptPath ||
59+
`/${entry.source}` === scriptPath
5860
)!.target
5961
)
6062
}

src/commands/web/internal/updateStyleTag.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export const updateStyleTag = async (
5454
'href',
5555
assetPathMap.find(
5656
(entry) =>
57-
entry.source === scriptPath || `./${entry.source}` === scriptPath
57+
entry.source === scriptPath ||
58+
`./${entry.source}` === scriptPath ||
59+
`/${entry.source}` === scriptPath
5860
)!.target
5961
)
6062
}

0 commit comments

Comments
 (0)