Skip to content

Commit

Permalink
fix: avoid module preload polyfill for zero js html (#4999)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Sep 24, 2021
1 parent 2d6f682 commit ac55755
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
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>
8 changes: 6 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

let canInlineEntry = false

// inject chunk asset links
Expand Down

0 comments on commit ac55755

Please sign in to comment.