Skip to content

Commit

Permalink
fix: only run build-html plugin on bundler inputs (fix #4067) (#5342)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimfeld authored Jan 11, 2022
1 parent 5d7b4c3 commit 7541a8d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
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 @@ -197,6 +197,25 @@ describe('noBody', () => {
})
})

describe('importAsString', () => {
// The build-html plugin should not alter HTML that is not an input.
test('not transformed', async () => {
const messages = []
function addConsoleMessage(message) {
messages.push(message.args()[0])
}

page.on('console', addConsoleMessage)
await page.goto(viteTestUrl + '/index.html')
await page.waitForLoadState()
page.off('console', addConsoleMessage)

const result = messages.map((m) => m.toString()).join('\n')
expect(result).toMatch('Some imported HTML')
expect(result).not.toMatch('This is injected')
})
})

describe('unicode path', () => {
test('direct access', async () => {
await page.goto(
Expand Down
5 changes: 5 additions & 0 deletions packages/playground/html/importAsString.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
Some imported HTML
</body>
</html>
2 changes: 2 additions & 0 deletions packages/playground/html/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { msg } from './shared'
import asString from './importAsString.html'
import './common.css'

console.log(msg + ' from main')
console.log('loaded string ' + asString)
12 changes: 12 additions & 0 deletions packages/playground/html/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ ${
}
]
}
},
{
// Emulate rollup-plugin-string
name: 'import-as-string-module',
transform(code, id) {
if (id.endsWith('importAsString.html')) {
return {
code: `export default ${JSON.stringify(code)}`,
map: { mappings: '' }
}
}
}
}
]
}
23 changes: 22 additions & 1 deletion packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,32 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
// Same reason with `htmlInlineProxyPlugin`
isAsyncScriptMap.set(config, new Map())

const inputFiles = new Set<string>()

return {
name: 'vite:build-html',

buildStart({ input }) {
isAsyncScriptMap.set(config, new Map())

let allInputs: string[]
if (typeof input === 'string') {
allInputs = [input]
} else if (Array.isArray(input)) {
allInputs = input
} else {
allInputs = Object.values(input)
}

for (const filename of allInputs) {
if (filename.endsWith('.html')) {
inputFiles.add(normalizePath(filename))
}
}
},

async transform(html, id) {
if (id.endsWith('.html')) {
if (inputFiles.has(id)) {
const publicPath = `/${slash(path.relative(config.root, id))}`
// pre-transform
html = await applyHtmlTransforms(html, preHooks, {
Expand Down

0 comments on commit 7541a8d

Please sign in to comment.