Skip to content

Commit

Permalink
chore(nuxt): switch back to nuxt-kit-extras with support for nuxt 2
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jan 23, 2022
1 parent bb383a3 commit caf63dc
Show file tree
Hide file tree
Showing 3 changed files with 3,402 additions and 383 deletions.
2 changes: 1 addition & 1 deletion integrations/nuxt/package.json
Expand Up @@ -40,7 +40,7 @@
"prepare": "nuxt-module-build --stub && nuxi prepare playground"
},
"dependencies": {
"@harlanzw/nuxt-kit-extras": "latest",
"@harlanzw/nuxt-kit-extras": "^0.2.4",
"@nuxt/kit": "npm:@nuxt/kit-edge@latest",
"@unlighthouse/core": "workspace:*",
"std-env": "^3.0.1"
Expand Down
18 changes: 8 additions & 10 deletions integrations/nuxt/src/module.ts
@@ -1,7 +1,8 @@
import { join } from 'path'
import {defineNuxtModule, addServerMiddleware, extendViteConfig, useNuxt } from '@nuxt/kit'
import type { RouteDefinition, UserConfig } from '@unlighthouse/core'
import type { UserConfig } from '@unlighthouse/core'
import { createUnlighthouse, useUnlighthouse, useLogger } from '@unlighthouse/core'
import {waitForRoutes, waitForDevServer} from "@harlanzw/nuxt-kit-extras";

export interface ModuleOptions extends UserConfig {

Expand All @@ -19,11 +20,7 @@ export default defineNuxtModule<ModuleOptions>({

const config = data as UserConfig

const routes = new Promise<RouteDefinition[]>(resolve => {
nuxt.hooks.hook('pages:extend', pages => {
resolve(pages as RouteDefinition[])
})
})
const routePromise = waitForRoutes()

const unlighthouse = useUnlighthouse() || await createUnlighthouse({
router: {
Expand All @@ -33,7 +30,8 @@ export default defineNuxtModule<ModuleOptions>({
root: nuxt.options.rootDir,
}, {
name: 'nuxt',
routeDefinitions: () => routes
// @ts-ignore
routeDefinitions: async () => await routePromise
})

// when we vite mode, the HTML is not server side rendered so we need to tell the scanner this
Expand All @@ -55,17 +53,17 @@ export default defineNuxtModule<ModuleOptions>({
},
})

nuxt.hooks.hook('listen', async (server, listener) => {
waitForDevServer().then(async ({ listenerServer, listener }) => {
const engine = useUnlighthouse()
const nuxtApp = useNuxt()
// for nuxt we can fully leverage the dev middleware server
await engine.setServerContext({
url: listener.url,
server: server,
server: listenerServer,
app: nuxtApp.server.app,
})
const logger = useLogger()
logger.success('⛵ Unlighthouse ready:' + engine.runtimeSettings.clientUrl)
logger.success('⛵ Unlighthouse ready: ' + engine.runtimeSettings.clientUrl)
})

nuxt.options.ignore.push(join(unlighthouse.resolvedConfig.outputPath, '**'))
Expand Down

0 comments on commit caf63dc

Please sign in to comment.