fix(nuxt3): use ts-friendly paths for global imports (#662)

This commit is contained in:
Daniel Roe 2021-10-05 22:47:19 +02:00 committed by GitHub
parent baa4ddbae0
commit ae0dbda2ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
import { addVitePlugin, addWebpackPlugin, defineNuxtModule, addTemplate, resolveAlias, addPluginTemplate } from '@nuxt/kit'
import { resolve } from 'pathe'
import { isAbsolute, relative, resolve } from 'pathe'
import type { Identifiers, GlobalImportsOptions } from './types'
import { TransformPlugin } from './transform'
import { defaultIdentifiers } from './identifiers'
@ -31,7 +31,17 @@ export default defineNuxtModule<GlobalImportsOptions>({
// Add types
const resolved = {}
const r = id => resolved[id] || (resolved[id] = resolveAlias(id, nuxt.options.alias))
const r = (id: string) => {
if (resolved[id]) { return resolved[id] }
let path = resolveAlias(id, nuxt.options.alias)
if (isAbsolute(path)) {
path = relative(nuxt.options.buildDir, path)
}
// Remove file extension for benefit of TypeScript
path = path.replace(/\.[a-z]+$/, '')
resolved[id] = path
return path
}
addTemplate({
filename: 'global-imports.d.ts',