fix(nuxt): prompt to set compatibility date with latest nitro (#27893)

This commit is contained in:
Daniel Roe 2024-06-28 13:53:25 +02:00 committed by GitHub
parent ffb0f4e66c
commit b4a2b8f480
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 86 additions and 0 deletions

View File

@ -72,6 +72,8 @@
"acorn": "8.12.0", "acorn": "8.12.0",
"c12": "^1.11.1", "c12": "^1.11.1",
"chokidar": "^3.6.0", "chokidar": "^3.6.0",
"compatx": "^0.1.8",
"consola": "^3.2.3",
"cookie-es": "^1.1.0", "cookie-es": "^1.1.0",
"defu": "^6.1.4", "defu": "^6.1.4",
"destr": "^2.0.3", "destr": "^2.0.3",

View File

@ -10,12 +10,18 @@ import type { Nuxt, NuxtHooks, NuxtModule, NuxtOptions } from 'nuxt/schema'
import type { PackageJson } from 'pkg-types' import type { PackageJson } from 'pkg-types'
import { readPackageJSON, resolvePackageJSON } from 'pkg-types' import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
import { hash } from 'ohash' import { hash } from 'ohash'
import consola from 'consola'
import { colorize } from 'consola/utils'
import { updateConfig } from 'c12/update'
import { formatDate } from 'compatx'
import type { DateString } from 'compatx'
import escapeRE from 'escape-string-regexp' import escapeRE from 'escape-string-regexp'
import { withTrailingSlash, withoutLeadingSlash } from 'ufo' import { withTrailingSlash, withoutLeadingSlash } from 'ufo'
import defu from 'defu' import defu from 'defu'
import { gt, satisfies } from 'semver' import { gt, satisfies } from 'semver'
import { hasTTY, isCI } from 'std-env'
import pagesModule from '../pages/module' import pagesModule from '../pages/module'
import metaModule from '../head/module' import metaModule from '../head/module'
import componentsModule from '../components/module' import componentsModule from '../components/module'
@ -60,6 +66,9 @@ export function createNuxt (options: NuxtOptions): Nuxt {
return nuxt return nuxt
} }
// TODO: update to nitro import
const fallbackCompatibilityDate = '2024-04-03' as DateString
const nightlies = { const nightlies = {
'nitropack': 'nitropack-nightly', 'nitropack': 'nitropack-nightly',
'nitro': 'nitro-nightly', 'nitro': 'nitro-nightly',
@ -74,6 +83,8 @@ const keyDependencies = [
'@nuxt/schema', '@nuxt/schema',
] ]
let warnedAboutCompatDate = false
async function initNuxt (nuxt: Nuxt) { async function initNuxt (nuxt: Nuxt) {
// Register user hooks // Register user hooks
for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) { for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) {
@ -82,6 +93,59 @@ async function initNuxt (nuxt: Nuxt) {
} }
} }
// Prompt to set compatibility date
if (!nuxt.options.compatibilityDate) {
const todaysDate = formatDate(new Date())
if (!warnedAboutCompatDate) {
// Print warning
console.info(`Nuxt now supports pinning the behavior of provider and deployment presets with a compatibility date. We recommend you specify a \`compatibilityDate\` in your \`nuxt.config\` file.`)
}
// Prompt to update in dev mode
if (!warnedAboutCompatDate && nuxt.options.dev && hasTTY && !isCI) {
const result = await consola.prompt(`Do you want to update your ${colorize('cyan', 'nuxt.config')} to set ${colorize('cyan', `compatibilityDate: '${todaysDate}'`)}?`, {
type: 'confirm',
default: true,
})
if (result === true) {
const res = await updateConfig({
configFile: 'nuxt.config',
cwd: nuxt.options.rootDir,
async onCreate ({ configFile }) {
const shallCreate = await consola.prompt(`Do you want to create ${colorize('cyan', relative(nuxt.options.rootDir, configFile))}?`, {
type: 'confirm',
default: true,
})
if (shallCreate !== true) {
return false
}
return _getDefaultNuxtConfig()
},
onUpdate (config) {
config.compatibilityDate = todaysDate
},
}).catch((error) => {
consola.error(`Failed to update config: ${error.message}`)
return null
})
if (res?.configFile) {
nuxt.options.compatibilityDate = todaysDate
consola.success(`Compatibility date set to \`${todaysDate}\` in \`${relative(nuxt.options.rootDir, res.configFile)}\``)
}
}
}
if (!nuxt.options.compatibilityDate) {
nuxt.options.compatibilityDate = fallbackCompatibilityDate
if (!warnedAboutCompatDate) {
console.log(`Using \`${fallbackCompatibilityDate}\` as fallback compatibility date.`)
}
}
warnedAboutCompatDate = true
}
// Restart Nuxt when layer directories are added or removed // Restart Nuxt when layer directories are added or removed
const layersDir = withTrailingSlash(resolve(nuxt.options.rootDir, 'layers')) const layersDir = withTrailingSlash(resolve(nuxt.options.rootDir, 'layers'))
nuxt.hook('builder:watch', (event, relativePath) => { nuxt.hook('builder:watch', (event, relativePath) => {
@ -762,3 +826,10 @@ function createPortalProperties (sourceValue: any, options: NuxtOptions, paths:
}) })
} }
} }
const _getDefaultNuxtConfig = () => /* js */
`// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devtools: { enabled: true }
})
`

View File

@ -1,3 +1,4 @@
export default defineNuxtConfig({ export default defineNuxtConfig({
compatibilityDate: '2024-06-28',
devtools: { enabled: true }, devtools: { enabled: true },
}) })

View File

@ -287,6 +287,12 @@ importers:
chokidar: chokidar:
specifier: ^3.6.0 specifier: ^3.6.0
version: 3.6.0 version: 3.6.0
compatx:
specifier: ^0.1.8
version: 0.1.8
consola:
specifier: ^3.2.3
version: 3.2.3
cookie-es: cookie-es:
specifier: ^1.1.0 specifier: ^1.1.0
version: 1.1.0 version: 1.1.0

View File

@ -1,6 +1,7 @@
import { addTypeTemplate, installModule } from 'nuxt/kit' import { addTypeTemplate, installModule } from 'nuxt/kit'
export default defineNuxtConfig({ export default defineNuxtConfig({
compatibilityDate: '2024-06-28',
experimental: { experimental: {
typedPages: true, typedPages: true,
appManifest: true, appManifest: true,

View File

@ -12,6 +12,7 @@ declare module 'nitro/types' {
} }
export default defineNuxtConfig({ export default defineNuxtConfig({
compatibilityDate: '2024-06-28',
app: { app: {
pageTransition: true, pageTransition: true,
layoutTransition: true, layoutTransition: true,

View File

@ -1,3 +1,4 @@
export default defineNuxtConfig({ export default defineNuxtConfig({
compatibilityDate: '2024-06-28',
experimental: { appManifest: true }, experimental: { appManifest: true },
}) })

View File

@ -3,6 +3,7 @@ import { fileURLToPath } from 'node:url'
const testWithInlineVue = process.env.EXTERNAL_VUE === 'false' const testWithInlineVue = process.env.EXTERNAL_VUE === 'false'
export default defineNuxtConfig({ export default defineNuxtConfig({
compatibilityDate: '2024-06-28',
pages: false, pages: false,
experimental: { experimental: {
externalVue: !testWithInlineVue, externalVue: !testWithInlineVue,

View File

@ -1,5 +1,6 @@
// https://nuxt.com/docs/api/nuxt-config // https://nuxt.com/docs/api/nuxt-config
export default defineNuxtConfig({ export default defineNuxtConfig({
compatibilityDate: '2024-06-28',
experimental: { experimental: {
externalVue: false, externalVue: false,
}, },

View File

@ -3,6 +3,7 @@ import { fileURLToPath } from 'node:url'
const testWithInlineVue = process.env.EXTERNAL_VUE === 'false' const testWithInlineVue = process.env.EXTERNAL_VUE === 'false'
export default defineNuxtConfig({ export default defineNuxtConfig({
compatibilityDate: '2024-06-28',
experimental: { experimental: {
externalVue: !testWithInlineVue, externalVue: !testWithInlineVue,
}, },