feat(nuxt): upgrade to nitropack 2.2 (#18889)

This commit is contained in:
pooya parsa 2023-02-09 07:02:07 +01:00 committed by GitHub
parent 2e6224602c
commit 9845467421
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 311 additions and 343 deletions

View File

@ -60,7 +60,7 @@
"execa": "^6.1.0", "execa": "^6.1.0",
"expect-type": "^0.15.0", "expect-type": "^0.15.0",
"globby": "^13.1.3", "globby": "^13.1.3",
"jiti": "^1.16.2", "jiti": "^1.17.0",
"markdownlint-cli": "^0.33.0", "markdownlint-cli": "^0.33.0",
"nuxi": "workspace:*", "nuxi": "workspace:*",
"nuxt": "workspace:*", "nuxt": "workspace:*",

View File

@ -20,7 +20,7 @@
"globby": "^13.1.3", "globby": "^13.1.3",
"hash-sum": "^2.0.0", "hash-sum": "^2.0.0",
"ignore": "^5.2.4", "ignore": "^5.2.4",
"jiti": "^1.16.2", "jiti": "^1.17.0",
"knitwork": "^1.0.0", "knitwork": "^1.0.0",
"lodash.template": "^4.5.0", "lodash.template": "^4.5.0",
"mlly": "^1.1.0", "mlly": "^1.1.0",

View File

@ -35,8 +35,8 @@
"execa": "^6.1.0", "execa": "^6.1.0",
"flat": "^5.0.2", "flat": "^5.0.2",
"giget": "^1.0.0", "giget": "^1.0.0",
"h3": "^1.2.1", "h3": "^1.4.0",
"jiti": "^1.16.2", "jiti": "^1.17.0",
"listhen": "^1.0.2", "listhen": "^1.0.2",
"mlly": "^1.1.0", "mlly": "^1.1.0",
"mri": "^1.2.0", "mri": "^1.2.0",

View File

@ -54,14 +54,14 @@
"estree-walker": "^3.0.3", "estree-walker": "^3.0.3",
"fs-extra": "^11.1.0", "fs-extra": "^11.1.0",
"globby": "^13.1.3", "globby": "^13.1.3",
"h3": "^1.2.1", "h3": "^1.4.0",
"hash-sum": "^2.0.0", "hash-sum": "^2.0.0",
"hookable": "^5.4.2", "hookable": "^5.4.2",
"jiti": "^1.16.2", "jiti": "^1.17.0",
"knitwork": "^1.0.0", "knitwork": "^1.0.0",
"magic-string": "^0.27.0", "magic-string": "^0.27.0",
"mlly": "^1.1.0", "mlly": "^1.1.0",
"nitropack": "^2.1.2", "nitropack": "^2.2.0",
"nuxi": "3.1.2", "nuxi": "3.1.2",
"ofetch": "^1.0.0", "ofetch": "^1.0.0",
"ohash": "^1.0.0", "ohash": "^1.0.0",
@ -71,7 +71,7 @@
"strip-literal": "^1.0.1", "strip-literal": "^1.0.1",
"ufo": "^1.0.1", "ufo": "^1.0.1",
"unctx": "^2.1.1", "unctx": "^2.1.1",
"unenv": "^1.1.0", "unenv": "^1.1.1",
"unhead": "^1.0.21", "unhead": "^1.0.21",
"unimport": "^2.2.4", "unimport": "^2.2.4",
"unplugin": "^1.0.1", "unplugin": "^1.0.1",

View File

@ -3,6 +3,7 @@ import type { TypedInternalResponse, NitroFetchOptions, NitroFetchRequest, Avail
import type { Ref } from 'vue' import type { Ref } from 'vue'
import { computed, unref, reactive } from 'vue' import { computed, unref, reactive } from 'vue'
import { hash } from 'ohash' import { hash } from 'ohash'
import { useRequestFetch } from './ssr'
import type { AsyncDataOptions, _Transform, KeyOfRes, AsyncData, PickFrom } from './asyncData' import type { AsyncDataOptions, _Transform, KeyOfRes, AsyncData, PickFrom } from './asyncData'
import { useAsyncData } from './asyncData' import { useAsyncData } from './asyncData'
@ -22,6 +23,7 @@ export interface UseFetchOptions<
M extends AvailableRouterMethod<R> = AvailableRouterMethod<R> M extends AvailableRouterMethod<R> = AvailableRouterMethod<R>
> extends AsyncDataOptions<DataT, Transform, PickKeys>, ComputedFetchOptions<R, M> { > extends AsyncDataOptions<DataT, Transform, PickKeys>, ComputedFetchOptions<R, M> {
key?: string key?: string
$fetch?: typeof globalThis.$fetch
} }
export function useFetch< export function useFetch<
@ -102,7 +104,15 @@ export function useFetch<
const asyncData = useAsyncData<_ResT, ErrorT, Transform, PickKeys>(key, () => { const asyncData = useAsyncData<_ResT, ErrorT, Transform, PickKeys>(key, () => {
controller?.abort?.() controller?.abort?.()
controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController
return $fetch(_request.value, { signal: controller.signal, ..._fetchOptions } as any) as Promise<_ResT>
const isLocalFetch = typeof _request.value === 'string' && _request.value.startsWith('/')
let _$fetch = opts.$fetch || globalThis.$fetch
// Use fetch with request context and headers for server direct API calls
if (process.server && !opts.$fetch && isLocalFetch) {
_$fetch = useRequestFetch()
}
return _$fetch(_request.value, { signal: controller.signal, ..._fetchOptions } as any) as Promise<_ResT>
}, _asyncDataOptions) }, _asyncDataOptions)
return asyncData return asyncData

View File

@ -9,7 +9,7 @@ export { useFetch, useLazyFetch } from './fetch'
export type { FetchResult, UseFetchOptions } from './fetch' export type { FetchResult, UseFetchOptions } from './fetch'
export { useCookie } from './cookie' export { useCookie } from './cookie'
export type { CookieOptions, CookieRef } from './cookie' export type { CookieOptions, CookieRef } from './cookie'
export { useRequestHeaders, useRequestEvent, setResponseStatus } from './ssr' export { useRequestHeaders, useRequestEvent, useRequestFetch, setResponseStatus } from './ssr'
export { onNuxtReady } from './ready' export { onNuxtReady } from './ready'
export { abortNavigation, addRouteMiddleware, defineNuxtRouteMiddleware, onBeforeRouteLeave, onBeforeRouteUpdate, setPageLayout, navigateTo, useRoute, useRouter } from './router' export { abortNavigation, addRouteMiddleware, defineNuxtRouteMiddleware, onBeforeRouteLeave, onBeforeRouteUpdate, setPageLayout, navigateTo, useRoute, useRouter } from './router'
export type { AddRouteMiddlewareOptions, RouteMiddleware } from './router' export type { AddRouteMiddlewareOptions, RouteMiddleware } from './router'

View File

@ -16,6 +16,14 @@ export function useRequestEvent (nuxtApp: NuxtApp = useNuxtApp()): H3Event {
return nuxtApp.ssrContext?.event as H3Event return nuxtApp.ssrContext?.event as H3Event
} }
export function useRequestFetch (): typeof global.$fetch {
if (process.client) {
return globalThis.$fetch
}
const event = useNuxtApp().ssrContext?.event as H3Event
return event?.$fetch as typeof globalThis.$fetch || globalThis.$fetch
}
export function setResponseStatus (code: number, message?: string) { export function setResponseStatus (code: number, message?: string) {
const event = process.server && useRequestEvent() const event = process.server && useRequestEvent()
if (event) { if (event) {

View File

@ -82,7 +82,13 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
generateTsConfig: false generateTsConfig: false
}, },
publicAssets: [ publicAssets: [
{ dir: resolve(nuxt.options.buildDir, 'dist/client') }, nuxt.options.dev
? { dir: resolve(nuxt.options.buildDir, 'dist/client') }
: {
dir: join(nuxt.options.buildDir, 'dist/client', nuxt.options.app.buildAssetsDir),
maxAge: 30 * 24 * 60 * 60,
baseURL: nuxt.options.app.buildAssetsDir
},
...nuxt.options._layers ...nuxt.options._layers
.map(layer => join(layer.config.srcDir, layer.config.dir?.public || 'public')) .map(layer => join(layer.config.srcDir, layer.config.dir?.public || 'public'))
.filter(dir => existsSync(dir)) .filter(dir => existsSync(dir))
@ -157,10 +163,18 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
// Add fallback server for `ssr: false` // Add fallback server for `ssr: false`
if (!nuxt.options.ssr) { if (!nuxt.options.ssr) {
nitroConfig.virtual!['#build/dist/server/server.mjs'] = 'export default () => {}' nitroConfig.virtual!['#build/dist/server/server.mjs'] = 'export default () => {}'
// In case a non-normalized absolute path is called for on Windows
if (process.platform === 'win32') {
nitroConfig.virtual!['#build/dist/server/server.mjs'.replace(/\//g, '\\')] = 'export default () => {}'
}
} }
if (!nuxt.options.experimental.inlineSSRStyles) { if (!nuxt.options.experimental.inlineSSRStyles) {
nitroConfig.virtual!['#build/dist/server/styles.mjs'] = 'export default {}' nitroConfig.virtual!['#build/dist/server/styles.mjs'] = 'export default {}'
// In case a non-normalized absolute path is called for on Windows
if (process.platform === 'win32') {
nitroConfig.virtual!['#build/dist/server/styles.mjs'.replace(/\//g, '\\')] = 'export default {}'
}
} }
// Register nuxt protection patterns // Register nuxt protection patterns

View File

@ -39,6 +39,7 @@ const appPreset = defineUnimportPreset({
'useCookie', 'useCookie',
'useRequestHeaders', 'useRequestHeaders',
'useRequestEvent', 'useRequestEvent',
'useRequestFetch',
'setResponseStatus', 'setResponseStatus',
'setPageLayout', 'setPageLayout',
'onNuxtReady', 'onNuxtReady',

View File

@ -18,7 +18,7 @@
"@types/semver": "^7.3.13", "@types/semver": "^7.3.13",
"@unhead/schema": "^1.0.21", "@unhead/schema": "^1.0.21",
"@vitejs/plugin-vue": "^4.0.0", "@vitejs/plugin-vue": "^4.0.0",
"nitropack": "^2.1.2", "nitropack": "^2.2.0",
"unbuild": "latest", "unbuild": "latest",
"vite": "~4.1.1" "vite": "~4.1.1"
}, },
@ -27,7 +27,7 @@
"create-require": "^1.1.1", "create-require": "^1.1.1",
"defu": "^6.1.2", "defu": "^6.1.2",
"hookable": "^5.4.2", "hookable": "^5.4.2",
"jiti": "^1.16.2", "jiti": "^1.17.0",
"pathe": "^1.1.0", "pathe": "^1.1.0",
"pkg-types": "^1.0.1", "pkg-types": "^1.0.1",
"postcss-import-resolver": "^2.0.0", "postcss-import-resolver": "^2.0.0",

View File

@ -19,7 +19,7 @@
"defu": "^6.1.2", "defu": "^6.1.2",
"execa": "^6.1.0", "execa": "^6.1.0",
"get-port-please": "^3.0.1", "get-port-please": "^3.0.1",
"jiti": "^1.16.2", "jiti": "^1.17.0",
"ofetch": "^1.0.0", "ofetch": "^1.0.0",
"pathe": "^1.1.0" "pathe": "^1.1.0"
}, },

View File

@ -33,7 +33,7 @@
"externality": "^1.0.0", "externality": "^1.0.0",
"fs-extra": "^11.1.0", "fs-extra": "^11.1.0",
"get-port-please": "^3.0.1", "get-port-please": "^3.0.1",
"h3": "^1.2.1", "h3": "^1.4.0",
"knitwork": "^1.0.0", "knitwork": "^1.0.0",
"magic-string": "^0.27.0", "magic-string": "^0.27.0",
"mlly": "^1.1.0", "mlly": "^1.1.0",

View File

@ -1 +1,3 @@
export default () => 'OK!' export default eventHandler((_event) => {
return 'Hello!'
})

File diff suppressed because it is too large Load Diff

View File

@ -70,6 +70,7 @@ describe.skipIf(isWindows)('minimal nuxt application', () => {
"estree-walker", "estree-walker",
"h3", "h3",
"hookable", "hookable",
"iron-webcrypto",
"node-fetch-native", "node-fetch-native",
"ofetch", "ofetch",
"ohash", "ohash",