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: avoid module preload polyfill for zero js html #4999

Merged
merged 2 commits into from Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/playground/html/__tests__/html.spec.ts
Expand Up @@ -104,6 +104,7 @@ if (isBuild) {

test('script is async', async () => {
expect(await page.$('head script[type=module][async]')).toBeTruthy()
expect(await page.$('head script[type=module]:not([async])')).toBeNull()
})
})

Expand All @@ -115,6 +116,20 @@ if (isBuild) {

test('script is mixed', async () => {
expect(await page.$('head script[type=module][async]')).toBeNull()
expect(await page.$('head script[type=module]:not([async])')).toBeTruthy()
})
})

describe('zeroJS', () => {
// Ensure that the modulePreload polyfill is discarded in this case

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

test('zeroJS', async () => {
expect(await page.$('head script[type=module]')).toBeNull()
})
})

Expand Down
1 change: 1 addition & 0 deletions packages/playground/html/vite.config.js
Expand Up @@ -11,6 +11,7 @@ module.exports = {
nested: resolve(__dirname, 'nested/index.html'),
scriptAsync: resolve(__dirname, 'scriptAsync.html'),
scriptMixed: resolve(__dirname, 'scriptMixed.html'),
zeroJS: resolve(__dirname, 'zeroJS.html'),
inline1: resolve(__dirname, 'inline/shared-1.html'),
inline2: resolve(__dirname, 'inline/shared-2.html'),
inline3: resolve(__dirname, 'inline/unique.html')
Expand Down
9 changes: 9 additions & 0 deletions packages/playground/html/zeroJS.html
@@ -0,0 +1,9 @@
<link rel="stylesheet" href="/main.css" />

<h1>zeroJS.html</h1>

<style>
body {
background-color: linen;
}
</style>
9 changes: 7 additions & 2 deletions packages/vite/src/node/plugins/html.ts
Expand Up @@ -293,8 +293,11 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {

processedHtml.set(id, s.toString())

// inject module preload polyfill
if (config.build.polyfillModulePreload) {
// inject module preload polyfill only when configured and needed
if (
config.build.polyfillModulePreload &&
(someScriptsAreAsync || someScriptsAreDefer)
) {
js = `import "${modulePreloadPolyfillId}";\n${js}`
}

Expand Down Expand Up @@ -391,6 +394,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
chunk.isEntry &&
chunk.facadeModuleId === id
) as OutputChunk | undefined

patak-dev marked this conversation as resolved.
Show resolved Hide resolved
let canInlineEntry = false

// inject chunk asset links
Expand All @@ -405,6 +409,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
// when not inlined, inject <script> for entry and modulepreload its dependencies
// when inlined, discard entry chunk and inject <script> for everything in post-order
const imports = getImportedChunks(chunk)

const assetTags = canInlineEntry
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
? imports.map((chunk) => toScriptTag(chunk, isAsync))
: [toScriptTag(chunk, isAsync), ...imports.map(toPreloadTag)]
Expand Down