Skip to content

Commit 10fd012

Browse files
committed
refactor(kit,nuxt,ui-templates,vite): address deprecations + improve regexp perf (#33093)
1 parent 04dda84 commit 10fd012

File tree

41 files changed

+229
-90
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+229
-90
lines changed

eslint.config.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import noOnlyTests from 'eslint-plugin-no-only-tests'
55
import typegen from 'eslint-typegen'
66
import perfectionist from 'eslint-plugin-perfectionist'
77
import { importX } from 'eslint-plugin-import-x'
8+
import parser from '@typescript-eslint/parser'
89

910
import { runtimeDependencies } from './packages/nuxt/src/meta.mjs'
1011

@@ -14,6 +15,7 @@ export default createConfigForNuxt({
1415
commaDangle: 'always-multiline',
1516
},
1617
tooling: true,
18+
typescript: true,
1719
},
1820
})
1921
.prepend(
@@ -94,6 +96,24 @@ export default createConfigForNuxt({
9496
},
9597
})
9698

99+
.append({
100+
files: ['packages/**/*.{mjs,js,ts}', '**/*.{spec,test}.{mjs,js,ts}'],
101+
ignores: [
102+
'packages/nuxt/src/app/types/augments.ts',
103+
'test/fixtures/basic/plugins/this-should-not-load.spec.js',
104+
],
105+
languageOptions: {
106+
parser,
107+
parserOptions: {
108+
projectService: true,
109+
tsconfigRootDir: import.meta.dirname,
110+
},
111+
},
112+
rules: {
113+
'@typescript-eslint/no-deprecated': 'error',
114+
},
115+
})
116+
97117
.override('nuxt/tooling/unicorn', {
98118
rules: {
99119
'unicorn/no-new-array': 'off',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"@types/babel__helper-plugin-utils": "7.10.3",
8989
"@types/node": "22.18.0",
9090
"@types/semver": "7.7.0",
91+
"@typescript-eslint/parser": "8.41.0",
9192
"@unhead/vue": "2.0.14",
9293
"@vitest/coverage-v8": "3.2.4",
9394
"@vue/test-utils": "2.4.6",

packages/kit/src/compatibility.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export async function checkNuxtCompatibility (constraints: NuxtCompatibility, nu
3737
}
3838

3939
// Bridge compatibility check
40-
if (isNuxt2(nuxt)) {
40+
if (isNuxtMajorVersion(2, nuxt)) {
4141
const bridgeRequirement = constraints.bridge
4242
const hasBridge = !!(nuxt.options as any).bridge
4343
if (bridgeRequirement === true && !hasBridge) {

packages/kit/src/context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export const getNuxtCtx = () => asyncNuxtStorage.tryUse()
2929
* ```
3030
*/
3131
export function useNuxt (): Nuxt {
32+
// eslint-disable-next-line @typescript-eslint/no-deprecated
3233
const instance = asyncNuxtStorage.tryUse() || nuxtCtx.tryUse()
3334
if (!instance) {
3435
throw new Error('Nuxt instance is unavailable!')
@@ -49,6 +50,7 @@ export function useNuxt (): Nuxt {
4950
* ```
5051
*/
5152
export function tryUseNuxt (): Nuxt | null {
53+
// eslint-disable-next-line @typescript-eslint/no-deprecated
5254
return asyncNuxtStorage.tryUse() || nuxtCtx.tryUse()
5355
}
5456

packages/kit/src/internal/esm.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function tryResolveModule (id: string, url: string | string[] | URL | URL
3232

3333
export function resolveModule (id: string, options?: ResolveModuleOptions) {
3434
return resolveModulePath(id, {
35+
// eslint-disable-next-line @typescript-eslint/no-deprecated
3536
from: options?.url ?? options?.paths ?? [import.meta.url],
3637
extensions: ['.js', '.mjs', '.cjs', '.ts', '.mts', '.cts'],
3738
})
@@ -70,6 +71,7 @@ export function requireModule<T = unknown> (id: string, opts?: ImportModuleOptio
7071
const jiti = createJiti(import.meta.url, {
7172
interopDefault: opts?.interopDefault !== false,
7273
})
74+
// eslint-disable-next-line @typescript-eslint/no-deprecated
7375
return jiti(pathToFileURL(resolvedPath).href) as T
7476
}
7577

@@ -78,6 +80,7 @@ export function requireModule<T = unknown> (id: string, opts?: ImportModuleOptio
7880
*/
7981
export function tryRequireModule<T = unknown> (id: string, opts?: ImportModuleOptions) {
8082
try {
83+
// eslint-disable-next-line @typescript-eslint/no-deprecated
8184
return requireModule<T>(id, opts)
8285
} catch {
8386
// intentionally empty as this is a `try-` function

packages/kit/src/internal/template.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function compileTemplate<T> (template: NuxtTemplate<T>, ctx: any) {
2727
}
2828

2929
/** @deprecated */
30-
const serialize = (data: any) => JSON.stringify(data, null, 2).replace(/"\{(.+)\}"(?=,?$)/gm, r => JSON.parse(r).replace(/^\{(.*)\}$/, '$1'))
30+
const serialize = (data: any) => JSON.stringify(data, null, 2).replace(/"\{.+\}"(?=,?$)/gm, r => JSON.parse(r).replace(/^\{(.*)\}$/, '$1'))
3131

3232
/** @deprecated */
3333
const importSources = (sources: string | string[], { lazy = false } = {}) => {
@@ -44,4 +44,5 @@ const importSources = (sources: string | string[], { lazy = false } = {}) => {
4444
const importName = genSafeVariableName
4545

4646
/** @deprecated */
47+
// eslint-disable-next-line @typescript-eslint/no-deprecated
4748
export const templateUtils = { serialize, importName, importSources }

packages/kit/src/layout.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { NuxtTemplate } from '@nuxt/schema'
22
import { join, parse } from 'pathe'
33
import { kebabCase } from 'scule'
4-
import { isNuxt2 } from './compatibility'
4+
import { isNuxtMajorVersion } from './compatibility'
55
import { useNuxt } from './context'
66
import { logger } from './logger'
77
import { addTemplate } from './template'
@@ -13,7 +13,7 @@ export function addLayout (this: any, template: NuxtTemplate | string, name?: st
1313
const { filename, src } = addTemplate(template)
1414
const layoutName = kebabCase(name || parse(filename).name).replace(LAYOUT_RE, '')
1515

16-
if (isNuxt2(nuxt)) {
16+
if (isNuxtMajorVersion(2, nuxt)) {
1717
// Nuxt 2 adds layouts in options
1818
const layout = (nuxt.options as any).layouts[layoutName]
1919
if (layout) {

packages/kit/src/loader/nuxt.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export interface LoadNuxtOptions extends LoadNuxtConfigOptions {
2323

2424
export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
2525
// Backward compatibility
26+
// eslint-disable-next-line @typescript-eslint/no-deprecated
2627
opts.cwd ||= opts.rootDir
28+
// eslint-disable-next-line @typescript-eslint/no-deprecated
2729
opts.overrides ||= opts.config || {}
2830

2931
// Apply dev as config override

packages/kit/src/module/define.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { dirname } from 'pathe'
66
import type { ModuleDefinition, ModuleOptions, ModuleSetupInstallResult, ModuleSetupReturn, Nuxt, NuxtModule, NuxtOptions, ResolvedModuleOptions, ResolvedNuxtTemplate } from '@nuxt/schema'
77
import { logger } from '../logger'
88
import { nuxtCtx, tryUseNuxt, useNuxt } from '../context'
9-
import { checkNuxtCompatibility, isNuxt2 } from '../compatibility'
9+
import { checkNuxtCompatibility, isNuxtMajorVersion } from '../compatibility'
1010
import { compileTemplate, templateUtils } from '../internal/template'
1111

1212
/**
@@ -156,16 +156,19 @@ function _defineNuxtModule<
156156
const NUXT2_SHIMS_KEY = '__nuxt2_shims_key__'
157157
function nuxt2Shims (nuxt: Nuxt) {
158158
// Avoid duplicate install and only apply to Nuxt2
159-
if (!isNuxt2(nuxt) || nuxt[NUXT2_SHIMS_KEY as keyof Nuxt]) { return }
159+
if (!isNuxtMajorVersion(2, nuxt) || nuxt[NUXT2_SHIMS_KEY as keyof Nuxt]) { return }
160160
nuxt[NUXT2_SHIMS_KEY as keyof Nuxt] = true
161161

162162
// Allow using nuxt.hooks
163163
// @ts-expect-error Nuxt 2 extends hookable
164164
nuxt.hooks = nuxt
165165

166166
// Allow using useNuxt()
167+
// eslint-disable-next-line @typescript-eslint/no-deprecated
167168
if (!nuxtCtx.tryUse()) {
169+
// eslint-disable-next-line @typescript-eslint/no-deprecated
168170
nuxtCtx.set(nuxt)
171+
// eslint-disable-next-line @typescript-eslint/no-deprecated
169172
nuxt.hook('close', () => nuxtCtx.unset())
170173
}
171174

@@ -182,6 +185,7 @@ function nuxt2Shims (nuxt: Nuxt) {
182185
nuxt.hook('build:templates', async (templates) => {
183186
const context = {
184187
nuxt,
188+
// eslint-disable-next-line @typescript-eslint/no-deprecated
185189
utils: templateUtils,
186190
app: {
187191
dir: nuxt.options.srcDir,
@@ -195,6 +199,7 @@ function nuxt2Shims (nuxt: Nuxt) {
195199
},
196200
}
197201
for await (const template of virtualTemplates) {
202+
// eslint-disable-next-line @typescript-eslint/no-deprecated
198203
const contents = await compileTemplate({ ...template, src: '' }, context)
199204
await fsp.mkdir(dirname(template.dst), { recursive: true })
200205
await fsp.writeFile(template.dst, contents)

packages/kit/src/module/install.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { parseNodeModulePath } from 'mlly'
88
import { resolveModuleURL } from 'exsolve'
99
import { isRelative } from 'ufo'
1010
import { resolvePackageJSON } from 'pkg-types'
11-
import { isNuxt2 } from '../compatibility'
11+
import { isNuxtMajorVersion } from '../compatibility'
1212
import { read as readRc, update as updateRc } from 'rc9'
1313
import semver from 'semver'
1414
import { directoryToURL } from '../internal/esm'
@@ -63,7 +63,7 @@ export async function installModule<
6363

6464
// Call module
6565
const res = (
66-
isNuxt2()
66+
isNuxtMajorVersion(2, nuxt)
6767
// @ts-expect-error Nuxt 2 `moduleContainer` is not typed
6868
? await nuxtModule.call(nuxt.moduleContainer, inlineOptions, nuxt)
6969
: nuxt.options.experimental?.debugModuleMutation && nuxt._asyncLocalStorageModule

0 commit comments

Comments
 (0)