Skip to content

Commit

Permalink
fix: set nuxt context and normalise config as first module (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jul 12, 2022
1 parent 55c5f1a commit 9be3a44
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
49 changes: 35 additions & 14 deletions module.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,42 @@ module.exports = function (...args) {
const pkg = require('./package.json')

module.exports.defineNuxtConfig = (config = {}) => {
if (config.bridge !== false) {
// Add new handlers options
config.serverHandlers = config.serverHandlers || []
config.devServerHandlers = config.devServerHandlers || []

config.bridge = config.bridge || {}
config.bridge._version = pkg.version
if (!config.buildModules) {
config.buildModules = []
}
if (!config.buildModules.find(m => m === '@nuxt/bridge' || m === '@nuxt/bridge-edge')) {
// Ensure other modules register their hooks before
config.buildModules.push('@nuxt/bridge')
}
// Nuxt kit depends on this flag to check bridge compatibility
config.bridge = config.bridge ?? true

if (!config.bridge) { return config }

// Add new handlers options
config.serverHandlers = config.serverHandlers || []
config.devServerHandlers = config.devServerHandlers || []

config.bridge = config.bridge || {}
config.bridge._version = pkg.version
if (!config.buildModules) {
config.buildModules = []
}
if (!config.buildModules.find(m => m === '@nuxt/bridge' || m === '@nuxt/bridge-edge')) {
// Ensure other modules register their hooks before
config.buildModules.push('@nuxt/bridge')
}
config.buildModules.unshift(async function () {
const nuxt = this.nuxt

const { nuxtCtx } = await import('@nuxt/kit')

// Allow using kit composables in all modules
if (nuxtCtx.use()) {
nuxtCtx.unset()
}
nuxtCtx.set(nuxt)

// Mock _layers for nitro and auto-imports
nuxt.options._layers = nuxt.options._layers || [{
config: nuxt.options,
cwd: nuxt.options.rootDir,
configFile: nuxt.options._nuxtConfigFile
}]
})
return config
}

Expand Down
17 changes: 1 addition & 16 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createRequire } from 'module'
import { defineNuxtModule, installModule, checkNuxtCompatibility, nuxtCtx } from '@nuxt/kit'
import { defineNuxtModule, installModule, checkNuxtCompatibility } from '@nuxt/kit'
import type { NuxtModule } from '@nuxt/schema'
import { NuxtCompatibility } from '@nuxt/schema'
import type { BridgeConfig } from '../types'
Expand Down Expand Up @@ -36,23 +36,8 @@ export default defineNuxtModule({
// Disable if users explicitly set to false
if ((nuxt.options as any).bridge === false) { return }

// Nuxt kit depends on this flag to check bridge compatibility
(nuxt.options as any).bridge = (nuxt.options as any).bridge || true

const _require = createRequire(import.meta.url)

// Allow using kit composables in all modules
if (!nuxtCtx.use()) {
nuxtCtx.set(nuxt)
}

// Mock _layers
nuxt.options._layers = nuxt.options._layers || [{
config: nuxt.options,
cwd: nuxt.options.rootDir,
configFile: nuxt.options._nuxtConfigFile
}]

if (opts.nitro) {
nuxt.hook('modules:done', async () => {
await setupNitroBridge()
Expand Down

0 comments on commit 9be3a44

Please sign in to comment.