2021-10-11 13:29:02 +00:00
|
|
|
import { relative, resolve, join } from 'pathe'
|
2020-11-04 12:34:18 +00:00
|
|
|
import consola from 'consola'
|
2020-11-20 00:16:31 +00:00
|
|
|
import { rollup, watch as rollupWatch } from 'rollup'
|
2021-10-02 16:01:17 +00:00
|
|
|
import fse from 'fs-extra'
|
2020-11-14 20:41:38 +00:00
|
|
|
import { printFSTree } from './utils/tree'
|
2020-11-04 12:34:18 +00:00
|
|
|
import { getRollupConfig } from './rollup/config'
|
2020-12-07 12:48:29 +00:00
|
|
|
import { hl, prettyPath, serializeTemplate, writeFile, isDirectory } from './utils'
|
2021-01-22 19:55:59 +00:00
|
|
|
import { NitroContext } from './context'
|
2021-02-18 16:06:58 +00:00
|
|
|
import { scanMiddleware } from './server/middleware'
|
2020-11-04 12:34:18 +00:00
|
|
|
|
2021-01-22 19:55:59 +00:00
|
|
|
export async function prepare (nitroContext: NitroContext) {
|
|
|
|
consola.info(`Nitro preset is ${hl(nitroContext.preset)}`)
|
2020-11-04 12:34:18 +00:00
|
|
|
|
2021-01-22 19:55:59 +00:00
|
|
|
await cleanupDir(nitroContext.output.dir)
|
2020-11-20 00:16:31 +00:00
|
|
|
|
2021-01-22 19:55:59 +00:00
|
|
|
if (!nitroContext.output.publicDir.startsWith(nitroContext.output.dir)) {
|
|
|
|
await cleanupDir(nitroContext.output.publicDir)
|
2020-11-20 11:55:55 +00:00
|
|
|
}
|
2020-11-14 20:41:38 +00:00
|
|
|
|
2021-01-22 19:55:59 +00:00
|
|
|
if (!nitroContext.output.serverDir.startsWith(nitroContext.output.dir)) {
|
|
|
|
await cleanupDir(nitroContext.output.serverDir)
|
2020-11-20 11:55:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-20 00:16:31 +00:00
|
|
|
|
2020-11-20 11:55:55 +00:00
|
|
|
async function cleanupDir (dir: string) {
|
|
|
|
consola.info('Cleaning up', prettyPath(dir))
|
2021-10-02 16:01:17 +00:00
|
|
|
await fse.emptyDir(dir)
|
2020-11-20 00:16:31 +00:00
|
|
|
}
|
2020-11-14 20:41:38 +00:00
|
|
|
|
2021-01-22 19:55:59 +00:00
|
|
|
export async function generate (nitroContext: NitroContext) {
|
2021-02-18 16:06:58 +00:00
|
|
|
consola.start('Generating public...')
|
2020-12-07 12:48:29 +00:00
|
|
|
|
2021-01-22 19:55:59 +00:00
|
|
|
const clientDist = resolve(nitroContext._nuxt.buildDir, 'dist/client')
|
2020-12-07 12:48:29 +00:00
|
|
|
if (await isDirectory(clientDist)) {
|
2021-10-02 16:01:17 +00:00
|
|
|
await fse.copy(clientDist, join(nitroContext.output.publicDir, nitroContext._nuxt.publicPath))
|
2020-12-07 12:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 10:29:48 +00:00
|
|
|
const publicDir = nitroContext._nuxt.publicDir
|
|
|
|
if (await isDirectory(publicDir)) {
|
2021-10-02 16:01:17 +00:00
|
|
|
await fse.copy(publicDir, nitroContext.output.publicDir)
|
2020-12-07 12:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 16:06:58 +00:00
|
|
|
consola.success('Generated public ' + prettyPath(nitroContext.output.publicDir))
|
2020-11-20 11:55:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 19:55:59 +00:00
|
|
|
export async function build (nitroContext: NitroContext) {
|
2020-11-20 11:55:55 +00:00
|
|
|
// Compile html template
|
2021-01-22 19:55:59 +00:00
|
|
|
const htmlSrc = resolve(nitroContext._nuxt.buildDir, `views/${{ 2: 'app', 3: 'document' }[2]}.template.html`)
|
2021-10-18 09:06:23 +00:00
|
|
|
const htmlTemplate = { src: htmlSrc, contents: '', dst: '' }
|
2021-07-15 09:38:06 +00:00
|
|
|
htmlTemplate.dst = htmlTemplate.src.replace(/.html$/, '.mjs').replace('app.', 'document.')
|
2021-10-02 16:01:17 +00:00
|
|
|
htmlTemplate.contents = nitroContext.vfs[htmlTemplate.src] || await fse.readFile(htmlTemplate.src, 'utf-8')
|
2021-06-16 11:48:16 +00:00
|
|
|
await nitroContext._internal.hooks.callHook('nitro:document', htmlTemplate)
|
2021-10-18 09:06:23 +00:00
|
|
|
const compiled = 'export default ' + serializeTemplate(htmlTemplate.contents)
|
|
|
|
await writeFile(htmlTemplate.dst, compiled)
|
2020-11-20 11:55:55 +00:00
|
|
|
|
2021-01-22 19:55:59 +00:00
|
|
|
nitroContext.rollupConfig = getRollupConfig(nitroContext)
|
|
|
|
await nitroContext._internal.hooks.callHook('nitro:rollup:before', nitroContext)
|
|
|
|
return nitroContext._nuxt.dev ? _watch(nitroContext) : _build(nitroContext)
|
2020-11-20 00:16:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 13:29:02 +00:00
|
|
|
async function writeTypes (nitroContext: NitroContext) {
|
|
|
|
const routeTypes: Record<string, string[]> = {}
|
|
|
|
|
|
|
|
const middleware = [
|
|
|
|
...nitroContext.scannedMiddleware,
|
|
|
|
...nitroContext.middleware
|
|
|
|
]
|
|
|
|
|
|
|
|
for (const mw of middleware) {
|
|
|
|
if (typeof mw.handle !== 'string') { continue }
|
|
|
|
const relativePath = relative(nitroContext._nuxt.buildDir, mw.handle).replace(/\.[a-z]+$/, '')
|
|
|
|
routeTypes[mw.route] = routeTypes[mw.route] || []
|
|
|
|
routeTypes[mw.route].push(`ReturnType<typeof import('${relativePath}').default>`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const lines = [
|
2021-10-22 22:33:22 +00:00
|
|
|
'// Generated by nitro',
|
2021-10-11 13:29:02 +00:00
|
|
|
'declare module \'@nuxt/nitro\' {',
|
|
|
|
' interface InternalApi {',
|
|
|
|
...Object.entries(routeTypes).map(([path, types]) => ` '${path}': ${types.join(' | ')}`),
|
|
|
|
' }',
|
|
|
|
'}',
|
|
|
|
// Makes this a module for augmentation purposes
|
|
|
|
'export {}'
|
|
|
|
]
|
|
|
|
|
|
|
|
await writeFile(join(nitroContext._nuxt.buildDir, 'nitro.d.ts'), lines.join('\n'))
|
|
|
|
}
|
|
|
|
|
2021-01-22 19:55:59 +00:00
|
|
|
async function _build (nitroContext: NitroContext) {
|
2021-02-18 16:06:58 +00:00
|
|
|
nitroContext.scannedMiddleware = await scanMiddleware(nitroContext._nuxt.serverDir)
|
2021-10-11 22:36:50 +00:00
|
|
|
await writeTypes(nitroContext)
|
2020-11-20 00:16:31 +00:00
|
|
|
|
2021-02-18 16:06:58 +00:00
|
|
|
consola.start('Building server...')
|
2021-01-22 19:55:59 +00:00
|
|
|
const build = await rollup(nitroContext.rollupConfig).catch((error) => {
|
2021-02-18 16:06:58 +00:00
|
|
|
consola.error('Rollup error: ' + error.message)
|
2020-11-13 16:13:18 +00:00
|
|
|
throw error
|
|
|
|
})
|
2020-11-04 12:34:18 +00:00
|
|
|
|
2021-02-18 16:06:58 +00:00
|
|
|
consola.start('Writing server bundle...')
|
2021-01-22 19:55:59 +00:00
|
|
|
await build.write(nitroContext.rollupConfig.output)
|
2020-11-04 12:34:18 +00:00
|
|
|
|
2021-02-18 16:06:58 +00:00
|
|
|
consola.success('Server built')
|
2021-01-22 19:55:59 +00:00
|
|
|
await printFSTree(nitroContext.output.serverDir)
|
|
|
|
await nitroContext._internal.hooks.callHook('nitro:compiled', nitroContext)
|
2020-11-14 00:03:26 +00:00
|
|
|
|
|
|
|
return {
|
2021-09-27 12:49:36 +00:00
|
|
|
entry: resolve(nitroContext.rollupConfig.output.dir, nitroContext.rollupConfig.output.entryFileNames as string)
|
2020-11-14 00:03:26 +00:00
|
|
|
}
|
2020-11-04 12:34:18 +00:00
|
|
|
}
|
2020-11-20 00:16:31 +00:00
|
|
|
|
2021-02-19 14:01:08 +00:00
|
|
|
function startRollupWatcher (nitroContext: NitroContext) {
|
2021-01-22 19:55:59 +00:00
|
|
|
const watcher = rollupWatch(nitroContext.rollupConfig)
|
2021-04-04 21:06:56 +00:00
|
|
|
let start: number
|
2020-11-20 00:16:31 +00:00
|
|
|
|
|
|
|
watcher.on('event', (event) => {
|
|
|
|
switch (event.code) {
|
|
|
|
// The watcher is (re)starting
|
|
|
|
case 'START':
|
|
|
|
return
|
|
|
|
|
|
|
|
// Building an individual bundle
|
|
|
|
case 'BUNDLE_START':
|
|
|
|
start = Date.now()
|
|
|
|
return
|
|
|
|
|
|
|
|
// Finished building all bundles
|
|
|
|
case 'END':
|
2021-01-22 19:55:59 +00:00
|
|
|
nitroContext._internal.hooks.callHook('nitro:compiled', nitroContext)
|
2021-02-18 16:06:58 +00:00
|
|
|
consola.success('Nitro built', start ? `in ${Date.now() - start} ms` : '')
|
|
|
|
return
|
2020-11-20 00:16:31 +00:00
|
|
|
|
|
|
|
// Encountered an error while bundling
|
|
|
|
case 'ERROR':
|
2021-02-18 16:06:58 +00:00
|
|
|
consola.error('Rollup error: ' + event.error)
|
2020-11-20 00:16:31 +00:00
|
|
|
// consola.error(event.error)
|
|
|
|
}
|
|
|
|
})
|
2021-02-19 14:01:08 +00:00
|
|
|
return watcher
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _watch (nitroContext: NitroContext) {
|
|
|
|
let watcher = startRollupWatcher(nitroContext)
|
|
|
|
|
|
|
|
nitroContext.scannedMiddleware = await scanMiddleware(nitroContext._nuxt.serverDir,
|
|
|
|
(middleware, event) => {
|
|
|
|
nitroContext.scannedMiddleware = middleware
|
|
|
|
if (['add', 'addDir'].includes(event)) {
|
|
|
|
watcher.close()
|
2021-10-11 13:29:02 +00:00
|
|
|
writeTypes(nitroContext).catch(console.error)
|
2021-02-19 14:01:08 +00:00
|
|
|
watcher = startRollupWatcher(nitroContext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2021-10-11 22:36:50 +00:00
|
|
|
await writeTypes(nitroContext)
|
2020-11-20 00:16:31 +00:00
|
|
|
}
|