Skip to content

Commit

Permalink
chore: remove @ts-ignore and fix some issues (#20273)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Apr 14, 2023
1 parent b602b66 commit f366ab4
Show file tree
Hide file tree
Showing 46 changed files with 116 additions and 117 deletions.
7 changes: 7 additions & 0 deletions .eslintrc
Expand Up @@ -73,6 +73,13 @@
"disallowTypeAnnotations": false
}
],
"@typescript-eslint/ban-ts-comment": [
"error",
{
"ts-expect-error": "allow-with-description",
"ts-ignore": true
}
],
"@typescript-eslint/no-unused-vars": [
"error",
{
Expand Down
2 changes: 1 addition & 1 deletion examples/experimental/wasm/server/api/sum.ts
Expand Up @@ -2,7 +2,7 @@ import { defineLazyEventHandler } from 'h3'

export default defineLazyEventHandler(async () => {
const { exports: { sum } } = await loadWasmInstance(
// @ts-ignore
// @ts-expect-error TODO: https://github.com/nuxt/nuxt/issues/14131
() => import('~/server/wasm/sum.wasm')
)

Expand Down
6 changes: 6 additions & 0 deletions packages/kit/index.d.ts
@@ -0,0 +1,6 @@
declare global {
var __NUXT_PREPATHS__: string[] | string | undefined
var __NUXT_PATHS__: string[] | string | undefined
}

export {}
8 changes: 3 additions & 5 deletions packages/kit/src/internal/cjs.ts
Expand Up @@ -85,14 +85,12 @@ export function getRequireCacheItem (id: string) {
/** @deprecated Do not use CJS utils */
export function resolveModule (id: string, opts: ResolveModuleOptions = {}) {
return normalize(_require.resolve(id, {
paths: ([] as string[]).concat(
// @ts-ignore
paths: ([] as Array<string | undefined>).concat(
global.__NUXT_PREPATHS__,
opts.paths || [],
process.cwd(),
// @ts-ignore
global.__NUXT_PATHS__
).filter(Boolean)
).filter(Boolean) as string[]
}))
}

Expand Down Expand Up @@ -137,7 +135,7 @@ export function importModule (id: string, opts: RequireModuleOptions = {}) {
export function tryImportModule (id: string, opts: RequireModuleOptions = {}) {
try {
return importModule(id, opts).catch(() => undefined)
} catch { }
} catch {}
}

/** @deprecated Do not use CJS utils */
Expand Down
10 changes: 4 additions & 6 deletions packages/kit/src/module/define.ts
Expand Up @@ -103,10 +103,8 @@ export function defineNuxtModule<OptionsT extends ModuleOptions> (definition: Mo
const NUXT2_SHIMS_KEY = '__nuxt2_shims_key__'
function nuxt2Shims (nuxt: Nuxt) {
// Avoid duplicate install and only apply to Nuxt2
// @ts-ignore
if (!isNuxt2(nuxt) || nuxt[NUXT2_SHIMS_KEY]) { return }
// @ts-ignore
nuxt[NUXT2_SHIMS_KEY] = true
if (!isNuxt2(nuxt) || nuxt[NUXT2_SHIMS_KEY as keyof Nuxt]) { return }
nuxt[NUXT2_SHIMS_KEY as keyof Nuxt] = true

// Allow using nuxt.hooks
// @ts-expect-error Nuxt 2 extends hookable
Expand All @@ -120,14 +118,14 @@ function nuxt2Shims (nuxt: Nuxt) {

// Support virtual templates with getContents() by writing them to .nuxt directory
let virtualTemplates: ResolvedNuxtTemplate[]
// @ts-ignore Nuxt 2 hook
// @ts-expect-error Nuxt 2 hook
nuxt.hook('builder:prepared', (_builder, buildOptions) => {
virtualTemplates = buildOptions.templates.filter((t: any) => t.getContents)
for (const template of virtualTemplates) {
buildOptions.templates.splice(buildOptions.templates.indexOf(template), 1)
}
})
// @ts-ignore Nuxt 2 hook
// @ts-expect-error Nuxt 2 hook
nuxt.hook('build:templates', async (templates) => {
const context = {
nuxt,
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/pages.ts
Expand Up @@ -7,7 +7,7 @@ import { isNuxt2 } from './compatibility'
export function extendPages (cb: NuxtHooks['pages:extend']) {
const nuxt = useNuxt()
if (isNuxt2(nuxt)) {
// @ts-expect-error
// @ts-expect-error TODO: Nuxt 2 hook
nuxt.hook('build:extendRoutes', cb)
} else {
nuxt.hook('pages:extend', cb)
Expand Down
4 changes: 2 additions & 2 deletions packages/nuxi/src/cli-run.ts
@@ -1,5 +1,5 @@
// @ts-ignore
// @ts-expect-error internal property for tracking start time
process._startTime = Date.now()

// @ts-ignore
// @ts-expect-error `default` property is not declared
import('./cli').then(r => (r.default || r).main())
2 changes: 0 additions & 2 deletions packages/nuxi/src/cli.ts
Expand Up @@ -15,7 +15,6 @@ async function _main () {
'no-clear'
]
})
// @ts-ignore
const command = args._.shift() || 'usage'

showBanner(command === 'dev' && args.clear !== false && !args.help)
Expand All @@ -30,7 +29,6 @@ async function _main () {
// Check Node.js version in background
setTimeout(() => { checkEngines().catch(() => {}) }, 1000)

// @ts-ignore default.default is hotfix for #621
const cmd = await commands[command as Command]() as NuxtCommand
if (args.h || args.help) {
showHelp(cmd.meta)
Expand Down
4 changes: 2 additions & 2 deletions packages/nuxi/src/commands/info.ts
Expand Up @@ -7,6 +7,7 @@ import destr from 'destr'
import { splitByCase } from 'scule'
import clipboardy from 'clipboardy'
import type { NuxtModule } from '@nuxt/schema'
import type { packageManagerLocks } from '../utils/packageManagers'
import { getPackageManager, getPackageManagerVersion } from '../utils/packageManagers'
import { findup } from '../utils/fs'
import { defineNuxtCommand } from './index'
Expand Down Expand Up @@ -51,11 +52,10 @@ export default defineNuxtCommand({
? 'vite' /* nuxt-vite */
: 'webpack')

let packageManager = getPackageManager(rootDir)
let packageManager: keyof typeof packageManagerLocks | 'unknown' | null = getPackageManager(rootDir)
if (packageManager) {
packageManager += '@' + getPackageManagerVersion(packageManager)
} else {
// @ts-expect-error
packageManager = 'unknown'
}

Expand Down
4 changes: 1 addition & 3 deletions packages/nuxi/src/utils/cjs.ts
Expand Up @@ -4,11 +4,9 @@ import { dirname, normalize } from 'pathe'
export function getModulePaths (paths?: string | string[]): string[] {
return ([] as Array<string | undefined>)
.concat(
// @ts-expect-error global object
global.__NUXT_PREPATHS__,
paths,
process.cwd(),
// @ts-expect-error global object
global.__NUXT_PATHS__
)
.filter(Boolean) as string[]
Expand All @@ -30,7 +28,7 @@ export function tryRequireModule (id: string, paths?: string | string[]) {

export function getNearestPackage (id: string, paths?: string | string[]) {
while (dirname(id) !== id) {
try { return requireModule(id + '/package.json', paths) } catch { }
try { return requireModule(id + '/package.json', paths) } catch {}
id = dirname(id)
}
return null
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxi/src/utils/prepare.ts
Expand Up @@ -104,7 +104,7 @@ export const writeTypes = async (nuxt: Nuxt) => {

// This is needed for Nuxt 2 which clears the build directory again before building
// https://github.com/nuxt/nuxt/blob/2.x/packages/builder/src/builder.js#L144
// @ts-expect-error
// @ts-expect-error TODO: Nuxt 2 hook
nuxt.hook('builder:prepared', writeFile)

await writeFile()
Expand Down
4 changes: 3 additions & 1 deletion packages/nuxt/index.d.ts
@@ -1,5 +1,7 @@
declare global {
const __NUXT_VERSION__: string
var __NUXT_VERSION__: string
var __NUXT_PREPATHS__: string[] | string | undefined
var __NUXT_PATHS__: string[] | string | undefined
}

export {}
2 changes: 1 addition & 1 deletion packages/nuxt/src/app/components/island-renderer.ts
@@ -1,7 +1,7 @@
import type { defineAsyncComponent } from 'vue'
import { createVNode, defineComponent } from 'vue'

// @ts-ignore
// @ts-expect-error virtual file
import * as islandComponents from '#build/components.islands.mjs'
import { createError } from '#app/composables/error'

Expand Down
6 changes: 3 additions & 3 deletions packages/nuxt/src/app/components/layout.ts
Expand Up @@ -3,11 +3,11 @@ import { Transition, computed, defineComponent, h, inject, nextTick, onMounted,
import type { RouteLocationNormalizedLoaded } from 'vue-router'
import { _wrapIf } from './utils'
import { useRoute } from '#app/composables/router'
// @ts-ignore
// @ts-expect-error virtual file
import { useRoute as useVueRouterRoute } from '#build/pages'
// @ts-ignore
// @ts-expect-error virtual file
import layouts from '#build/layouts'
// @ts-ignore
// @ts-expect-error virtual file
import { appLayoutTransition as defaultLayoutTransition } from '#build/nuxt.config.mjs'

// TODO: revert back to defineAsyncComponent when https://github.com/vuejs/core/issues/6638 is resolved
Expand Down
4 changes: 2 additions & 2 deletions packages/nuxt/src/app/composables/asyncData.ts
Expand Up @@ -29,7 +29,7 @@ export type MultiWatchSources = (WatchSource<unknown> | object)[]
export interface AsyncDataOptions<
ResT,
DataT = ResT,
PickKeys extends KeysOf<DataT> =KeysOf<DataT>,
PickKeys extends KeysOf<DataT> = KeysOf<DataT>,
> {
server?: boolean
lazy?: boolean
Expand Down Expand Up @@ -270,7 +270,7 @@ export function useLazyAsyncData<
const autoKey = typeof args[args.length - 1] === 'string' ? args.pop() : undefined
if (typeof args[0] !== 'string') { args.unshift(autoKey) }
const [key, handler, options] = args as [string, (ctx?: NuxtApp) => Promise<ResT>, AsyncDataOptions<ResT, DataT, PickKeys>]
// @ts-ignore
// @ts-expect-error we pass an extra argument to prevent a key being injected
return useAsyncData(key, handler, { ...options, lazy: true }, null)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/app/composables/fetch.ts
Expand Up @@ -151,6 +151,6 @@ export function useLazyFetch<
...opts,
lazy: true
},
// @ts-ignore
// @ts-expect-error we pass an extra argument with the resolved auto-key to prevent another from being injected
autoKey)
}
2 changes: 1 addition & 1 deletion packages/nuxt/src/app/config.ts
@@ -1,7 +1,7 @@
import { reactive } from 'vue'
import type { AppConfig } from 'nuxt/schema'
import { useNuxtApp } from './nuxt'
// @ts-ignore
// @ts-expect-error virtual file
import __appConfig from '#build/app.config.mjs'

type DeepPartial<T> = T extends Function ? T : T extends Record<string, any> ? { [P in keyof T]?: DeepPartial<T[P]> } : T
Expand Down
18 changes: 10 additions & 8 deletions packages/nuxt/src/app/entry.ts
@@ -1,23 +1,27 @@
// We set __webpack_public_path via this import with webpack builder
import { createApp, createSSRApp, nextTick } from 'vue'
import { $fetch } from 'ofetch'
// @ts-ignore
import type { $Fetch, NitroFetchRequest } from 'nitropack'

// This file must be imported first for webpack as we set __webpack_public_path__ there
// @ts-expect-error virtual file
import { baseURL } from '#build/paths.mjs'

import type { CreateOptions } from '#app'
import { applyPlugins, createNuxtApp, normalizePlugins } from '#app/nuxt'

import '#build/css'
// @ts-ignore
// @ts-expect-error virtual file
import _plugins from '#build/plugins'
// @ts-ignore
// @ts-expect-error virtual file
import RootComponent from '#build/root-component.mjs'
// @ts-ignore
// @ts-expect-error virtual file
import { appRootId } from '#build/nuxt.config.mjs'

if (!globalThis.$fetch) {
// @ts-ignore
globalThis.$fetch = $fetch.create({
baseURL: baseURL()
})
}) as $Fetch<unknown, NitroFetchRequest>
}

let entry: Function
Expand Down Expand Up @@ -45,9 +49,7 @@ if (process.server) {
if (process.client) {
// TODO: temporary webpack 5 HMR fix
// https://github.com/webpack-contrib/webpack-hot-middleware/issues/390
// @ts-ignore
if (process.dev && import.meta.webpackHot) {
// @ts-ignore
import.meta.webpackHot.accept()
}

Expand Down
1 change: 0 additions & 1 deletion packages/nuxt/src/app/nuxt.ts
Expand Up @@ -239,7 +239,6 @@ export function createNuxtApp (options: CreateOptions) {

// Inject $nuxt
defineGetter(nuxtApp.vueApp, '$nuxt', nuxtApp)
// @ts-expect-error
defineGetter(nuxtApp.vueApp.config.globalProperties, '$nuxt', nuxtApp)

if (process.server) {
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/app/plugins/router.ts
Expand Up @@ -7,7 +7,7 @@ import { navigateTo } from '../composables/router'
import { useState } from '../composables/state'
import { useRequestEvent } from '../composables/ssr'

// @ts-ignore
// @ts-expect-error virtual file
import { globalMiddleware } from '#build/middleware'

interface Route {
Expand Down
3 changes: 3 additions & 0 deletions packages/nuxt/src/app/types/augments.d.ts
Expand Up @@ -22,6 +22,9 @@ declare module 'vue' {
interface App<HostElement> {
$nuxt: NuxtApp
}
interface ComponentCustomProperties {
$nuxt: NuxtApp
}
interface ComponentInternalInstance {
_nuxtOnBeforeMountCbs: Function[]
}
Expand Down
4 changes: 2 additions & 2 deletions packages/nuxt/src/core/runtime/nitro/error.ts
Expand Up @@ -55,9 +55,9 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
// Fallback to static rendered error page
if (!res) {
const { template } = process.dev
// @ts-ignore
// @ts-expect-error TODO: add legacy type support for subpath imports
? await import('@nuxt/ui-templates/templates/error-dev.mjs')
// @ts-ignore
// @ts-expect-error TODO: add legacy type support for subpath imports
: await import('@nuxt/ui-templates/templates/error-500.mjs')
if (process.dev) {
// TODO: Support `message` in template
Expand Down
1 change: 0 additions & 1 deletion packages/nuxt/src/core/runtime/nitro/paths.ts
@@ -1,5 +1,4 @@
import { joinURL } from 'ufo'
// @ts-ignore
import { useRuntimeConfig } from '#internal/nitro'

export function baseURL (): string {
Expand Down
16 changes: 8 additions & 8 deletions packages/nuxt/src/core/runtime/nitro/renderer.ts
Expand Up @@ -15,14 +15,14 @@ import { useNitroApp } from '#internal/nitro/app'

// eslint-disable-next-line import/no-restricted-paths
import type { NuxtApp, NuxtSSRContext } from '#app/nuxt'
// @ts-ignore
// @ts-expect-error virtual file
import { appRootId, appRootTag } from '#internal/nuxt.config.mjs'
// @ts-ignore
// @ts-expect-error virtual file
import { buildAssetsURL, publicAssetsURL } from '#paths'

// @ts-ignore
// @ts-expect-error private property consumed by vite-generated url helpers
globalThis.__buildAssetsURL = buildAssetsURL
// @ts-ignore
// @ts-expect-error private property consumed by vite-generated url helpers
globalThis.__publicAssetsURL = publicAssetsURL

export interface NuxtRenderHTMLContext {
Expand Down Expand Up @@ -61,18 +61,18 @@ export interface NuxtRenderResponse {

interface ClientManifest {}

// @ts-ignore
// @ts-expect-error file will be produced after app build
const getClientManifest: () => Promise<Manifest> = () => import('#build/dist/server/client.manifest.mjs')
.then(r => r.default || r)
.then(r => typeof r === 'function' ? r() : r) as Promise<ClientManifest>

// @ts-ignore
// @ts-expect-error virtual file
const getStaticRenderedHead = (): Promise<NuxtMeta> => import('#head-static').then(r => r.default || r)

// @ts-ignore
// @ts-expect-error file will be produced after app build
const getServerEntry = () => import('#build/dist/server/server.mjs').then(r => r.default || r)

// @ts-ignore
// @ts-expect-error file will be produced after app build
const getSSRStyles = lazyCachedFunction((): Promise<Record<string, () => Promise<string[]>>> => import('#build/dist/server/styles.mjs').then(r => r.default || r))

// -- SSR Renderer --
Expand Down
7 changes: 3 additions & 4 deletions packages/nuxt/src/core/schema.ts
Expand Up @@ -10,7 +10,7 @@ import {
resolveSchema as resolveUntypedSchema
} from 'untyped'
import type { Schema, SchemaDefinition } from 'untyped'
// @ts-ignore
// @ts-expect-error TODO: add upstream type
import untypedPlugin from 'untyped/babel-plugin'
import jiti from 'jiti'

Expand Down Expand Up @@ -76,7 +76,7 @@ export default defineNuxtModule({

async function resolveSchema () {
// Global import
// @ts-ignore
// @ts-expect-error adding to globalThis for 'auto-import' support within nuxt.config file
globalThis.defineNuxtSchema = (val: any) => val

// Load schema from layers
Expand Down Expand Up @@ -107,9 +107,8 @@ export default defineNuxtModule({
schemaDefs.map(schemaDef => resolveUntypedSchema(schemaDef))
)

// @ts-expect-error
// Merge after normalization
const schema = defu(...schemas)
const schema = defu(...schemas as [Schema, Schema])

// Allow hooking to extend resolved schema
await nuxt.hooks.callHook('schema:resolved', schema)
Expand Down

0 comments on commit f366ab4

Please sign in to comment.