Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Commit

Permalink
feat: emitting declaration file for .ts files too
Browse files Browse the repository at this point in the history
also fixed a bug where "outDir" in tsconfig was ignored
  • Loading branch information
egoist committed Aug 25, 2021
1 parent 81c3898 commit c2282f3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ vue-dts-gen src/App.vue
# Emits dist/App.d.ts

# Or glob patterns
vue-dts-gen "src/*.vue"
vue-dts-gen "src/*.{ts,vue}"
# Emits dist/*.d.ts
```

Expand Down
2 changes: 2 additions & 0 deletions examples/main.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const _default: 123;
export default _default;
1 change: 1 addition & 0 deletions examples/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 123;
32 changes: 23 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import fs from 'fs'
import { Project, SourceFile } from 'ts-morph'
import { Project, SourceFile, CompilerOptions } from 'ts-morph'
import glob from 'fast-glob'
import resolveFrom from 'resolve-from'

Expand Down Expand Up @@ -28,14 +28,17 @@ export async function build({ input, outDir }: Options) {
const tsConfigFilePath = fs.existsSync('tsconfig.json')
? 'tsconfig.json'
: undefined
const compilerOptions: CompilerOptions = {
allowJs: true,
declaration: true,
emitDeclarationOnly: true,
noEmitOnError: true,
}
if (outDir) {
compilerOptions.outDir = outDir
}
const project = new Project({
compilerOptions: {
allowJs: true,
declaration: true,
emitDeclarationOnly: true,
noEmitOnError: true,
outDir,
},
compilerOptions,
tsConfigFilePath,
skipAddingFilesFromTsConfig: true,
})
Expand All @@ -46,6 +49,17 @@ export async function build({ input, outDir }: Options) {
await Promise.all(
files.map(async (file) => {
const content = await fs.promises.readFile(file, 'utf8')
if (file.endsWith('.ts')) {
const sourceFile = project.createSourceFile(
path.relative(process.cwd(), file),
content,
{
overwrite: true,
},
)
sourceFiles.push(sourceFile)
return
}
const sfc = vueCompiler.parse(content)
const { script, scriptSetup } = sfc.descriptor
if (script || scriptSetup) {
Expand Down Expand Up @@ -74,7 +88,7 @@ export async function build({ input, outDir }: Options) {
const diagnostics = project.getPreEmitDiagnostics()
console.log(project.formatDiagnosticsWithColorAndContext(diagnostics))

project.emitToMemory()
const emitted = project.emitToMemory()

for (const sourceFile of sourceFiles) {
const emitOutput = sourceFile.getEmitOutput()
Expand Down

0 comments on commit c2282f3

Please sign in to comment.