From df3e2fd78996fe5608668dacf30de5901ea59c72 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 23 Mar 2021 21:45:41 +0000 Subject: [PATCH] fix: prevent resolution errors on old node versions --- src/module/babel.ts | 5 ++--- src/module/utils.ts | 14 +++++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/module/babel.ts b/src/module/babel.ts index ea6e0682..ba895c0e 100644 --- a/src/module/babel.ts +++ b/src/module/babel.ts @@ -1,5 +1,6 @@ import type { NuxtOptions } from '@nuxt/types' import type { ModuleThis } from '@nuxt/types/config/module' +import { resolveRelativePath } from './utils' export function registerBabelPlugin(this: ModuleThis) { const nuxtOptions: NuxtOptions = this.nuxt.options @@ -16,9 +17,7 @@ export function registerBabelPlugin(this: ModuleThis) { 'Unable to automatically add Babel plugin. Make sure your custom `build.babel.plugins` returns `@nuxtjs/composition-api/babel`' ) } else { - nuxtOptions.build.babel.plugins.push( - require.resolve('@nuxtjs/composition-api/babel') - ) + nuxtOptions.build.babel.plugins.push(resolveRelativePath('babel')) } /** diff --git a/src/module/utils.ts b/src/module/utils.ts index 62006aec..bdeda5f8 100644 --- a/src/module/utils.ts +++ b/src/module/utils.ts @@ -15,6 +15,18 @@ export function isUrl(url: string) { return ['http', '//'].some(str => url.startsWith(str)) } +export function resolveRelativePath(id: string) { + let src + + try { + src = require.resolve(`@nuxtjs/composition-api/${id}`) + } catch { + src = require.resolve(join(__dirname, `./${id}`)) + } + + return src +} + export function addResolvedTemplate( this: ModuleThis, template: string, @@ -22,7 +34,7 @@ export function addResolvedTemplate( ) { const nuxtOptions: NuxtOptions = this.nuxt.options - const src = require.resolve(`@nuxtjs/composition-api/${template}`) + const src = resolveRelativePath(template) const { dst } = this.addTemplate({ src, fileName: join('composition-api', basename(src)),