mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 09:02:03 +00:00
feat(nuxt): support object-syntax plugins (#20003)
This commit is contained in:
parent
f951a15232
commit
4285092879
@ -40,6 +40,31 @@ export default defineNuxtPlugin(nuxtApp => {
|
|||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Object Syntax Plugins
|
||||||
|
|
||||||
|
It is also possible to define a plugin using an object syntax, for more advanced use cases. For example:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
export default defineNuxtPlugin({
|
||||||
|
name: 'my-plugin',
|
||||||
|
enforce: 'pre', // or 'post'
|
||||||
|
async setup (nuxtApp) {
|
||||||
|
// this is the equivalent of a normal functional plugin
|
||||||
|
},
|
||||||
|
hooks: {
|
||||||
|
// You can directly register Nuxt app hooks here
|
||||||
|
'app:created'() {
|
||||||
|
const nuxtApp = useNuxtApp()
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
::alert
|
||||||
|
If you are using an object-syntax plugin, the properties may be statically analyzed in future to produce a more optimized build. So you should not define them at runtime. For example, setting `enforce: process.server ? 'pre' : 'post'` would defeat any future optimization Nuxt is able to do for your plugins.
|
||||||
|
::
|
||||||
|
|
||||||
## Plugin Registration Order
|
## Plugin Registration Order
|
||||||
|
|
||||||
You can control the order in which plugins are registered by prefixing a number to the file names.
|
You can control the order in which plugins are registered by prefixing a number to the file names.
|
||||||
|
@ -116,19 +116,19 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||||||
::alert
|
::alert
|
||||||
Normally `payload` must contain only plain JavaScript objects. But by setting `experimental.renderJsonPayloads`, it is possible to use more advanced types, such as `ref`, `reactive`, `shallowRef`, `shallowReactive` and `NuxtError`.
|
Normally `payload` must contain only plain JavaScript objects. But by setting `experimental.renderJsonPayloads`, it is possible to use more advanced types, such as `ref`, `reactive`, `shallowRef`, `shallowReactive` and `NuxtError`.
|
||||||
|
|
||||||
You can also add your own types. In future you will be able to add your own types easily with [object-syntax plugins](https://github.com/nuxt/nuxt/issues/14628). For now, you must add your plugin which calls both `definePayloadReducer` and `definePayloadReviver` via a custom module:
|
You can also add your own types, with a special plugin helper:
|
||||||
|
|
||||||
```ts
|
```ts [plugins/custom-payload.ts]
|
||||||
export default defineNuxtConfig({
|
/**
|
||||||
modules: [
|
* This kind of plugin runs very early in the Nuxt lifecycle, before we revive the payload.
|
||||||
function (_options, nuxt) {
|
* You will not have access to the router or other Nuxt-injected properties.
|
||||||
// TODO: support directly via object syntax plugins: https://github.com/nuxt/nuxt/issues/14628
|
*/
|
||||||
nuxt.hook('modules:done', () => {
|
export default definePayloadPlugin((nuxtApp) => {
|
||||||
nuxt.options.plugins.unshift('~/plugins/custom-type-plugin')
|
definePayloadReducer('BlinkingText', data => data === '<blink>' && '_')
|
||||||
})
|
definePayloadReviver('BlinkingText', () => '<blink>')
|
||||||
},
|
|
||||||
]
|
|
||||||
})
|
})
|
||||||
|
```
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
### `isHydrating`
|
### `isHydrating`
|
||||||
|
@ -139,9 +139,31 @@ interface _NuxtApp {
|
|||||||
export interface NuxtApp extends _NuxtApp {}
|
export interface NuxtApp extends _NuxtApp {}
|
||||||
|
|
||||||
export const NuxtPluginIndicator = '__nuxt_plugin'
|
export const NuxtPluginIndicator = '__nuxt_plugin'
|
||||||
|
|
||||||
|
export interface PluginMeta {
|
||||||
|
name?: string
|
||||||
|
enforce?: 'pre' | 'default' | 'post'
|
||||||
|
/**
|
||||||
|
* This allows more granular control over plugin order and should only be used by advanced users.
|
||||||
|
* It overrides the value of `enforce` and is used to sort plugins.
|
||||||
|
*/
|
||||||
|
order?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ResolvedPluginMeta {
|
||||||
|
name?: string
|
||||||
|
order: number
|
||||||
|
}
|
||||||
|
|
||||||
export interface Plugin<Injections extends Record<string, unknown> = Record<string, unknown>> {
|
export interface Plugin<Injections extends Record<string, unknown> = Record<string, unknown>> {
|
||||||
(nuxt: _NuxtApp): Promise<void> | Promise<{ provide?: Injections }> | void | { provide?: Injections }
|
(nuxt: _NuxtApp): Promise<void> | Promise<{ provide?: Injections }> | void | { provide?: Injections }
|
||||||
[NuxtPluginIndicator]?: true
|
[NuxtPluginIndicator]?: true
|
||||||
|
meta?: ResolvedPluginMeta
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ObjectPluginInput<Injections extends Record<string, unknown> = Record<string, unknown>> extends PluginMeta {
|
||||||
|
hooks?: Partial<RuntimeNuxtHooks>
|
||||||
|
setup?: Plugin<Injections>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CreateOptions {
|
export interface CreateOptions {
|
||||||
@ -306,25 +328,30 @@ export function normalizePlugins (_plugins: Plugin[]) {
|
|||||||
const legacyInjectPlugins: Plugin[] = []
|
const legacyInjectPlugins: Plugin[] = []
|
||||||
const invalidPlugins: Plugin[] = []
|
const invalidPlugins: Plugin[] = []
|
||||||
|
|
||||||
const plugins = _plugins.map((plugin) => {
|
const plugins: Plugin[] = []
|
||||||
|
|
||||||
|
for (const plugin of _plugins) {
|
||||||
if (typeof plugin !== 'function') {
|
if (typeof plugin !== 'function') {
|
||||||
invalidPlugins.push(plugin)
|
if (process.dev) { invalidPlugins.push(plugin) }
|
||||||
return null
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Skip invalid plugins in next releases
|
||||||
|
let _plugin = plugin
|
||||||
if (plugin.length > 1) {
|
if (plugin.length > 1) {
|
||||||
legacyInjectPlugins.push(plugin)
|
|
||||||
// Allow usage without wrapper but warn
|
// Allow usage without wrapper but warn
|
||||||
// TODO: Skip invalid in next releases
|
if (process.dev) { legacyInjectPlugins.push(plugin) }
|
||||||
// @ts-ignore
|
// @ts-expect-error deliberate invalid second argument
|
||||||
return (nuxtApp: NuxtApp) => plugin(nuxtApp, nuxtApp.provide)
|
_plugin = (nuxtApp: NuxtApp) => plugin(nuxtApp, nuxtApp.provide)
|
||||||
// return null
|
|
||||||
}
|
}
|
||||||
if (!isNuxtPlugin(plugin)) {
|
|
||||||
unwrappedPlugins.push(plugin)
|
|
||||||
// Allow usage without wrapper but warn
|
// Allow usage without wrapper but warn
|
||||||
|
if (process.dev && !isNuxtPlugin(_plugin)) { unwrappedPlugins.push(_plugin) }
|
||||||
|
|
||||||
|
plugins.push(_plugin)
|
||||||
}
|
}
|
||||||
return plugin
|
|
||||||
}).filter(Boolean)
|
plugins.sort((a, b) => (a.meta?.order || orderMap.default) - (b.meta?.order || orderMap.default))
|
||||||
|
|
||||||
if (process.dev && legacyInjectPlugins.length) {
|
if (process.dev && legacyInjectPlugins.length) {
|
||||||
console.warn('[warn] [nuxt] You are using a plugin with legacy Nuxt 2 format (context, inject) which is likely to be broken. In the future they will be ignored:', legacyInjectPlugins.map(p => p.name || p).join(','))
|
console.warn('[warn] [nuxt] You are using a plugin with legacy Nuxt 2 format (context, inject) which is likely to be broken. In the future they will be ignored:', legacyInjectPlugins.map(p => p.name || p).join(','))
|
||||||
@ -336,12 +363,53 @@ export function normalizePlugins (_plugins: Plugin[]) {
|
|||||||
console.warn('[warn] [nuxt] You are using a plugin that has not been wrapped in `defineNuxtPlugin`. It is advised to wrap your plugins as in the future this may enable enhancements:', unwrappedPlugins.map(p => p.name || p).join(','))
|
console.warn('[warn] [nuxt] You are using a plugin that has not been wrapped in `defineNuxtPlugin`. It is advised to wrap your plugins as in the future this may enable enhancements:', unwrappedPlugins.map(p => p.name || p).join(','))
|
||||||
}
|
}
|
||||||
|
|
||||||
return plugins as Plugin[]
|
return plugins
|
||||||
}
|
}
|
||||||
|
|
||||||
export function defineNuxtPlugin<T extends Record<string, unknown>> (plugin: Plugin<T>) {
|
// -50: pre-all (nuxt)
|
||||||
plugin[NuxtPluginIndicator] = true
|
// -40: custom payload revivers (user)
|
||||||
return plugin
|
// -30: payload reviving (nuxt)
|
||||||
|
// -20: pre (user) <-- pre mapped to this
|
||||||
|
// -10: default (nuxt)
|
||||||
|
// 0: default (user) <-- default behavior
|
||||||
|
// +10: post (nuxt)
|
||||||
|
// +20: post (user) <-- post mapped to this
|
||||||
|
// +30: post-all (nuxt)
|
||||||
|
|
||||||
|
const orderMap: Record<NonNullable<ObjectPluginInput['enforce']>, number> = {
|
||||||
|
pre: -20,
|
||||||
|
default: 0,
|
||||||
|
post: 20
|
||||||
|
}
|
||||||
|
|
||||||
|
export function definePayloadPlugin<T extends Record<string, unknown>> (plugin: Plugin<T> | ObjectPluginInput<T>) {
|
||||||
|
return defineNuxtPlugin(plugin, { order: -40 })
|
||||||
|
}
|
||||||
|
|
||||||
|
export function defineNuxtPlugin<T extends Record<string, unknown>> (plugin: Plugin<T> | ObjectPluginInput<T>, meta?: PluginMeta): Plugin<T> {
|
||||||
|
if (typeof plugin === 'function') { return defineNuxtPlugin({ setup: plugin }, meta) }
|
||||||
|
|
||||||
|
const wrapper: Plugin<T> = (nuxtApp) => {
|
||||||
|
if (plugin.hooks) {
|
||||||
|
nuxtApp.hooks.addHooks(plugin.hooks)
|
||||||
|
}
|
||||||
|
if (plugin.setup) {
|
||||||
|
return plugin.setup(nuxtApp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wrapper.meta = {
|
||||||
|
name: meta?.name || plugin.name || plugin.setup?.name,
|
||||||
|
order:
|
||||||
|
meta?.order ||
|
||||||
|
plugin.order ||
|
||||||
|
orderMap[plugin.enforce || 'default'] ||
|
||||||
|
orderMap.default
|
||||||
|
}
|
||||||
|
|
||||||
|
wrapper[NuxtPluginIndicator] = true
|
||||||
|
|
||||||
|
return wrapper
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isNuxtPlugin (plugin: unknown) {
|
export function isNuxtPlugin (plugin: unknown) {
|
||||||
|
@ -3,7 +3,9 @@ import { defineNuxtPlugin, useRuntimeConfig } from '#app/nuxt'
|
|||||||
import { useRouter } from '#app/composables/router'
|
import { useRouter } from '#app/composables/router'
|
||||||
import { reloadNuxtApp } from '#app/composables/chunk'
|
import { reloadNuxtApp } from '#app/composables/chunk'
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxtApp) => {
|
export default defineNuxtPlugin({
|
||||||
|
name: 'nuxt:chunk-reload',
|
||||||
|
setup (nuxtApp) {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
|
|
||||||
@ -19,4 +21,5 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||||||
reloadNuxtApp({ path, persistState: true })
|
reloadNuxtApp({ path, persistState: true })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
@ -3,7 +3,9 @@ import { parseURL } from 'ufo'
|
|||||||
import { useHead } from '@unhead/vue'
|
import { useHead } from '@unhead/vue'
|
||||||
import { defineNuxtPlugin } from '#app/nuxt'
|
import { defineNuxtPlugin } from '#app/nuxt'
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxtApp) => {
|
export default defineNuxtPlugin({
|
||||||
|
name: 'nuxt:cross-origin-prefetch',
|
||||||
|
setup (nuxtApp) {
|
||||||
const externalURLs = ref(new Set<string>())
|
const externalURLs = ref(new Set<string>())
|
||||||
function generateRules () {
|
function generateRules () {
|
||||||
return {
|
return {
|
||||||
@ -32,4 +34,5 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
import { createDebugger } from 'hookable'
|
import { createDebugger } from 'hookable'
|
||||||
import { defineNuxtPlugin } from '#app/nuxt'
|
import { defineNuxtPlugin } from '#app/nuxt'
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxtApp) => {
|
export default defineNuxtPlugin({
|
||||||
|
name: 'nuxt:debug',
|
||||||
|
enforce: 'pre',
|
||||||
|
setup (nuxtApp) {
|
||||||
createDebugger(nuxtApp.hooks, { tag: 'nuxt-app' })
|
createDebugger(nuxtApp.hooks, { tag: 'nuxt-app' })
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
@ -3,7 +3,9 @@ import { defineNuxtPlugin } from '#app/nuxt'
|
|||||||
import { isPrerendered, loadPayload } from '#app/composables/payload'
|
import { isPrerendered, loadPayload } from '#app/composables/payload'
|
||||||
import { useRouter } from '#app/composables/router'
|
import { useRouter } from '#app/composables/router'
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxtApp) => {
|
export default defineNuxtPlugin({
|
||||||
|
name: 'nuxt:payload',
|
||||||
|
setup (nuxtApp) {
|
||||||
// Only enable behavior if initial page is prerendered
|
// Only enable behavior if initial page is prerendered
|
||||||
// TODO: Support hybrid and dev
|
// TODO: Support hybrid and dev
|
||||||
if (!isPrerendered()) {
|
if (!isPrerendered()) {
|
||||||
@ -24,4 +26,5 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||||||
if (!payload) { return }
|
if (!payload) { return }
|
||||||
Object.assign(nuxtApp.static.data, payload.data)
|
Object.assign(nuxtApp.static.data, payload.data)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import { defineNuxtPlugin } from '#app/nuxt'
|
import { defineNuxtPlugin } from '#app/nuxt'
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxtApp) => {
|
export default defineNuxtPlugin({
|
||||||
|
name: 'nuxt:webpack-preload',
|
||||||
|
setup (nuxtApp) {
|
||||||
nuxtApp.vueApp.mixin({
|
nuxtApp.vueApp.mixin({
|
||||||
beforeCreate () {
|
beforeCreate () {
|
||||||
const { _registeredComponents } = this.$nuxt.ssrContext
|
const { _registeredComponents } = this.$nuxt.ssrContext
|
||||||
@ -8,4 +10,5 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||||||
_registeredComponents.add(__moduleIdentifier)
|
_registeredComponents.add(__moduleIdentifier)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
import { defineNuxtPlugin } from '#app/nuxt'
|
import { defineNuxtPlugin, useNuxtApp } from '#app/nuxt'
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxtApp) => {
|
export default defineNuxtPlugin({
|
||||||
nuxtApp.hook('app:mounted', () => {
|
name: 'nuxt:restore-state',
|
||||||
|
hooks: {
|
||||||
|
'app:mounted' () {
|
||||||
|
const nuxtApp = useNuxtApp()
|
||||||
try {
|
try {
|
||||||
const state = sessionStorage.getItem('nuxt:reload:state')
|
const state = sessionStorage.getItem('nuxt:reload:state')
|
||||||
if (state) {
|
if (state) {
|
||||||
@ -9,5 +12,6 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||||||
Object.assign(nuxtApp.payload.state, JSON.parse(state)?.state)
|
Object.assign(nuxtApp.payload.state, JSON.parse(state)?.state)
|
||||||
}
|
}
|
||||||
} catch {}
|
} catch {}
|
||||||
})
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
@ -13,11 +13,15 @@ const revivers = {
|
|||||||
Reactive: (data: any) => reactive(data)
|
Reactive: (data: any) => reactive(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineNuxtPlugin(async (nuxtApp) => {
|
export default defineNuxtPlugin({
|
||||||
|
name: 'nuxt:revive-payload:client',
|
||||||
|
order: -30,
|
||||||
|
async setup (nuxtApp) {
|
||||||
for (const reviver in revivers) {
|
for (const reviver in revivers) {
|
||||||
definePayloadReviver(reviver, revivers[reviver as keyof typeof revivers])
|
definePayloadReviver(reviver, revivers[reviver as keyof typeof revivers])
|
||||||
}
|
}
|
||||||
Object.assign(nuxtApp.payload, await callWithNuxt(nuxtApp, getNuxtClientPayload, []))
|
Object.assign(nuxtApp.payload, await callWithNuxt(nuxtApp, getNuxtClientPayload, []))
|
||||||
// For backwards compatibility - TODO: remove later
|
// For backwards compatibility - TODO: remove later
|
||||||
window.__NUXT__ = nuxtApp.payload
|
window.__NUXT__ = nuxtApp.payload
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
@ -14,8 +14,11 @@ const reducers = {
|
|||||||
Reactive: (data: any) => isReactive(data) && toRaw(data)
|
Reactive: (data: any) => isReactive(data) && toRaw(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineNuxtPlugin(() => {
|
export default defineNuxtPlugin({
|
||||||
|
name: 'nuxt:revive-payload:server',
|
||||||
|
setup () {
|
||||||
for (const reducer in reducers) {
|
for (const reducer in reducers) {
|
||||||
definePayloadReducer(reducer, reducers[reducer as keyof typeof reducers])
|
definePayloadReducer(reducer, reducers[reducer as keyof typeof reducers])
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
@ -96,7 +96,10 @@ interface Router {
|
|||||||
removeRoute: (name: string) => void
|
removeRoute: (name: string) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
|
export default defineNuxtPlugin<{ route: Route, router: Router }>({
|
||||||
|
name: 'nuxt:router',
|
||||||
|
enforce: 'pre',
|
||||||
|
setup (nuxtApp) {
|
||||||
const initialURL = process.client
|
const initialURL = process.client
|
||||||
? withoutBase(window.location.pathname, useRuntimeConfig().app.baseURL) + window.location.search + window.location.hash
|
? withoutBase(window.location.pathname, useRuntimeConfig().app.baseURL) + window.location.search + window.location.hash
|
||||||
: nuxtApp.ssrContext!.url
|
: nuxtApp.ssrContext!.url
|
||||||
@ -110,7 +113,7 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
|
|||||||
error: []
|
error: []
|
||||||
}
|
}
|
||||||
|
|
||||||
const registerHook = <T extends keyof RouterHooks>(hook: T, guard: RouterHooks[T]) => {
|
const registerHook = <T extends keyof RouterHooks> (hook: T, guard: RouterHooks[T]) => {
|
||||||
hooks[hook].push(guard)
|
hooks[hook].push(guard)
|
||||||
return () => hooks[hook].splice(hooks[hook].indexOf(guard), 1)
|
return () => hooks[hook].splice(hooks[hook].indexOf(guard), 1)
|
||||||
}
|
}
|
||||||
@ -268,4 +271,5 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
|
|||||||
router
|
router
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
@ -40,11 +40,14 @@ const components = ${genObjectFromRawEntries(globalComponents.map((c) => {
|
|||||||
return [c.pascalName, `defineAsyncComponent(${genDynamicImport(c.filePath, { comment })}.then(c => ${exp}))`]
|
return [c.pascalName, `defineAsyncComponent(${genDynamicImport(c.filePath, { comment })}.then(c => ${exp}))`]
|
||||||
}))}
|
}))}
|
||||||
|
|
||||||
export default defineNuxtPlugin(nuxtApp => {
|
export default defineNuxtPlugin({
|
||||||
|
name: 'nuxt:global-components',
|
||||||
|
setup (nuxtApp) {
|
||||||
for (const name in components) {
|
for (const name in components) {
|
||||||
nuxtApp.vueApp.component(name, components[name])
|
nuxtApp.vueApp.component(name, components[name])
|
||||||
nuxtApp.vueApp.component('Lazy' + name, components[name])
|
nuxtApp.vueApp.component('Lazy' + name, components[name])
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
@ -292,9 +292,9 @@ async function initNuxt (nuxt: Nuxt) {
|
|||||||
|
|
||||||
// Add experimental support for custom types in JSON payload
|
// Add experimental support for custom types in JSON payload
|
||||||
if (nuxt.options.experimental.renderJsonPayloads) {
|
if (nuxt.options.experimental.renderJsonPayloads) {
|
||||||
nuxt.hook('modules:done', () => {
|
nuxt.hooks.hook('modules:done', () => {
|
||||||
nuxt.options.plugins.unshift(resolve(nuxt.options.appDir, 'plugins/revive-payload.client'))
|
addPlugin(resolve(nuxt.options.appDir, 'plugins/revive-payload.client'))
|
||||||
nuxt.options.plugins.unshift(resolve(nuxt.options.appDir, 'plugins/revive-payload.server'))
|
addPlugin(resolve(nuxt.options.appDir, 'plugins/revive-payload.server'))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,9 @@ import { defineNuxtPlugin } from '#app/nuxt'
|
|||||||
// @ts-expect-error untyped
|
// @ts-expect-error untyped
|
||||||
import { appHead } from '#build/nuxt.config.mjs'
|
import { appHead } from '#build/nuxt.config.mjs'
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxtApp) => {
|
export default defineNuxtPlugin({
|
||||||
|
name: 'nuxt:head',
|
||||||
|
setup (nuxtApp) {
|
||||||
const createHead = process.server ? createServerHead : createClientHead
|
const createHead = process.server ? createServerHead : createClientHead
|
||||||
const head = createHead()
|
const head = createHead()
|
||||||
head.push(appHead)
|
head.push(appHead)
|
||||||
@ -38,4 +40,5 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
@ -2,7 +2,10 @@
|
|||||||
import { polyfillAsVueUseHead } from '@unhead/vue/polyfill'
|
import { polyfillAsVueUseHead } from '@unhead/vue/polyfill'
|
||||||
import { defineNuxtPlugin } from '#app/nuxt'
|
import { defineNuxtPlugin } from '#app/nuxt'
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxtApp) => {
|
export default defineNuxtPlugin({
|
||||||
|
name: 'nuxt:vueuse-head-polyfill',
|
||||||
|
setup (nuxtApp) {
|
||||||
// avoid breaking ecosystem dependencies using low-level @vueuse/head APIs
|
// avoid breaking ecosystem dependencies using low-level @vueuse/head APIs
|
||||||
polyfillAsVueUseHead(nuxtApp.vueApp._context.provides.usehead)
|
polyfillAsVueUseHead(nuxtApp.vueApp._context.provides.usehead)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
@ -23,6 +23,7 @@ const appPreset = defineUnimportPreset({
|
|||||||
'defineNuxtComponent',
|
'defineNuxtComponent',
|
||||||
'useNuxtApp',
|
'useNuxtApp',
|
||||||
'defineNuxtPlugin',
|
'defineNuxtPlugin',
|
||||||
|
'definePayloadPlugin',
|
||||||
'reloadNuxtApp',
|
'reloadNuxtApp',
|
||||||
'useRuntimeConfig',
|
'useRuntimeConfig',
|
||||||
'useState',
|
'useState',
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
import { hasProtocol } from 'ufo'
|
import { hasProtocol } from 'ufo'
|
||||||
import { defineNuxtPlugin, useNuxtApp } from '#app/nuxt'
|
import { defineNuxtPlugin } from '#app/nuxt'
|
||||||
import { useRouter } from '#app/composables/router'
|
import { useRouter } from '#app/composables/router'
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import layouts from '#build/layouts'
|
import layouts from '#build/layouts'
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import { namedMiddleware } from '#build/middleware'
|
import { namedMiddleware } from '#build/middleware'
|
||||||
|
|
||||||
export default defineNuxtPlugin(() => {
|
export default defineNuxtPlugin({
|
||||||
const nuxtApp = useNuxtApp()
|
name: 'nuxt:prefetch',
|
||||||
|
setup (nuxtApp) {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
// Force layout prefetch on route changes
|
// Force layout prefetch on route changes
|
||||||
@ -38,4 +39,5 @@ export default defineNuxtPlugin(() => {
|
|||||||
layouts[layout]()
|
layouts[layout]()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
@ -45,7 +45,10 @@ function createCurrentLocation (
|
|||||||
return path + search + hash
|
return path + search + hash
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineNuxtPlugin(async (nuxtApp) => {
|
export default defineNuxtPlugin({
|
||||||
|
name: 'nuxt:router',
|
||||||
|
enforce: 'pre',
|
||||||
|
async setup (nuxtApp) {
|
||||||
let routerBase = useRuntimeConfig().app.baseURL
|
let routerBase = useRuntimeConfig().app.baseURL
|
||||||
if (routerOptions.hashMode && !routerBase.includes('#')) {
|
if (routerOptions.hashMode && !routerBase.includes('#')) {
|
||||||
// allow the user to provide a `#` in the middle: `/base/#/app`
|
// allow the user to provide a `#` in the middle: `/base/#/app`
|
||||||
@ -198,4 +201,5 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
return { provide: { router } }
|
return { provide: { router } }
|
||||||
|
}
|
||||||
}) as Plugin<{ router: Router }>
|
}) as Plugin<{ router: Router }>
|
||||||
|
@ -195,6 +195,7 @@ export default defineUntypedSchema({
|
|||||||
asyncFunctions: ['defineNuxtPlugin', 'defineNuxtRouteMiddleware'],
|
asyncFunctions: ['defineNuxtPlugin', 'defineNuxtRouteMiddleware'],
|
||||||
objectDefinitions: {
|
objectDefinitions: {
|
||||||
defineNuxtComponent: ['asyncData', 'setup'],
|
defineNuxtComponent: ['asyncData', 'setup'],
|
||||||
|
defineNuxtPlugin: ['setup'],
|
||||||
definePageMeta: ['middleware', 'validate']
|
definePageMeta: ['middleware', 'validate']
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ describe.skipIf(isWindows || process.env.ECOSYSTEM_CI)('minimal nuxt application
|
|||||||
|
|
||||||
it('default client bundle size', async () => {
|
it('default client bundle size', async () => {
|
||||||
stats.client = await analyzeSizes('**/*.js', publicDir)
|
stats.client = await analyzeSizes('**/*.js', publicDir)
|
||||||
expect(roundToKilobytes(stats.client.totalBytes)).toMatchInlineSnapshot('"104k"')
|
expect(roundToKilobytes(stats.client.totalBytes)).toMatchInlineSnapshot('"105k"')
|
||||||
expect(stats.client.files.map(f => f.replace(/\..*\.js/, '.js'))).toMatchInlineSnapshot(`
|
expect(stats.client.files.map(f => f.replace(/\..*\.js/, '.js'))).toMatchInlineSnapshot(`
|
||||||
[
|
[
|
||||||
"_nuxt/_plugin-vue_export-helper.js",
|
"_nuxt/_plugin-vue_export-helper.js",
|
||||||
@ -40,7 +40,7 @@ describe.skipIf(isWindows || process.env.ECOSYSTEM_CI)('minimal nuxt application
|
|||||||
|
|
||||||
it('default server bundle size', async () => {
|
it('default server bundle size', async () => {
|
||||||
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
|
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
|
||||||
expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"91k"')
|
expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"92k"')
|
||||||
|
|
||||||
const modules = await analyzeSizes('node_modules/**/*', serverDir)
|
const modules = await analyzeSizes('node_modules/**/*', serverDir)
|
||||||
expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2650k"')
|
expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2650k"')
|
||||||
|
6
test/fixtures/basic/nuxt.config.ts
vendored
6
test/fixtures/basic/nuxt.config.ts
vendored
@ -108,12 +108,6 @@ export default defineNuxtConfig({
|
|||||||
addVitePlugin(plugin.vite())
|
addVitePlugin(plugin.vite())
|
||||||
addWebpackPlugin(plugin.webpack())
|
addWebpackPlugin(plugin.webpack())
|
||||||
},
|
},
|
||||||
function (_options, nuxt) {
|
|
||||||
// TODO: support directly via object syntax plugins: https://github.com/nuxt/nuxt/issues/14628
|
|
||||||
nuxt.hook('modules:done', () => {
|
|
||||||
nuxt.options.plugins.unshift('~/plugins/custom-type-registration')
|
|
||||||
})
|
|
||||||
},
|
|
||||||
function (_options, nuxt) {
|
function (_options, nuxt) {
|
||||||
const routesToDuplicate = ['/async-parent', '/fixed-keyed-child-parent', '/keyed-child-parent', '/with-layout', '/with-layout2']
|
const routesToDuplicate = ['/async-parent', '/fixed-keyed-child-parent', '/keyed-child-parent', '/with-layout', '/with-layout2']
|
||||||
const stripLayout = (page: NuxtPage): NuxtPage => ({
|
const stripLayout = (page: NuxtPage): NuxtPage => ({
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
export default defineNuxtPlugin((nuxtApp) => {
|
export default definePayloadPlugin((nuxtApp) => {
|
||||||
definePayloadReducer('BlinkingText', data => data === '<original-blink>' && '_')
|
definePayloadReducer('BlinkingText', data => data === '<original-blink>' && '_')
|
||||||
definePayloadReviver('BlinkingText', () => '<revivified-blink>')
|
definePayloadReviver('BlinkingText', () => '<revivified-blink>')
|
||||||
if (process.server) {
|
if (process.server) {
|
||||||
|
Loading…
Reference in New Issue
Block a user