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: modulepreload polyfill only during build (fix #4786) #7816

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -32,6 +32,12 @@ if (isBuild) {
expect(htmlEntry.assets.length).toEqual(1)
})
} else {
test('No ReferenceError', async () => {
browserErrors.forEach((error) => {
expect(error.name).not.toBe('ReferenceError')
})
})
patak-dev marked this conversation as resolved.
Show resolved Hide resolved

describe('CSS HMR', () => {
test('preserve the base in CSS HMR', async () => {
await untilUpdated(() => getColor('body'), 'black') // sanity check
Expand Down
@@ -1,3 +1,5 @@
import 'vite/modulepreload-polyfill'

export const colorClass = 'text-black'

export function colorHeading() {
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/modulePreloadPolyfill.ts
Expand Up @@ -5,7 +5,8 @@ import { isModernFlag } from './importAnalysisBuild'
export const modulePreloadPolyfillId = 'vite/modulepreload-polyfill'

export function modulePreloadPolyfillPlugin(config: ResolvedConfig): Plugin {
const skip = config.build.ssr
// `isModernFlag` is only available during build since it is resolved by `vite:build-import-analysis`
const skip = config.command !== 'build' || config.build.ssr
let polyfillString: string | undefined

return {
Expand Down
8 changes: 8 additions & 0 deletions scripts/jestPerTestSetup.ts
Expand Up @@ -24,6 +24,7 @@ declare global {
const page: Page | undefined

const browserLogs: string[]
const browserErrors: Error[]
const serverLogs: string[]
const viteTestUrl: string | undefined
const watcher: RollupWatcher | undefined
Expand All @@ -34,6 +35,7 @@ declare const global: {
page?: Page

browserLogs: string[]
browserErrors: Error[]
serverLogs: string[]
viteTestUrl?: string
watcher?: RollupWatcher
Expand All @@ -56,13 +58,19 @@ const onConsole = (msg: ConsoleMessage) => {
logs.push(msg.text())
}

const errors: Error[] = (global.browserErrors = [])
const onPageError = (error: Error) => {
errors.push(error)
}

beforeAll(async () => {
const page = global.page
if (!page) {
return
}
try {
page.on('console', onConsole)
page.on('pageerror', onPageError)

const testPath = expect.getState().testPath
const testName = slash(testPath).match(/playground\/([\w-]+)\//)?.[1]
Expand Down