Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 45 additions & 0 deletions packages/plugin-legacy/src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, expect, test } from 'vitest'
import { modulePreloadLinkRE } from '../index'

describe('modulePreloadLinkRE', () => {
const matches: Array<[string, string]> = [
['rel first', '<link rel="modulepreload" crossorigin href="/assets/x.js">'],
[
'rel after other attributes',
'<link href="/assets/x.js" rel="modulepreload">',
],
['rel only', '<link rel="modulepreload">'],
['self-closing', '<link rel="modulepreload"/>'],
['self-closing with space', '<link rel="modulepreload" />'],
['single quotes', "<link rel='modulepreload' href='/assets/x.js'>"],
[
'attributes across multiple lines',
'<link\n rel="modulepreload"\n href="/assets/x.js"\n>',
],
]

for (const [name, html] of matches) {
test(`matches: ${name}`, () => {
expect(html.replace(modulePreloadLinkRE, '')).toBe('')
})
}

const nonMatches: Array<[string, string]> = [
['tag name with suffix', '<linkfoo rel="modulepreload">'],
['custom element with hyphen', '<link-preview rel="modulepreload">'],
['bare link tag', '<link>'],
['stylesheet link', '<link rel="stylesheet" href="/assets/x.css">'],
['preload (not modulepreload)', '<link rel="preload" href="/assets/x.js">'],
[
'attribute name ending in rel',
'<link xrel="modulepreload" href="/assets/x.js">',
],
['mismatched quotes', `<link rel="modulepreload'>`],
]

for (const [name, html] of nonMatches) {
test(`does not match: ${name}`, () => {
expect(html.replace(modulePreloadLinkRE, '')).toBe(html)
})
}
})
6 changes: 5 additions & 1 deletion packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ const _require = createRequire(import.meta.url)

const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/
const prefixedHashInFileNameRE = /\W?\[hash(?::\d+)?\]/
export const modulePreloadLinkRE: RegExp =
/<link(?![\w-])[^>]*?\srel=(['"])modulepreload\1[^>]*>/g

// browsers supporting dynamic import + import.meta.resolve + async generator
const modernTargetsEsbuild = [
Expand Down Expand Up @@ -641,7 +643,9 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
}
}
if (!genModern) {
html = html.replace(/<script type="module".*?<\/script>/g, '')
html = html
.replace(/<script type="module".*?<\/script>/g, '')
.replace(modulePreloadLinkRE, '')
}

const tags: HtmlTagDescriptor[] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ test.runIf(isBuild)('includes a script tag for SystemJS', async () => {
.poll(() => page.getAttribute('#vite-legacy-entry', 'data-src'))
.toMatch(/.\/assets\/index-legacy-(.+)\.js/)
})

test.runIf(isBuild)(
'does not emit modulepreload links for legacy-only output',
async () => {
await page.goto(viteTestUrl + '/no-polyfills.html')
expect(await page.content()).not.toContain('modulepreload')
},
)
Loading