Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 9d4165a

Browse files
committed
fix: register composition api before middleware
* 3-prong approach based on whether middleware/layouts are enabled features closes #457
1 parent 0aca79d commit 9d4165a

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/module.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Module, NuxtOptions } from '@nuxt/types'
2+
import { relative, sep } from 'upath'
23

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

@@ -40,9 +41,28 @@ const compositionApiModule: Module<never> = function compositionApiModule() {
4041
})
4142
})
4243

43-
// Register the Vue Composition API before any other layouts
44-
45-
this.addLayout(resolveRelativePath('runtime/templates/layout.js'), '0')
44+
// Register the Vue Composition API
45+
46+
if (nuxtOptions.features.middleware) {
47+
const middleware = addResolvedTemplate.call(this, 'register.js')
48+
this.nuxt.hook(
49+
'build:templates',
50+
({ templateVars }: { templateVars: Record<string, any> }) => {
51+
templateVars.middleware.unshift({
52+
src: middleware,
53+
dst: '.' + sep + relative(nuxtOptions.buildDir, middleware),
54+
name: 'compositionApiRegistration',
55+
})
56+
}
57+
)
58+
} else if (nuxtOptions.features.layouts) {
59+
this.addLayout(resolveRelativePath('runtime/templates/layout.js'), '0')
60+
} else {
61+
const dst = addResolvedTemplate.call(this, 'register.js')
62+
this.nuxt.hook('modules:done', () =>
63+
this.nuxt.hook('build:before', () => nuxtOptions.plugins.unshift(dst))
64+
)
65+
}
4666

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

src/runtime/templates/register.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Vue from 'vue'
2+
import CompositionApi from '@vue/composition-api'
3+
4+
Vue.use(CompositionApi)
5+

0 commit comments

Comments
 (0)