Skip to content

Commit

Permalink
test: add case
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Dec 29, 2022
1 parent 52bfae4 commit a8d1a21
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
18 changes: 9 additions & 9 deletions packages/vite/src/node/plugins/html.ts
Expand Up @@ -52,8 +52,7 @@ const moduleScriptRE =
/[ \t]*<script[^>]*type\s*=\s*(?:"module"|'module'|module)[^>]*>/i
const modulePreloadLinkRE =
/[ \t]*<link[^>]*rel\s*=\s*(?:"modulepreload"|'modulepreload'|modulepreload)[\s\S]*?\/>/i

const importMapDependenciesRE = new RegExp(
const importMapAppendRE = new RegExp(
[moduleScriptRE, modulePreloadLinkRE].map((r) => r.source).join('|'),
'i',
)
Expand Down Expand Up @@ -898,10 +897,10 @@ export function preImportMapHook(
const importMapIndex = html.match(importMapRE)?.index
if (importMapIndex === undefined) return

const importMapDepIndex = html.match(importMapDependenciesRE)?.index
if (importMapDepIndex === undefined) return
const importMapAppendIndex = html.match(importMapAppendRE)?.index
if (importMapAppendIndex === undefined) return

if (importMapDepIndex < importMapIndex) {
if (importMapAppendIndex < importMapIndex) {
const relativeHtml = normalizePath(
path.relative(config.root, ctx.filename),
)
Expand All @@ -921,7 +920,7 @@ export function preImportMapHook(
*/
export function postImportMapHook(): IndexHtmlTransformHook {
return (html) => {
if (!importMapDependenciesRE.test(html)) return
if (!importMapAppendRE.test(html)) return

let importMap: string | undefined
html = html.replace(importMapRE, (match) => {
Expand All @@ -930,9 +929,10 @@ export function postImportMapHook(): IndexHtmlTransformHook {
})

if (importMap) {
html = html.replace(importMapDependenciesRE, (match) => {
return `${importMap}\n${match}`
})
html = html.replace(
importMapAppendRE,
(match) => `${importMap}\n${match}`,
)
}

return html
Expand Down
19 changes: 13 additions & 6 deletions playground/html/__tests__/html.spec.ts
Expand Up @@ -251,12 +251,6 @@ describe.runIf(isServe)('invalid', () => {
})
})

test('importmap', () => {
expect(browserLogs).not.toContain(
'An import map is added after module script load was triggered.',
)
})

describe('Valid HTML', () => {
test('valid HTML is parsed', async () => {
await page.goto(viteTestUrl + '/valid.html')
Expand All @@ -267,3 +261,16 @@ describe('Valid HTML', () => {
expect(await getColor('#duplicated-attrs')).toBe('green')
})
})

describe('importmap', () => {
beforeAll(async () => {
await page.goto(viteTestUrl + '/importmapOrder.html')
})

// Should put this test at the end to get all browser logs above
test('importmap should be prepended', async () => {
expect(browserLogs).not.toContain(
'An import map is added after module script load was triggered.',
)
})
})
14 changes: 14 additions & 0 deletions playground/html/importmapOrder.html
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script type="importmap">
{
"imports": {
"some-pkg": "url-of-pkg"
}
}
</script>
<link rel="modulepreload" href="url-of-pkg" />
<script type="module" src="/main.js"></script>
</head>
</html>
5 changes: 4 additions & 1 deletion playground/html/vite.config.js
Expand Up @@ -28,6 +28,7 @@ module.exports = {
),
linkProps: resolve(__dirname, 'link-props/index.html'),
valid: resolve(__dirname, 'valid.html'),
importmapOrder: resolve(__dirname, 'importmapOrder.html'),
},
},
},
Expand Down Expand Up @@ -168,7 +169,9 @@ ${
},
{
name: 'head-prepend-importmap',
transformIndexHtml() {
transformIndexHtml(_, ctx) {
if (ctx.path.includes('importmapOrder')) return

return [
{
tag: 'script',
Expand Down

0 comments on commit a8d1a21

Please sign in to comment.