From a793e7ad48c56a034fd547081ed32b47a810c23e Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 6 May 2023 23:00:50 +0100 Subject: [PATCH] chore: type-check `.mjs` files (#20711) --- package.json | 2 ++ packages/vite/src/runtime/client.manifest.mjs | 1 + packages/vite/src/runtime/vite-node-shared.mjs | 2 ++ packages/vite/src/runtime/vite-node.mjs | 12 ++++++++++++ pnpm-lock.yaml | 6 ++++++ scripts/crawl.mjs | 3 ++- 6 files changed, 25 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index c395a92adb..9fcc2c996a 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,9 @@ "@types/node": "^18.16.5", "@types/semver": "^7.3.13", "case-police": "^0.6.0", + "chalk": "^5.2.0", "changelogen": "^0.5.3", + "consola": "^3.1.0", "crawler": "^1.4.0", "devalue": "^4.3.0", "eslint": "^8.40.0", diff --git a/packages/vite/src/runtime/client.manifest.mjs b/packages/vite/src/runtime/client.manifest.mjs index a30f394960..4d5232d0ab 100644 --- a/packages/vite/src/runtime/client.manifest.mjs +++ b/packages/vite/src/runtime/client.manifest.mjs @@ -1,3 +1,4 @@ +// @ts-check import { viteNodeFetch } from './vite-node-shared.mjs' export default () => viteNodeFetch('/manifest') diff --git a/packages/vite/src/runtime/vite-node-shared.mjs b/packages/vite/src/runtime/vite-node-shared.mjs index 2220bcd2de..9a9026595f 100644 --- a/packages/vite/src/runtime/vite-node-shared.mjs +++ b/packages/vite/src/runtime/vite-node-shared.mjs @@ -1,3 +1,4 @@ +// @ts-check import { Agent as HTTPSAgent } from 'node:https' import { $fetch } from 'ofetch' @@ -5,6 +6,7 @@ export const viteNodeOptions = JSON.parse(process.env.NUXT_VITE_NODE_OPTIONS || export const viteNodeFetch = $fetch.create({ baseURL: viteNodeOptions.baseURL, + // @ts-expect-error https://github.com/node-fetch/node-fetch#custom-agent agent: viteNodeOptions.baseURL.startsWith('https://') ? new HTTPSAgent({ rejectUnauthorized: false }) : null diff --git a/packages/vite/src/runtime/vite-node.mjs b/packages/vite/src/runtime/vite-node.mjs index fd1369eab4..baf2162bb5 100644 --- a/packages/vite/src/runtime/vite-node.mjs +++ b/packages/vite/src/runtime/vite-node.mjs @@ -1,3 +1,5 @@ +// @ts-check + import { performance } from 'node:perf_hooks' import { createError } from 'h3' import { ViteNodeRunner } from 'vite-node/client' @@ -5,8 +7,10 @@ import { consola } from 'consola' import { viteNodeFetch, viteNodeOptions } from './vite-node-shared.mjs' const runner = createRunner() +/** @type {(ssrContext: import('#app').NuxtSSRContext) => Promise} */ let render +/** @param ssrContext {import('#app').NuxtSSRContext} */ export default async (ssrContext) => { // Workaround for stub mode // https://github.com/nuxt/framework/pull/3983 @@ -33,6 +37,7 @@ function createRunner () { return new ViteNodeRunner({ root: viteNodeOptions.root, // Equals to Nuxt `srcDir` base: viteNodeOptions.base, + // @ts-expect-error https://github.com/vitest-dev/vitest/pull/3312 resolveId (id, importer) { _importers.set(id, importer) }, async fetchModule (id) { const importer = _importers.get(id) @@ -67,11 +72,18 @@ function createRunner () { }) } +/** + * @param errorData {any} + * @param id {string} + * @param importer {string} + */ function formatViteError (errorData, id, importer) { const errorCode = errorData.name || errorData.reasonCode || errorData.code const frame = errorData.frame || errorData.source || errorData.pluginCode + /** @param locObj {{ file?: string, id?: string, url?: string }} */ const getLocId = (locObj = {}) => locObj.file || locObj.id || locObj.url || id || '' + /** @param locObj {{ line?: string, column?: string }} */ const getLocPos = (locObj = {}) => locObj.line ? `${locObj.line}:${locObj.column || 0}` : '' const locId = getLocId(errorData.loc) || getLocId(errorData.location) || getLocId(errorData.input) || getLocId(errorData) const locPos = getLocPos(errorData.loc) || getLocPos(errorData.location) || getLocPos(errorData.input) || getLocPos(errorData) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ad97763cfc..1d7f6cc28d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -40,9 +40,15 @@ importers: case-police: specifier: ^0.6.0 version: 0.6.0 + chalk: + specifier: ^5.2.0 + version: 5.2.0 changelogen: specifier: ^0.5.3 version: 0.5.3 + consola: + specifier: ^3.1.0 + version: 3.1.0 crawler: specifier: ^1.4.0 version: 1.4.0 diff --git a/scripts/crawl.mjs b/scripts/crawl.mjs index 14173a007f..dc626e6e55 100644 --- a/scripts/crawl.mjs +++ b/scripts/crawl.mjs @@ -1,3 +1,4 @@ +// @ts-check import Crawler from 'crawler' import { consola } from 'consola' import { parseURL, withoutTrailingSlash } from 'ufo' @@ -67,7 +68,7 @@ const crawler = new Crawler({ callback (error, res, done) { const { $ } = res const { uri } = res.options - const { statusCode } = res.request.response + const { statusCode } = res.response if (error || ![200, 301, 302].includes(statusCode) || !$) { // TODO: normalize relative links in module readmes - https://github.com/nuxt/nuxt.com/issues/1271