feat: migrate to nitro

This commit is contained in:
Pooya Parsa 2021-01-22 21:57:09 +01:00
parent c89166f8f9
commit faabd1ab54
4 changed files with 61 additions and 12 deletions

View File

@ -13,8 +13,8 @@ async function _main () {
const nuxt = await loadNuxt({ for: isDev ? 'dev' : 'build', rootDir })
if (isDev) {
const [{ url }] = await nuxt.server.listen(3000)
console.log('Listening:', url)
// https://github.com/nuxt-contrib/listhen
await nuxt.server.listen(3000, { name: 'Nuxt' })
}
await build(nuxt)

View File

@ -198,7 +198,7 @@ function normalizeConfig (_options: CliConfiguration) {
// If app.html is defined, set the template path to the user template
if (options.documentPath === undefined) {
options.documentPath = path.resolve(options.buildDir, 'views/app.template.html') // SIGMA/Nuxt2 compat
options.documentPath = path.resolve(options.buildDir, 'views/app.template.html') // NITRO/Nuxt2 compat
const userDocumentPath = path.join(options.srcDir, 'document.html')
if (fs.existsSync(userDocumentPath)) {
options.documentPath = userDocumentPath
@ -440,10 +440,7 @@ function normalizeConfig (_options: CliConfiguration) {
options._modules.push('@nuxt/telemetry')
}
// Sigma
options.appTemplatePath = path.resolve(options.appDir, '_templates/views/document.template.html') // SIGMA TODO
options._majorVersion = 3
options._modules.push('@nuxt/sigma')
return options
}

View File

@ -0,0 +1,55 @@
import {
wpfs,
getNitroContext,
createDevServer,
resolveMiddleware,
build,
prepare,
generate
} from '@nuxt/nitro'
import type { Nuxt } from './index'
export function initNitro (nuxt: Nuxt) {
// Create contexts
const nitroContext = getNitroContext(nuxt.options, nuxt.options.nitro || {})
const nitroDevContext = getNitroContext(nuxt.options, { preset: 'local' })
nuxt.server = createDevServer(nitroDevContext)
// Connect hooks
nuxt.addHooks(nitroContext.nuxtHooks)
nuxt.hook('close', () => nitroContext._internal.hooks.callHook('close'))
nuxt.addHooks(nitroDevContext.nuxtHooks)
nuxt.hook('close', () => nitroDevContext._internal.hooks.callHook('close'))
// Expose process.env.NITRO_PRESET
nuxt.options.env.NITRO_PRESET = nitroContext.preset
// Resolve middleware
nuxt.hook('modules:done', () => {
const { middleware, legacyMiddleware } =
resolveMiddleware(nuxt.options.serverMiddleware, nuxt.resolver.resolvePath)
nuxt.server.setLegacyMiddleware(legacyMiddleware)
nitroContext.middleware.push(...middleware)
nitroDevContext.middleware.push(...middleware)
})
// nuxt build/dev
nuxt.hook('build:done', async () => {
if (nuxt.options.dev) {
await build(nitroDevContext)
} else if (!nitroContext._nuxt.isStatic) {
await prepare(nitroContext)
await generate(nitroContext)
await build(nitroContext)
}
})
// nude dev
if (nuxt.options.dev) {
nitroDevContext._internal.hooks.hook('nitro:compiled', () => { nuxt.server.watch() })
nuxt.hook('build:compile', ({ compiler }) => { compiler.outputFileSystem = wpfs })
nuxt.hook('server:devMiddleware', (m) => { nuxt.server.setDevMiddleware(m) })
}
}

View File

@ -10,6 +10,7 @@ import { version } from '../../package.json'
import ModuleContainer from './module'
import Resolver from './resolver'
import { initNitro } from './nitro'
declare global {
namespace NodeJS {
@ -42,8 +43,6 @@ export default class Nuxt extends Hookable {
this.resolver = new Resolver(this)
this.moduleContainer = new ModuleContainer(this)
this.server = {} // SIGMA TODO
// Call ready
if (this.options._ready !== false) {
this.ready().catch((err) => {
@ -79,10 +78,8 @@ export default class Nuxt extends Hookable {
// Await for modules
await this.moduleContainer.ready()
// Await for server to be ready
if (this.server) {
await this.server.ready()
}
// Await for server
await initNitro(this)
// Call ready hook
await this.callHook('ready', this)