mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-31 07:40:33 +00:00
feat: add polyfills
option to auto-imports
This commit is contained in:
parent
f86d749077
commit
9fa3380af7
@ -9,17 +9,18 @@ import escapeRE from 'escape-string-regexp'
|
|||||||
import { lookupNodeModuleSubpath, parseNodeModulePath } from 'mlly'
|
import { lookupNodeModuleSubpath, parseNodeModulePath } from 'mlly'
|
||||||
import { isDirectory } from '../utils'
|
import { isDirectory } from '../utils'
|
||||||
import { TransformPlugin } from './transform'
|
import { TransformPlugin } from './transform'
|
||||||
import { defaultPresets } from './presets'
|
import { defaultPresets, appCompatPresets } from './presets'
|
||||||
|
|
||||||
export default defineNuxtModule<Partial<ImportsOptions>>({
|
export default defineNuxtModule<Partial<ImportsOptions>>({
|
||||||
meta: {
|
meta: {
|
||||||
name: 'nuxt:imports',
|
name: 'nuxt:imports',
|
||||||
configKey: 'imports',
|
configKey: 'imports',
|
||||||
},
|
},
|
||||||
defaults: nuxt => ({
|
defaults: nuxt => {
|
||||||
|
return {
|
||||||
autoImport: true,
|
autoImport: true,
|
||||||
scan: true,
|
scan: true,
|
||||||
presets: defaultPresets,
|
presets: nuxt.options.imports.polyfills ? [...defaultPresets, ...appCompatPresets] : defaultPresets,
|
||||||
global: false,
|
global: false,
|
||||||
imports: [],
|
imports: [],
|
||||||
dirs: [],
|
dirs: [],
|
||||||
@ -30,7 +31,9 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
|
|||||||
exclude: undefined,
|
exclude: undefined,
|
||||||
},
|
},
|
||||||
virtualImports: ['#imports'],
|
virtualImports: ['#imports'],
|
||||||
}),
|
polyfills: true,
|
||||||
|
}
|
||||||
|
},
|
||||||
async setup (options, nuxt) {
|
async setup (options, nuxt) {
|
||||||
// TODO: fix sharing of defaults between invocations of modules
|
// TODO: fix sharing of defaults between invocations of modules
|
||||||
const presets = JSON.parse(JSON.stringify(options.presets)) as ImportPresetWithDeprecation[]
|
const presets = JSON.parse(JSON.stringify(options.presets)) as ImportPresetWithDeprecation[]
|
||||||
|
@ -21,14 +21,6 @@ const granularAppPresets: InlinePreset[] = [
|
|||||||
imports: ['useNuxtApp', 'tryUseNuxtApp', 'defineNuxtPlugin', 'definePayloadPlugin', 'useRuntimeConfig', 'defineAppConfig'],
|
imports: ['useNuxtApp', 'tryUseNuxtApp', 'defineNuxtPlugin', 'definePayloadPlugin', 'useRuntimeConfig', 'defineAppConfig'],
|
||||||
from: '#app/nuxt',
|
from: '#app/nuxt',
|
||||||
},
|
},
|
||||||
{
|
|
||||||
imports: ['requestIdleCallback', 'cancelIdleCallback'],
|
|
||||||
from: '#app/compat/idle-callback',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
imports: ['setInterval'],
|
|
||||||
from: '#app/compat/interval',
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
imports: ['useAppConfig', 'updateAppConfig'],
|
imports: ['useAppConfig', 'updateAppConfig'],
|
||||||
from: '#app/config',
|
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[] = [
|
export const defaultPresets: InlinePreset[] = [
|
||||||
...commonPresets,
|
...commonPresets,
|
||||||
...granularAppPresets,
|
...granularAppPresets,
|
||||||
|
@ -32,4 +32,10 @@ export interface ImportsOptions extends UnimportOptions {
|
|||||||
exclude?: RegExp[]
|
exclude?: RegExp[]
|
||||||
include?: RegExp[]
|
include?: RegExp[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add polyfills for setInterval, requestIdleCallback, and others
|
||||||
|
* @default true
|
||||||
|
*/
|
||||||
|
polyfills?: boolean
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it, vi } from 'vitest'
|
||||||
import type { ComponentOptions } from 'vue'
|
import type { ComponentOptions } from 'vue'
|
||||||
import { Suspense, defineComponent, h, toDisplayString, useAttrs } from 'vue'
|
import { Suspense, defineComponent, h, toDisplayString, useAttrs } from 'vue'
|
||||||
import { mountSuspended } from '@nuxt/test-utils/runtime'
|
import { mountSuspended } from '@nuxt/test-utils/runtime'
|
||||||
@ -65,3 +65,29 @@ describe('client page', () => {
|
|||||||
expect(wrapper.find('#fallback').exists()).toBe(false)
|
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()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
@ -21,6 +21,9 @@ export default defineVitestConfig({
|
|||||||
experimental: {
|
experimental: {
|
||||||
appManifest: process.env.TEST_MANIFEST !== 'manifest-off',
|
appManifest: process.env.TEST_MANIFEST !== 'manifest-off',
|
||||||
},
|
},
|
||||||
|
imports: {
|
||||||
|
polyfills: false,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user