feat(nuxt): allow disabling auto-imported polyfills (#30332)

This commit is contained in:
Ryota Watanabe 2025-02-09 15:48:37 +09:00 committed by Daniel Roe
parent 2122bebc67
commit 41d4973279
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B
5 changed files with 55 additions and 9 deletions

View File

@ -9,7 +9,7 @@ import escapeRE from 'escape-string-regexp'
import { lookupNodeModuleSubpath, parseNodeModulePath } from 'mlly'
import { isDirectory, logger } from '../utils'
import { TransformPlugin } from './transform'
import { defaultPresets } from './presets'
import { appCompatPresets, defaultPresets } from './presets'
export default defineNuxtModule<Partial<ImportsOptions>>({
meta: {
@ -30,11 +30,16 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
exclude: undefined,
},
virtualImports: ['#imports'],
polyfills: true,
}),
async setup (options, nuxt) {
// TODO: fix sharing of defaults between invocations of modules
const presets = JSON.parse(JSON.stringify(options.presets)) as ImportPresetWithDeprecation[]
if (nuxt.options.imports.polyfills) {
presets.push(...appCompatPresets)
}
// Allow modules extending sources
await nuxt.callHook('imports:sources', presets)

View File

@ -21,14 +21,6 @@ const granularAppPresets: InlinePreset[] = [
imports: ['useNuxtApp', 'tryUseNuxtApp', 'defineNuxtPlugin', 'definePayloadPlugin', 'useRuntimeConfig', 'defineAppConfig'],
from: '#app/nuxt',
},
{
imports: ['requestIdleCallback', 'cancelIdleCallback'],
from: '#app/compat/idle-callback',
},
{
imports: ['setInterval'],
from: '#app/compat/interval',
},
{
imports: ['useAppConfig', 'updateAppConfig'],
from: '#app/config',
@ -258,6 +250,17 @@ const vueTypesPreset = defineUnimportPreset({
],
})
export const appCompatPresets: InlinePreset[] = [
{
imports: ['requestIdleCallback', 'cancelIdleCallback'],
from: '#app/compat/idle-callback',
},
{
imports: ['setInterval'],
from: '#app/compat/interval',
},
]
export const defaultPresets: InlinePreset[] = [
...commonPresets,
...granularAppPresets,

View File

@ -32,4 +32,10 @@ export interface ImportsOptions extends UnimportOptions {
exclude?: RegExp[]
include?: RegExp[]
}
/**
* Add polyfills for setInterval, requestIdleCallback, and others
* @default true
*/
polyfills?: boolean
}

View File

@ -0,0 +1,29 @@
import { mount } from '@vue/test-utils'
import { describe, expect, it, vi } from 'vitest'
import { defineComponent, h } from 'vue'
describe('app/compat', () => {
const Component = defineComponent({
setup () {
const visible = ref(false)
setInterval(() => {
visible.value = true
}, 1000)
return () => h('div', {}, visible.value ? h('span', { id: 'child' }) : {})
},
})
it('setInterval is not auto-imported', async () => {
vi.useFakeTimers()
const wrapper = mount(Component)
vi.advanceTimersByTime(1000)
await wrapper.vm.$nextTick()
expect(wrapper.find('#child').exists()).toBe(true)
vi.useRealTimers()
})
})

View File

@ -22,6 +22,9 @@ export default defineVitestConfig({
experimental: {
appManifest: process.env.TEST_MANIFEST !== 'manifest-off',
},
imports: {
polyfills: false,
},
},
},
},