Merge branch 'main' into feat/useResponseHeader

This commit is contained in:
Jelmer 2024-09-17 15:58:30 +02:00 committed by GitHub
commit e9f29ebb4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 95 additions and 13 deletions

View File

@ -29,7 +29,7 @@ jobs:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Lychee link checker - name: Lychee link checker
uses: lycheeverse/lychee-action@c38ba4f281730ee0d64e6963f49b708e01567b86 # for v1.8.0 uses: lycheeverse/lychee-action@64c64dfc7ad14257a2001ef393627d334a516a1f # for v1.8.0
with: with:
# arguments with file types to check # arguments with file types to check
args: >- args: >-

View File

@ -64,6 +64,10 @@ If you get an error message like `Nuxt instance is unavailable` then it probably
Watch a video from Alexander Lichter about handling async code in composables and fixing `Nuxt instance is unavailable` in your app. Watch a video from Alexander Lichter about handling async code in composables and fixing `Nuxt instance is unavailable` in your app.
:: ::
::tip
When using a composable that requires the Nuxt context inside a non-SFC component, you need to wrap your component with `defineNuxtComponent` instead of `defineComponent`
::
::read-more{to="/docs/guide/going-further/experimental-features#asynccontext" icon="i-ph-star-duotone"} ::read-more{to="/docs/guide/going-further/experimental-features#asynccontext" icon="i-ph-star-duotone"}
Checkout the `asyncContext` experimental feature to use Nuxt composables in async functions. Checkout the `asyncContext` experimental feature to use Nuxt composables in async functions.
:: ::

View File

@ -34,7 +34,7 @@
"errx": "^0.1.0", "errx": "^0.1.0",
"globby": "^14.0.2", "globby": "^14.0.2",
"hash-sum": "^2.0.0", "hash-sum": "^2.0.0",
"ignore": "^5.3.2", "ignore": "^6.0.1",
"jiti": "^2.0.0-beta.3", "jiti": "^2.0.0-beta.3",
"klona": "^2.0.6", "klona": "^2.0.6",
"mlly": "^1.7.1", "mlly": "^1.7.1",

View File

@ -86,7 +86,7 @@
"globby": "^14.0.2", "globby": "^14.0.2",
"h3": "npm:h3-nightly@2.0.0-1718872656.6765a6e", "h3": "npm:h3-nightly@2.0.0-1718872656.6765a6e",
"hookable": "^5.5.3", "hookable": "^5.5.3",
"ignore": "^5.3.2", "ignore": "^6.0.1",
"impound": "^0.1.0", "impound": "^0.1.0",
"jiti": "^2.0.0-beta.3", "jiti": "^2.0.0-beta.3",
"klona": "^2.0.6", "klona": "^2.0.6",

View File

@ -545,6 +545,12 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
// nuxt dev // nuxt dev
if (nuxt.options.dev) { if (nuxt.options.dev) {
nuxt.hook('webpack:compile', ({ name, compiler }) => {
if (name === 'server') {
const memfs = compiler.outputFileSystem as typeof import('node:fs')
nitro.options.virtual['#build/dist/server/server.mjs'] = () => memfs.readFileSync(join(nuxt.options.buildDir, 'dist/server/server.mjs'), 'utf-8')
}
})
nuxt.hook('webpack:compiled', () => { nuxt.server.reload() }) nuxt.hook('webpack:compiled', () => { nuxt.server.reload() })
nuxt.hook('vite:compiled', () => { nuxt.server.reload() }) nuxt.hook('vite:compiled', () => { nuxt.server.reload() })

View File

@ -151,10 +151,9 @@ import type { Plugin } from '#app'
type Decorate<T extends Record<string, any>> = { [K in keyof T as K extends string ? \`$\${K}\` : never]: T[K] } type Decorate<T extends Record<string, any>> = { [K in keyof T as K extends string ? \`$\${K}\` : never]: T[K] }
type IsAny<T> = 0 extends 1 & T ? true : false type InjectionType<A extends Plugin> = A extends {default: Plugin<infer T>} ? Decorate<T> : unknown
type InjectionType<A extends Plugin> = IsAny<A> extends true ? unknown : A extends Plugin<infer T> ? Decorate<T> : unknown
type NuxtAppInjections = \n ${tsImports.map(p => `InjectionType<typeof ${genDynamicImport(p, { wrapper: false })}.default>`).join(' &\n ')} type NuxtAppInjections = \n ${tsImports.map(p => `InjectionType<typeof ${genDynamicImport(p, { wrapper: false })}>`).join(' &\n ')}
declare module '#app' { declare module '#app' {
interface NuxtApp extends NuxtAppInjections { } interface NuxtApp extends NuxtAppInjections { }

View File

@ -0,0 +1,67 @@
import { describe, expect, it } from 'vitest'
import { UnctxTransformPlugin } from '../src/core/plugins/unctx'
describe('unctx transform in nuxt', () => {
it('should transform nuxt plugins', async () => {
const code = `
export default defineNuxtPlugin({
async setup () {
await Promise.resolve()
}
})
`
expect(await transform(code)).toMatchInlineSnapshot(`
"/* _processed_nuxt_unctx_transform */
import { executeAsync as __executeAsync } from "unctx";
export default defineNuxtPlugin({
async setup () {let __temp, __restore;
;(([__temp,__restore]=__executeAsync(()=>Promise.resolve())),await __temp,__restore());
}
},1)"
`)
})
it('should transform vue components using defineNuxtComponent', async () => {
const code = `
definePageMeta({
async middleware() {
await Promise.resolve()
}
})
export default defineNuxtComponent({
async setup () {
await Promise.resolve()
}
})
`
expect(await transform(code, 'app.ts')).toMatchInlineSnapshot(`
"/* _processed_nuxt_unctx_transform */
import { executeAsync as __executeAsync } from "unctx";
definePageMeta({
async middleware() {let __temp, __restore;
;(([__temp,__restore]=__executeAsync(()=>Promise.resolve())),await __temp,__restore());
}
})
export default defineNuxtComponent({
async setup () {let __temp, __restore;
;(([__temp,__restore]=__executeAsync(()=>Promise.resolve())),await __temp,__restore());
}
})"
`)
})
})
function transform (code: string, id = 'app.vue') {
const transformerOptions = {
helperModule: 'unctx',
asyncFunctions: ['defineNuxtPlugin', 'defineNuxtRouteMiddleware'],
objectDefinitions: {
defineNuxtComponent: ['asyncData', 'setup'],
defineNuxtPlugin: ['setup'],
definePageMeta: ['middleware', 'validate'],
},
}
const plugin = UnctxTransformPlugin.raw({ sourcemap: false, transformerOptions }, {} as any) as any
return plugin.transformInclude(id) ? Promise.resolve(plugin.transform(code)).then((r: any) => r?.code.replace(/^ {6}/gm, '').trim()) : null
}

View File

@ -48,7 +48,7 @@
"c12": "2.0.0-beta.2", "c12": "2.0.0-beta.2",
"esbuild-loader": "4.2.2", "esbuild-loader": "4.2.2",
"h3": "npm:h3-nightly@2.0.0-1718872656.6765a6e", "h3": "npm:h3-nightly@2.0.0-1718872656.6765a6e",
"ignore": "5.3.2", "ignore": "6.0.1",
"nitro": "npm:nitro-nightly@3.0.0-beta-28665895.e727afda", "nitro": "npm:nitro-nightly@3.0.0-beta-28665895.e727afda",
"ofetch": "1.3.4", "ofetch": "1.3.4",
"unbuild": "3.0.0-rc.7", "unbuild": "3.0.0-rc.7",

View File

@ -210,8 +210,8 @@ importers:
specifier: ^2.0.0 specifier: ^2.0.0
version: 2.0.0 version: 2.0.0
ignore: ignore:
specifier: ^5.3.2 specifier: ^6.0.1
version: 5.3.2 version: 6.0.1
jiti: jiti:
specifier: 2.0.0-beta.3 specifier: 2.0.0-beta.3
version: 2.0.0-beta.3 version: 2.0.0-beta.3
@ -355,8 +355,8 @@ importers:
specifier: ^5.5.3 specifier: ^5.5.3
version: 5.5.3 version: 5.5.3
ignore: ignore:
specifier: ^5.3.2 specifier: ^6.0.1
version: 5.3.2 version: 6.0.1
impound: impound:
specifier: ^0.1.0 specifier: ^0.1.0
version: 0.1.0(rollup@4.21.3)(webpack-sources@3.2.3) version: 0.1.0(rollup@4.21.3)(webpack-sources@3.2.3)
@ -576,8 +576,8 @@ importers:
specifier: npm:h3-nightly@2.0.0-1718872656.6765a6e specifier: npm:h3-nightly@2.0.0-1718872656.6765a6e
version: h3-nightly@2.0.0-1718872656.6765a6e version: h3-nightly@2.0.0-1718872656.6765a6e
ignore: ignore:
specifier: 5.3.2 specifier: 6.0.1
version: 5.3.2 version: 6.0.1
nitro: nitro:
specifier: npm:nitro-nightly@3.0.0-beta-28665895.e727afda specifier: npm:nitro-nightly@3.0.0-beta-28665895.e727afda
version: nitro-nightly@3.0.0-beta-28665895.e727afda(typescript@5.6.2)(webpack-sources@3.2.3) version: nitro-nightly@3.0.0-beta-28665895.e727afda(typescript@5.6.2)(webpack-sources@3.2.3)
@ -4433,6 +4433,10 @@ packages:
resolution: {integrity: sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==} resolution: {integrity: sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==}
engines: {node: '>= 4'} engines: {node: '>= 4'}
ignore@6.0.1:
resolution: {integrity: sha512-9hCx6FGveEYzwsldacntlq0RdPsTjOAALVL4nqi1O8JU6OIzzchHELMNE9f+6ZMtuHG1vd+owvczaMhu6EM2Xw==}
engines: {node: '>= 4'}
image-meta@0.2.1: image-meta@0.2.1:
resolution: {integrity: sha512-K6acvFaelNxx8wc2VjbIzXKDVB0Khs0QT35U6NkGfTdCmjLNcO2945m7RFNR9/RPVFm48hq7QPzK8uGH18HCGw==} resolution: {integrity: sha512-K6acvFaelNxx8wc2VjbIzXKDVB0Khs0QT35U6NkGfTdCmjLNcO2945m7RFNR9/RPVFm48hq7QPzK8uGH18HCGw==}
@ -11345,6 +11349,8 @@ snapshots:
ignore@5.3.2: {} ignore@5.3.2: {}
ignore@6.0.1: {}
image-meta@0.2.1: {} image-meta@0.2.1: {}
immutable@4.3.7: {} immutable@4.3.7: {}