Skip to content

Commit

Permalink
fix: register composition api before middleware
Browse files Browse the repository at this point in the history
* 3-prong approach based on whether middleware/layouts are enabled features

closes #457
  • Loading branch information
danielroe committed Apr 19, 2021
1 parent 0aca79d commit 9d4165a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Module, NuxtOptions } from '@nuxt/types'
import { relative, sep } from 'upath'

import { name, version } from '../package.json'

Expand Down Expand Up @@ -40,9 +41,28 @@ const compositionApiModule: Module<never> = function compositionApiModule() {
})
})

// Register the Vue Composition API before any other layouts

this.addLayout(resolveRelativePath('runtime/templates/layout.js'), '0')
// Register the Vue Composition API

if (nuxtOptions.features.middleware) {
const middleware = addResolvedTemplate.call(this, 'register.js')
this.nuxt.hook(
'build:templates',
({ templateVars }: { templateVars: Record<string, any> }) => {
templateVars.middleware.unshift({
src: middleware,
dst: '.' + sep + relative(nuxtOptions.buildDir, middleware),
name: 'compositionApiRegistration',
})
}
)
} else if (nuxtOptions.features.layouts) {
this.addLayout(resolveRelativePath('runtime/templates/layout.js'), '0')
} else {
const dst = addResolvedTemplate.call(this, 'register.js')
this.nuxt.hook('modules:done', () =>
this.nuxt.hook('build:before', () => nuxtOptions.plugins.unshift(dst))
)
}

// If we're using nuxt-vite, register vite plugin & inject configuration

Expand Down
5 changes: 5 additions & 0 deletions src/runtime/templates/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Vue from 'vue'
import CompositionApi from '@vue/composition-api'

Vue.use(CompositionApi)

0 comments on commit 9d4165a

Please sign in to comment.