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

fix: use middleware for vite registration #479

Closed
wants to merge 8 commits into from
27 changes: 26 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -38,11 +39,35 @@ const compositionApiModule: Module<never> = function compositionApiModule() {
})
})

// Add dummy middleware file

const middleware = addResolvedTemplate.call(this, 'register.mjs')
this.nuxt.hook(
'build:templates',
({ templateVars }: { templateVars: Record<string, any> }) => {
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
Expand Down
1 change: 1 addition & 0 deletions src/runtime/templates/register.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}