From 22cb8342d332a4a8a6e3020955e007e796d52e54 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 26 Aug 2024 15:25:30 +0100 Subject: [PATCH] fix(nuxt): handle mismatching declaration/plugin extensions (#28709) --- packages/nuxt/src/core/templates.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index d766d87f02..f4f5d4da8e 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -120,11 +120,21 @@ export const pluginsDeclaration: NuxtTemplate = { const relativePath = relative(typesDir, pluginPath) const correspondingDeclaration = pluginPath.replace(/\.(?[cm])?jsx?$/, '.d.$ts') + // if `.d.ts` file exists alongside a `.js` plugin, or if `.d.mts` file exists alongside a `.mjs` plugin, we can use the entire path if (correspondingDeclaration !== pluginPath && exists(correspondingDeclaration)) { tsImports.push(relativePath) continue } + const incorrectDeclaration = pluginPath.replace(/\.[cm]jsx?$/, '.d.ts') + // if `.d.ts` file exists, but plugin is `.mjs`, add `.js` extension to the import + // to hotfix issue until ecosystem updates to `@nuxt/module-builder@>=0.8.0` + if (incorrectDeclaration !== pluginPath && exists(incorrectDeclaration)) { + tsImports.push(relativePath.replace(/\.[cm](jsx?)$/, '.$1')) + continue + } + + // if there is no declaration we only want to remove the extension if it's a TypeScript file if (exists(pluginPath)) { if (TS_RE.test(pluginPath)) { tsImports.push(relativePath.replace(EXTENSION_RE, ''))