diff --git a/packages/nuxt/src/imports/module.ts b/packages/nuxt/src/imports/module.ts index 2714470a43..1e6d9a3093 100644 --- a/packages/nuxt/src/imports/module.ts +++ b/packages/nuxt/src/imports/module.ts @@ -9,28 +9,31 @@ import escapeRE from 'escape-string-regexp' import { lookupNodeModuleSubpath, parseNodeModulePath } from 'mlly' import { isDirectory } from '../utils' import { TransformPlugin } from './transform' -import { defaultPresets } from './presets' +import { defaultPresets, appCompatPresets } from './presets' export default defineNuxtModule>({ meta: { name: 'nuxt:imports', configKey: 'imports', }, - defaults: nuxt => ({ - autoImport: true, - scan: true, - presets: defaultPresets, - global: false, - imports: [], - dirs: [], - transform: { - include: [ - new RegExp('^' + escapeRE(nuxt.options.buildDir)), - ], - exclude: undefined, - }, - virtualImports: ['#imports'], - }), + defaults: nuxt => { + return { + autoImport: true, + scan: true, + presets: nuxt.options.imports.polyfills ? [...defaultPresets, ...appCompatPresets] : defaultPresets, + global: false, + imports: [], + dirs: [], + transform: { + include: [ + new RegExp('^' + escapeRE(nuxt.options.buildDir)), + ], + 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[] diff --git a/packages/nuxt/src/imports/presets.ts b/packages/nuxt/src/imports/presets.ts index 40a6e557f1..de9096e9b9 100644 --- a/packages/nuxt/src/imports/presets.ts +++ b/packages/nuxt/src/imports/presets.ts @@ -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', @@ -256,6 +248,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, diff --git a/packages/schema/src/types/imports.ts b/packages/schema/src/types/imports.ts index 96171ecf87..5dc26e22c4 100644 --- a/packages/schema/src/types/imports.ts +++ b/packages/schema/src/types/imports.ts @@ -32,4 +32,10 @@ export interface ImportsOptions extends UnimportOptions { exclude?: RegExp[] include?: RegExp[] } + + /** + * Add polyfills for setInterval, requestIdleCallback, and others + * @default true + */ + polyfills?: boolean } diff --git a/test/nuxt/client.test.ts b/test/nuxt/client.test.ts index 87d34acc44..ceb0b3bbf6 100644 --- a/test/nuxt/client.test.ts +++ b/test/nuxt/client.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { describe, expect, it, vi } from 'vitest' import type { ComponentOptions } from 'vue' import { Suspense, defineComponent, h, toDisplayString, useAttrs } from 'vue' import { mountSuspended } from '@nuxt/test-utils/runtime' @@ -65,3 +65,29 @@ describe('client page', () => { expect(wrapper.find('#fallback').exists()).toBe(false) }) }) + +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() + }) +}) diff --git a/vitest.nuxt.config.ts b/vitest.nuxt.config.ts index 6c0ef9cabc..75e2446e89 100644 --- a/vitest.nuxt.config.ts +++ b/vitest.nuxt.config.ts @@ -21,6 +21,9 @@ export default defineVitestConfig({ experimental: { appManifest: process.env.TEST_MANIFEST !== 'manifest-off', }, + imports: { + polyfills: false, + }, }, }, },