From 66635ffa3e439c069c0e544e08dc08c666eefe16 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 24 May 2022 15:15:57 +0800 Subject: [PATCH] fix: generate relative dts --- src/core/ctx.ts | 30 +++++++++++++++++++++-------- test/__snapshots__/dts.test.ts.snap | 1 + test/dts.test.ts | 5 ++++- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/core/ctx.ts b/src/core/ctx.ts index 5e787e4..7514e44 100644 --- a/src/core/ctx.ts +++ b/src/core/ctx.ts @@ -1,4 +1,4 @@ -import { resolve } from 'path' +import { dirname, relative, resolve } from 'path' import { promises as fs } from 'fs' import { throttle, toArray } from '@antfu/utils' import { createFilter } from '@rollup/pluginutils' @@ -58,9 +58,24 @@ export function createContext(options: Options = {}, root = process.cwd()) { ? resolve(root, 'auto-imports.d.ts') : resolve(root, preferDTS) - const generateConfigFiles = throttle(500, false, () => { + function generateDTS(file: string) { + const dir = dirname(file) + return unimport.generateTypeDecarations({ + resolvePath: (i) => { + if (i.from.startsWith('.') || i.from.startsWith('/')) { + const related = relative(dir, i.from).replace(/\.ts$/, '') + return !related.startsWith('.') + ? `./${related}` + : related + } + return i.from + }, + }) + } + + const writeConfigFiles = throttle(500, false, () => { if (dts) - fs.writeFile(dts, unimport.generateTypeDecarations(), 'utf-8') + fs.writeFile(dts, generateDTS(dts), 'utf-8') if (eslintrc.enabled && eslintrc.filepath) fs.writeFile(eslintrc.filepath, generateESLintConfigs(unimport.getImports(), eslintrc), 'utf-8') }) @@ -76,7 +91,7 @@ export function createContext(options: Options = {}, root = process.cwd()) { ] as Import[] }) } - generateConfigFiles() + writeConfigFiles() } async function transform(code: string, id: string) { @@ -87,7 +102,7 @@ export function createContext(options: Options = {}, root = process.cwd()) { if (!s.hasChanged()) return - generateConfigFiles() + writeConfigFiles() return { code: s.toString(), @@ -103,10 +118,9 @@ export function createContext(options: Options = {}, root = process.cwd()) { dirs, filter, scanDirs, - generateConfigFiles, + generateConfigFiles: writeConfigFiles, transform, - // for testing - unimport: unimport as any, + generateDTS, } } diff --git a/test/__snapshots__/dts.test.ts.snap b/test/__snapshots__/dts.test.ts.snap index b6698a9..14f91b7 100644 --- a/test/__snapshots__/dts.test.ts.snap +++ b/test/__snapshots__/dts.test.ts.snap @@ -50,6 +50,7 @@ declare global { const fade: typeof import('svelte/transition')['fade'] const flip: typeof import('svelte/animate')['flip'] const fly: typeof import('svelte/transition')['fly'] + const foo: typeof import('./foo')['foo'] const forwardRef: typeof import('react')['forwardRef'] const get: typeof import('svelte/store')['get'] const getAllContexts: typeof import('svelte')['getAllContexts'] diff --git a/test/dts.test.ts b/test/dts.test.ts index 3dbffb2..9959eab 100644 --- a/test/dts.test.ts +++ b/test/dts.test.ts @@ -1,6 +1,8 @@ +import { join } from 'path' import { createContext } from '../src/core/ctx' it('dts', () => { + const cwd = process.cwd() const ctx = createContext({ imports: [ 'vue-demi', @@ -19,10 +21,11 @@ it('dts', () => { custom2: [ ['*', 'custom2'], ], + [join(cwd, 'foo.ts')]: ['foo'], }, 'vue/macros', ], }) - expect(ctx.unimport.generateTypeDecarations()).toMatchSnapshot() + expect(ctx.generateDTS(join(cwd, 'index.d.ts'))).toMatchSnapshot() })