import type { Module , NuxtOptions } from '@nuxt/types' ; import { resolve } from 'upath' ; import type { Configuration } from 'webpack' ; import { name , version } from '../../package.json' ; import { registerBabelPlugin } from './babel-register' ; import { addGlobalsFile } from './globals-register' ; import { addResolvedTemplate , resolveCoreJsVersion } from './utils' ; const compositionApiModule = function compositionApiModule ( ) { const nuxt = this . nuxt as { options : NuxtOptions ; } ; addGlobalsFile . call ( this ) ; const runtimeDir = resolve ( __dirname , 'runtime' ) ; nuxt . options . build . transpile = nuxt . options . build . transpile || [ ] ; nuxt . options . build . transpile . push ( '@nuxtjs/composition-api' , runtimeDir ) ; const vueEntry = nuxt . options . alias . vue || ( nuxt . options . dev ? this . nuxt . resolver . resolveModule ( 'vue/dist/vue.common.dev.js' ) : this . nuxt . resolver . resolveModule ( 'vue/dist/vue.runtime.esm.js' ) ) ; const vueAliases = Object . fromEntries ( [ '.common.dev' , '.common' , '.common.prod' , '.esm.browser' , '.esm.browser.min' , '.esm' , '' , '.min' , '.runtime.common.dev' , '.runtime.common' , '.runtime.common.prod' , '.runtime.esm' , '.runtime' , '.runtime.min' , ] . flatMap ( m => [ `vue/dist/vue${ m }` , `vue/dist/vue${ m }.js` ] ) . map ( m => [ m , vueEntry ] ) ) ; nuxt . options . alias = { ... vueAliases , ... nuxt . options . alias , vue : vueEntry , } ; const capiEntrypoint = '@vue/composition-api/dist/vue-composition-api.mjs' ; const capiResolution = nuxt . options . alias [ '@vue/composition-api' ] || this . nuxt . resolver . resolveModule ( capiEntrypoint ) ; const capiAliases = Object . fromEntries ( [ '.common' , '' , '.prod' , '.common.prod' , '.esm' , '.mjs' ] . flatMap ( m => [ `@vue/composition-api/dist/vue-composition-api${ m }` , `@vue/composition-api/dist/vue-composition-api${ m }.js` , ] ) . map ( m => [ m , capiResolution ] ) ) ; nuxt . options . alias = { ... capiAliases , ... nuxt . options . alias , '@vue/composition-api' : capiResolution , } ; nuxt . options . alias [ '@nuxtjs/composition-api' ] = nuxt . options . alias [ '@nuxtjs/composition-api' ] || this . nuxt . resolver . resolveModule ( '@nuxtjs/composition-api' ) . replace ( '.js' , '.mjs' ) ; const registration = addResolvedTemplate . call ( this , 'register.mjs' ) ; this . nuxt . hook ( 'webpack:config' , ( config ) => { config . forEach ( config => { const entry = config . entry as Record < string , string [ ] > ; entry . app . unshift ( registration ) ; } ) ; } ) ; this . extendBuild ( config => { if ( ! config . module ) return ; config . module . rules . forEach ( rule => { if ( rule . test instanceof RegExp && rule . test . test ( 'index.mjs' ) ) { rule . type = 'javascript/auto' ; } } ) ; config . module . rules . unshift ( { test : /\.mjs$/ , type : 'javascript/auto' , include : [ /node_modules/ ] , } ) ; } ) ; const viteMiddleware = addResolvedTemplate . call ( this , 'middleware.mjs' ) ; this . nuxt . hook ( 'vite:extend' , async ( ctx ) => { const { compositionApiPlugin } = await import ( './vite-plugin' ) ; ctx . config . plugins . push ( compositionApiPlugin ( ) ) ; ctx . config . resolve . alias [ './middleware.js' ] = viteMiddleware ; } ) ; registerBabelPlugin . call ( this ) ; addResolvedTemplate . call ( this , 'polyfill.client.mjs' , { corejsPolyfill : resolveCoreJsVersion . call ( this ) , } ) ; const globalPlugin = addResolvedTemplate . call ( this , 'plugin.mjs' ) ; const metaPlugin = addResolvedTemplate . call ( this , 'meta.mjs' ) ; this . nuxt . hook ( 'modules:done' , ( ) => { nuxt . options . plugins . push ( metaPlugin ) ; nuxt . options . plugins . unshift ( globalPlugin ) ; } ) ; this . addModule ( 'unplugin-vue2-script-setup/nuxt' ) ; if ( ! this . nuxt . options . capi ?. disableMigrationWarning ) { this . nuxt . hook ( 'build:done' , ( ) => { console . info ( "`Nuxt Bridge has now been released in beta.` It has full composition API support and it's strongly recommended to migrate from `@nuxtjs/composition-api`, if possible, by following the steps at `https://v3.nuxtjs.org/getting-started/bridge`. Feedback welcome at `https://github.com/nuxt-community/composition-api/discussions/585`.\n" ) ; } ) ; } } ; compositionApiModule . meta = { name , version , } ; export default compositionApiModule ;