chore(nitro): add some more types (#49)

Co-authored-by: Pooya Parsa <pyapar@gmail.com>
This commit is contained in:
Daniel Roe 2021-04-04 22:06:56 +01:00 committed by GitHub
parent 0ba484b99a
commit 06f1ba9853
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 127 additions and 28 deletions

View File

@ -83,5 +83,12 @@
"vue": "3.0.11", "vue": "3.0.11",
"vue-bundle-renderer": "^0.2.3", "vue-bundle-renderer": "^0.2.3",
"vue-server-renderer": "^2.6.12" "vue-server-renderer": "^2.6.12"
},
"devDependencies": {
"@types/debounce": "^1.2.0",
"@types/fs-extra": "^9.0.10",
"@types/http-proxy": "^1.17.5",
"@types/node-fetch": "^2.5.9",
"@types/serve-static": "^1.13.9"
} }
} }

View File

@ -81,7 +81,7 @@ async function _build (nitroContext: NitroContext) {
function startRollupWatcher (nitroContext: NitroContext) { function startRollupWatcher (nitroContext: NitroContext) {
const watcher = rollupWatch(nitroContext.rollupConfig) const watcher = rollupWatch(nitroContext.rollupConfig)
let start let start: number
watcher.on('event', (event) => { watcher.on('event', (event) => {
switch (event.code) { switch (event.code) {

View File

@ -1,6 +1,11 @@
declare module NodeJS { declare global {
interface Global { namespace NodeJS {
__timing__: any interface Global {
$config: any __timing__: any
$config: any
}
} }
} }
// type export required to turn this into a module for TS augmentation purposes
export type A = {}

View File

@ -1,21 +1,24 @@
import { Worker } from 'worker_threads' import { Worker } from 'worker_threads'
import { createApp } from 'h3'
import { resolve } from 'upath' import chokidar, { FSWatcher } from 'chokidar'
import type { Server } from 'connect'
import debounce from 'debounce' import debounce from 'debounce'
import chokidar from 'chokidar'
import { listen, Listener } from 'listhen'
import serveStatic from 'serve-static'
import servePlaceholder from 'serve-placeholder'
import { createProxy } from 'http-proxy'
import { stat } from 'fs-extra' import { stat } from 'fs-extra'
import { createApp, Middleware } from 'h3'
import { createProxy } from 'http-proxy'
import { listen, Listener, ListenOptions } from 'listhen'
import servePlaceholder from 'serve-placeholder'
import serveStatic from 'serve-static'
import { resolve } from 'upath'
import type { NitroContext } from '../context' import type { NitroContext } from '../context'
import type { ServerMiddleware } from './middleware'
export function createDevServer (nitroContext: NitroContext) { export function createDevServer (nitroContext: NitroContext) {
// Worker // Worker
const workerEntry = resolve(nitroContext.output.dir, nitroContext.output.serverDir, 'index.js') const workerEntry = resolve(nitroContext.output.dir, nitroContext.output.serverDir, 'index.js')
let pendingWorker: Worker let pendingWorker: Worker | null
let activeWorker: Worker let activeWorker: Worker
let workerAddress: string let workerAddress: string | null
async function reload () { async function reload () {
if (pendingWorker) { if (pendingWorker) {
await pendingWorker.terminate() await pendingWorker.terminate()
@ -68,7 +71,7 @@ export function createDevServer (nitroContext: NitroContext) {
const proxy = createProxy() const proxy = createProxy()
app.use((req, res) => { app.use((req, res) => {
if (workerAddress) { if (workerAddress) {
proxy.web(req, res, { target: workerAddress }, (_err) => { proxy.web(req, res, { target: workerAddress }, (_err: unknown) => {
// console.error('[proxy]', err) // console.error('[proxy]', err)
}) })
} else { } else {
@ -78,7 +81,7 @@ export function createDevServer (nitroContext: NitroContext) {
// Listen // Listen
let listeners: Listener[] = [] let listeners: Listener[] = []
const _listen = async (port, opts?) => { const _listen = async (port: ListenOptions['port'], opts?: Partial<ListenOptions>) => {
const listener = await listen(app, { port, ...opts }) const listener = await listen(app, { port, ...opts })
listeners.push(listener) listeners.push(listener)
return listener return listener
@ -87,7 +90,7 @@ export function createDevServer (nitroContext: NitroContext) {
// Watch for dist and reload worker // Watch for dist and reload worker
const pattern = '**/*.{js,json}' const pattern = '**/*.{js,json}'
const events = ['add', 'change'] const events = ['add', 'change']
let watcher let watcher: FSWatcher
function watch () { function watch () {
if (watcher) { return } if (watcher) { return }
const dReload = debounce(() => reload().catch(console.warn), 200, true) const dReload = debounce(() => reload().catch(console.warn), 200, true)
@ -123,17 +126,22 @@ export function createDevServer (nitroContext: NitroContext) {
} }
} }
function createDynamicMiddleware () { interface DynamicMiddleware {
let middleware set: (input: Middleware | ServerMiddleware[]) => void
middleware: Middleware
}
function createDynamicMiddleware (): DynamicMiddleware {
let middleware: Middleware
return { return {
set: (input) => { set: (input) => {
if (!Array.isArray(input)) { if (!Array.isArray(input)) {
middleware = input middleware = input
return return
} }
const app = require('connect')() const app: Server = require('connect')()
for (const m of input) { for (const m of input) {
app.use(m.path || m.route || '/', m.handler || m.handle) app.use(m.path || m.route || '/', m.handler || m.handle!)
} }
middleware = app middleware = app
}, },

View File

@ -2,11 +2,22 @@ import { resolve, join, extname } from 'upath'
import { joinURL } from 'ufo' import { joinURL } from 'ufo'
import globby from 'globby' import globby from 'globby'
import { watch } from 'chokidar' import { watch } from 'chokidar'
import type { Middleware } from 'h3'
import { tryResolvePath, Nuxt } from '@nuxt/kit' import { tryResolvePath, Nuxt } from '@nuxt/kit'
export interface ServerMiddleware { export interface ServerMiddleware {
route: string route: string
handle: string /**
* @deprecated use route
*/
path?: string
handle?: Middleware
/**
* @deprecated use handle
*/
handler?: Middleware
lazy?: boolean // Default is true lazy?: boolean // Default is true
promisify?: boolean // Default is true promisify?: boolean // Default is true
} }

View File

@ -44,7 +44,7 @@ export function tryImport (dir: string, path: string) {
} catch (_err) { } } catch (_err) { }
} }
export async function writeFile (file, contents, log = false) { export async function writeFile (file: string, contents: string, log = false) {
await fse.mkdirp(dirname(file)) await fse.mkdirp(dirname(file))
await fse.writeFile(file, contents, 'utf-8') await fse.writeFile(file, contents, 'utf-8')
if (log) { if (log) {
@ -52,7 +52,7 @@ export async function writeFile (file, contents, log = false) {
} }
} }
export function resolvePath (nitroContext: NitroInput, path: string | ((nitroContext) => string), resolveBase: string = ''): string { export function resolvePath (nitroContext: NitroInput, path: string | ((nitroContext: NitroInput) => string), resolveBase: string = ''): string {
if (typeof path === 'function') { if (typeof path === 'function') {
path = path(nitroContext) path = path(nitroContext)
} }

View File

@ -6,7 +6,7 @@ import { readFile } from 'fs-extra'
import chalk from 'chalk' import chalk from 'chalk'
import stdenv from 'std-env' import stdenv from 'std-env'
export async function printFSTree (dir) { export async function printFSTree (dir: string) {
if (stdenv.test) { if (stdenv.test) {
return return
} }

View File

@ -1772,7 +1772,7 @@ __metadata:
languageName: unknown languageName: unknown
linkType: soft linkType: soft
"@nuxt/nitro@^0.2.0, @nuxt/nitro@workspace:packages/nitro": "@nuxt/nitro@^0.2.1, @nuxt/nitro@workspace:packages/nitro":
version: 0.0.0-use.local version: 0.0.0-use.local
resolution: "@nuxt/nitro@workspace:packages/nitro" resolution: "@nuxt/nitro@workspace:packages/nitro"
dependencies: dependencies:
@ -1788,6 +1788,11 @@ __metadata:
"@rollup/plugin-replace": ^2.4.2 "@rollup/plugin-replace": ^2.4.2
"@rollup/plugin-virtual": ^2.0.3 "@rollup/plugin-virtual": ^2.0.3
"@rollup/pluginutils": ^4.1.0 "@rollup/pluginutils": ^4.1.0
"@types/debounce": ^1.2.0
"@types/fs-extra": ^9.0.10
"@types/http-proxy": ^1.17.5
"@types/node-fetch": ^2.5.9
"@types/serve-static": ^1.13.9
"@vercel/nft": ^0.9.6 "@vercel/nft": ^0.9.6
"@vue/server-renderer": ^3.0.11 "@vue/server-renderer": ^3.0.11
archiver: ^5.3.0 archiver: ^5.3.0
@ -2278,6 +2283,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@types/debounce@npm:^1.2.0":
version: 1.2.0
resolution: "@types/debounce@npm:1.2.0"
checksum: c45dbc5fa369b9d29616f994a83ca6833a82ebdabbb455a92fed3c292d0f15482e6ba76c8fed7ea3c947df398fd89754880e38d6a247323dac467241f7ae5be0
languageName: node
linkType: hard
"@types/eslint-scope@npm:^3.7.0": "@types/eslint-scope@npm:^3.7.0":
version: 3.7.0 version: 3.7.0
resolution: "@types/eslint-scope@npm:3.7.0" resolution: "@types/eslint-scope@npm:3.7.0"
@ -2312,6 +2324,15 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@types/fs-extra@npm:^9.0.10":
version: 9.0.10
resolution: "@types/fs-extra@npm:9.0.10"
dependencies:
"@types/node": "*"
checksum: 047a134510d86e1d5041a89d591a328392d62567b8d67d40f238fb3029dc08971f163798077ed0679870a6842381f05d50d14f75ae6b05b736cb18ece0eb7f4f
languageName: node
linkType: hard
"@types/graceful-fs@npm:^4.1.2": "@types/graceful-fs@npm:^4.1.2":
version: 4.1.5 version: 4.1.5
resolution: "@types/graceful-fs@npm:4.1.5" resolution: "@types/graceful-fs@npm:4.1.5"
@ -2321,6 +2342,15 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@types/http-proxy@npm:^1.17.5":
version: 1.17.5
resolution: "@types/http-proxy@npm:1.17.5"
dependencies:
"@types/node": "*"
checksum: 21aefcc88273cb99474a7682209c795ee0dad15cc277b027ea504e65faab5cbb799b067662f7ae7c0255651bf9433a41811930c76f7561757f02f2c8262ee592
languageName: node
linkType: hard
"@types/istanbul-lib-coverage@npm:*, @types/istanbul-lib-coverage@npm:^2.0.0, @types/istanbul-lib-coverage@npm:^2.0.1": "@types/istanbul-lib-coverage@npm:*, @types/istanbul-lib-coverage@npm:^2.0.0, @types/istanbul-lib-coverage@npm:^2.0.1":
version: 2.0.3 version: 2.0.3
resolution: "@types/istanbul-lib-coverage@npm:2.0.3" resolution: "@types/istanbul-lib-coverage@npm:2.0.3"
@ -2370,6 +2400,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@types/mime@npm:^1":
version: 1.3.2
resolution: "@types/mime@npm:1.3.2"
checksum: c354bc135628c2f4ab64801ca3867c3acbd4050611579c4c9f5bdfecfb70db71bb8540bf8611b4319f5ef44139c5f7c5af81254369add5ed59e7e02ce929b96f
languageName: node
linkType: hard
"@types/mime@npm:^2.0.2": "@types/mime@npm:^2.0.2":
version: 2.0.3 version: 2.0.3
resolution: "@types/mime@npm:2.0.3" resolution: "@types/mime@npm:2.0.3"
@ -2391,6 +2428,16 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@types/node-fetch@npm:^2.5.9":
version: 2.5.9
resolution: "@types/node-fetch@npm:2.5.9"
dependencies:
"@types/node": "*"
form-data: ^3.0.0
checksum: 0c25139d135382dfe6c63bcbfc059d29350a3d950a258d4611f13180e207926d68b07bd3d1937e6eb4a98213a6da1aa285cc94167b9cf67e41d4634d3d2f5b3b
languageName: node
linkType: hard
"@types/node@npm:*": "@types/node@npm:*":
version: 14.14.35 version: 14.14.35
resolution: "@types/node@npm:14.14.35" resolution: "@types/node@npm:14.14.35"
@ -2442,6 +2489,16 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@types/serve-static@npm:^1.13.9":
version: 1.13.9
resolution: "@types/serve-static@npm:1.13.9"
dependencies:
"@types/mime": ^1
"@types/node": "*"
checksum: f261127514057b5c038d76259128d7b765dd92bfeaf769d05b8ddf5f254d066ce6142a935e2fc707bb3009d6544a979591852f0d135d0ed4d0c56db08738df6b
languageName: node
linkType: hard
"@types/source-list-map@npm:*": "@types/source-list-map@npm:*":
version: 0.1.2 version: 0.1.2
resolution: "@types/source-list-map@npm:0.1.2" resolution: "@types/source-list-map@npm:0.1.2"
@ -4489,7 +4546,7 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"combined-stream@npm:^1.0.6, combined-stream@npm:~1.0.6": "combined-stream@npm:^1.0.6, combined-stream@npm:^1.0.8, combined-stream@npm:~1.0.6":
version: 1.0.8 version: 1.0.8
resolution: "combined-stream@npm:1.0.8" resolution: "combined-stream@npm:1.0.8"
dependencies: dependencies:
@ -6648,6 +6705,17 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"form-data@npm:^3.0.0":
version: 3.0.1
resolution: "form-data@npm:3.0.1"
dependencies:
asynckit: ^0.4.0
combined-stream: ^1.0.8
mime-types: ^2.1.12
checksum: 6574ff4d2d845c47ba288151a6e0573767eefdee9da358be199e0f0b66aa5c454e7d62ed5e0daef6f84167ba1e9596e064f36f206488e460ef449ef7b653cdfc
languageName: node
linkType: hard
"form-data@npm:~2.3.2": "form-data@npm:~2.3.2":
version: 2.3.3 version: 2.3.3
resolution: "form-data@npm:2.3.3" resolution: "form-data@npm:2.3.3"
@ -10368,7 +10436,7 @@ __metadata:
dependencies: dependencies:
"@nuxt/app": ^0.2.0 "@nuxt/app": ^0.2.0
"@nuxt/kit": ^0.2.0 "@nuxt/kit": ^0.2.0
"@nuxt/nitro": ^0.2.0 "@nuxt/nitro": ^0.2.1
"@nuxt/vite-builder": ^0.1.1 "@nuxt/vite-builder": ^0.1.1
"@nuxt/webpack-builder": ^0.1.1 "@nuxt/webpack-builder": ^0.1.1
chokidar: ^3.5.1 chokidar: ^3.5.1