fix(nuxt): don't try to strip directory file extensions (#25965)

This commit is contained in:
Mehmet 2024-02-27 12:11:27 +03:00 committed by GitHub
parent 3678a4f5d0
commit 40c3a3918e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 5 deletions

View File

@ -1,3 +1,4 @@
import { existsSync } from 'node:fs'
import { addTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, isIgnored, logger, resolveAlias, tryResolveModule, updateTemplates, useNuxt } from '@nuxt/kit'
import { isAbsolute, join, normalize, relative, resolve } from 'pathe'
import type { Import, Unimport } from 'unimport'
@ -5,6 +6,7 @@ import { createUnimport, scanDirExports } from 'unimport'
import type { ImportPresetWithDeprecation, ImportsOptions } from 'nuxt/schema'
import { lookupNodeModuleSubpath, parseNodeModulePath } from 'mlly'
import { isDirectory } from '../utils'
import { TransformPlugin } from './transform'
import { defaultPresets } from './presets'
@ -140,13 +142,10 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
function addDeclarationTemplates (ctx: Unimport, options: Partial<ImportsOptions>) {
const nuxt = useNuxt()
// Remove file extension for benefit of TypeScript
const stripExtension = (path: string) => path.replace(/\.[a-z]+$/, '')
const resolvedImportPathMap = new Map<string, string>()
const r = ({ from }: Import) => resolvedImportPathMap.get(from)
async function cacheImportPaths(imports: Import[]) {
async function cacheImportPaths (imports: Import[]) {
const importSource = Array.from(new Set(imports.map(i => i.from)))
await Promise.all(importSource.map(async (from) => {
if (resolvedImportPathMap.has(from)) {
@ -163,11 +162,15 @@ function addDeclarationTemplates (ctx: Unimport, options: Partial<ImportsOptions
return join(dir, name, subpath || '')
}) ?? path
}
if (existsSync(path) && !(await isDirectory(path))) {
path = path.replace(/\.[a-z]+$/, '')
}
if (isAbsolute(path)) {
path = relative(join(nuxt.options.buildDir, 'types'), path)
}
path = stripExtension(path)
resolvedImportPathMap.set(from, path)
}))
}

View File

@ -1,3 +1,9 @@
import { promises as fsp } from 'node:fs'
export function toArray<T> (value: T | T[]): T[] {
return Array.isArray(value) ? value : [value]
}
export async function isDirectory (path: string) {
return (await fsp.lstat(path)).isDirectory()
}