diff --git a/src/module.ts b/src/module.ts index 3c0e3bef..fc60ed9e 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,5 +1,6 @@ import type { Module, NuxtOptions } from '@nuxt/types' -import { resolve } from 'upath' +import { writeFile } from 'fs-extra' +import { relative, resolve, sep } from 'upath' import { name, version } from '../package.json' @@ -38,11 +39,35 @@ const compositionApiModule: Module = function compositionApiModule() { }) }) + // Add dummy middleware file + + const middleware = addResolvedTemplate.call(this, 'register.mjs') + this.nuxt.hook( + 'build:templates', + ({ templateVars }: { templateVars: Record }) => { + templateVars.middleware.unshift({ + src: middleware, + dst: '.' + sep + relative(nuxtOptions.buildDir, middleware), + name: 'compositionApiRegistration', + }) + } + ) + // If we're using nuxt-vite, register vite plugin & inject configuration this.nuxt.hook('vite:extend', async (ctx: any) => { const { compositionApiPlugin } = await import('./vite-plugin') ctx.config.plugins.push(compositionApiPlugin()) + + await writeFile( + middleware, + ` + import Vue from 'vue'; + import CompositionApi from '@vue/composition-api'; + Vue.use(CompositionApi); + export default {}; + ` + ) }) // If we're using Babel, register Babel plugin for injecting keys diff --git a/src/runtime/templates/register.mjs b/src/runtime/templates/register.mjs new file mode 100644 index 00000000..b1c6ea43 --- /dev/null +++ b/src/runtime/templates/register.mjs @@ -0,0 +1 @@ +export default {}