Skip to content

Commit

Permalink
chore: type-check .mjs files (nuxt#20711)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored May 6, 2023
1 parent 7cb4c69 commit a793e7a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/runtime/client.manifest.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
import { viteNodeFetch } from './vite-node-shared.mjs'

export default () => viteNodeFetch('/manifest')
2 changes: 2 additions & 0 deletions packages/vite/src/runtime/vite-node-shared.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// @ts-check
import { Agent as HTTPSAgent } from 'node:https'
import { $fetch } from 'ofetch'

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
Expand Down
12 changes: 12 additions & 0 deletions packages/vite/src/runtime/vite-node.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// @ts-check

import { performance } from 'node:perf_hooks'
import { createError } from 'h3'
import { ViteNodeRunner } from 'vite-node/client'
import { consola } from 'consola'
import { viteNodeFetch, viteNodeOptions } from './vite-node-shared.mjs'

const runner = createRunner()
/** @type {(ssrContext: import('#app').NuxtSSRContext) => Promise<any>} */
let render

/** @param ssrContext {import('#app').NuxtSSRContext} */
export default async (ssrContext) => {
// Workaround for stub mode
// https://github.com/nuxt/framework/pull/3983
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion scripts/crawl.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
import Crawler from 'crawler'
import { consola } from 'consola'
import { parseURL, withoutTrailingSlash } from 'ufo'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a793e7a

Please sign in to comment.