Skip to content

Commit

Permalink
fix: generate relative dts
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 24, 2022
1 parent 9893e85 commit 66635ff
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
30 changes: 22 additions & 8 deletions 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'
Expand Down Expand Up @@ -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')
})
Expand All @@ -76,7 +91,7 @@ export function createContext(options: Options = {}, root = process.cwd()) {
] as Import[]
})
}
generateConfigFiles()
writeConfigFiles()
}

async function transform(code: string, id: string) {
Expand All @@ -87,7 +102,7 @@ export function createContext(options: Options = {}, root = process.cwd()) {
if (!s.hasChanged())
return

generateConfigFiles()
writeConfigFiles()

return {
code: s.toString(),
Expand All @@ -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,
}
}

Expand Down
1 change: 1 addition & 0 deletions test/__snapshots__/dts.test.ts.snap
Expand Up @@ -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']
Expand Down
5 changes: 4 additions & 1 deletion 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',
Expand All @@ -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()
})

0 comments on commit 66635ff

Please sign in to comment.