diff --git a/packages/nuxt3/src/cli.ts b/packages/nuxt3/src/cli.ts index a91bfc55a2..6286981ee0 100755 --- a/packages/nuxt3/src/cli.ts +++ b/packages/nuxt3/src/cli.ts @@ -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) diff --git a/packages/nuxt3/src/config/options.ts b/packages/nuxt3/src/config/options.ts index ef8635d0a4..6a55861566 100644 --- a/packages/nuxt3/src/config/options.ts +++ b/packages/nuxt3/src/config/options.ts @@ -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 } diff --git a/packages/nuxt3/src/core/nitro.ts b/packages/nuxt3/src/core/nitro.ts new file mode 100644 index 0000000000..17d31c0b06 --- /dev/null +++ b/packages/nuxt3/src/core/nitro.ts @@ -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) }) + } +} diff --git a/packages/nuxt3/src/core/nuxt.ts b/packages/nuxt3/src/core/nuxt.ts index b56002b6da..a8550a1a16 100644 --- a/packages/nuxt3/src/core/nuxt.ts +++ b/packages/nuxt3/src/core/nuxt.ts @@ -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)