Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(html): tags prepend doctype regex #5315

Merged
merged 4 commits into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/playground/html/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,22 @@ if (isBuild) {
})
})
}


patak-dev marked this conversation as resolved.
Show resolved Hide resolved
describe('noHead', () => {
// If there isn't a <head>, scripts are injected after <!DOCTYPE html>

beforeAll(async () => {
// viteTestUrl is globally injected in scripts/jestPerTestSetup.ts
await page.goto(viteTestUrl + '/noHead.html')
})

test('noHead tags transform', async () => {
const el = await page.$('meta[name=description]')
expect(await el.getAttribute('content')).toBe('a vite app')

const kw = await page.$('meta[name=keywords]')
expect(await kw.getAttribute('content')).toBe('es modules')
})

patak-dev marked this conversation as resolved.
Show resolved Hide resolved
})
7 changes: 7 additions & 0 deletions packages/playground/html/noHead.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<link rel="stylesheet" href="/main.css" />

<!-- comment one -->
<h1>Hello</h1>
<!-- comment two -->

<script type="module" src="/main.js"></script>
16 changes: 9 additions & 7 deletions packages/playground/html/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
scriptAsync: resolve(__dirname, 'scriptAsync.html'),
scriptMixed: resolve(__dirname, 'scriptMixed.html'),
zeroJS: resolve(__dirname, 'zeroJS.html'),
noHead: resolve(__dirname, 'noHead.html'),
inline1: resolve(__dirname, 'inline/shared-1.html'),
inline2: resolve(__dirname, 'inline/shared-2.html'),
inline3: resolve(__dirname, 'inline/unique.html')
Expand All @@ -24,18 +25,19 @@ module.exports = {
name: 'pre-transform',
transformIndexHtml: {
enforce: 'pre',
transform(html) {
transform(html, { filename }) {
if (html.includes('/@vite/client')) {
throw new Error('pre transform applied at wrong time!')
}
const head = `
<head lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ title }}</title>
</head>`
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ title }}</title>
</head>
<html lang="en">${filename.includes('noHead') ? '' : head}
<body>
${html}
</body>
Expand Down
21 changes: 13 additions & 8 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,21 +622,26 @@ function toPublicPath(filename: string, config: ResolvedConfig) {
}

const headInjectRE = /([ \t]*)<\/head>/
const headPrependInjectRE = [/([ \t]*)<head[^>]*>/, /<!doctype html>/i]
const headPrependInjectRE = /([ \t]*)<head[^>]*>/
const doctypePrependInjectRE = /<!doctype html>/i
function injectToHead(
html: string,
tags: HtmlTagDescriptor[],
prepend = false
) {
if (prepend) {
// inject after head or doctype
for (const re of headPrependInjectRE) {
if (re.test(html)) {
return html.replace(
re,
(match, p1) => `${match}\n${serializeTags(tags, incrementIndent(p1))}`
)
}
if (headPrependInjectRE.test(html)) {
return html.replace(
headPrependInjectRE,
(match, p1) => `${match}\n${serializeTags(tags, incrementIndent(p1))}`
)
}
else if(doctypePrependInjectRE.test(html)) {
return html.replace(
doctypePrependInjectRE,
`$&\n${serializeTags(tags)}`
)
}
} else {
// inject before head close
Expand Down