From dfc65a0c01e1b95faca39e6dc3c3d58d55392fe1 Mon Sep 17 00:00:00 2001 From: Ayoub Adib Date: Thu, 17 Mar 2022 14:03:36 +0100 Subject: [PATCH] feat: force not removing comments while generating ts declaration To preserve jsdoc comments --- examples/ts-module/src/Button.tsx | 3 +++ src/bundler/bundler.ts | 18 +++++------------- src/bundler/typescript.ts | 18 +++++++++++++++++- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/examples/ts-module/src/Button.tsx b/examples/ts-module/src/Button.tsx index 9fec92c..c93ed90 100644 --- a/examples/ts-module/src/Button.tsx +++ b/examples/ts-module/src/Button.tsx @@ -2,6 +2,9 @@ interface ButtonProps { label: string; } +/** + * Button component + */ export const Button = (props: ButtonProps) => { return ( <> diff --git a/src/bundler/bundler.ts b/src/bundler/bundler.ts index 523f30a..1696049 100644 --- a/src/bundler/bundler.ts +++ b/src/bundler/bundler.ts @@ -1,12 +1,13 @@ /* eslint-disable sonarjs/cognitive-complexity */ -import path from "path"; import { build } from "esbuild"; -import { helpers } from "termost"; import { CWD } from "../constants"; import { ModuleFormat } from "../types"; import { getPackageMetadata } from "./package"; import { jsxPlugin } from "./plugins"; -import { getTypeScriptConfiguration } from "./typescript"; +import { + generateTypeScriptDeclaration, + getTypeScriptConfiguration, +} from "./typescript"; type BundleParameters = { isProduction: boolean; @@ -36,16 +37,7 @@ export const bundle = async ({ if (!outfile) return null; - try { - const typingDir = path.dirname(outfile); - - await helpers.exec( - `tsc --declaration --emitDeclarationOnly --incremental --outDir ${typingDir}`, - { cwd: CWD } - ); - } catch (error) { - throw new Error(`Typing generation failed:\n${error}`); - } + await generateTypeScriptDeclaration(outfile); return outfile; }; diff --git a/src/bundler/typescript.ts b/src/bundler/typescript.ts index b9f4409..da4cdd7 100644 --- a/src/bundler/typescript.ts +++ b/src/bundler/typescript.ts @@ -1,4 +1,5 @@ -import { resolve } from "path"; +import { dirname, resolve } from "path"; +import { helpers } from "termost"; import { CWD } from "../constants"; export type TypeScriptConfiguration = { @@ -44,6 +45,21 @@ export const getTypeScriptConfiguration = } }; +export const generateTypeScriptDeclaration = async (outfile: string) => { + const outdir = dirname(outfile); + + try { + await helpers.exec( + `tsc --declaration --emitDeclarationOnly --incremental --removeComments false --outDir ${outdir}`, + { cwd: CWD } + ); + + return outdir; + } catch (error) { + throw new Error(`Type generation failed:\n${error}`); + } +}; + export const hasTypeScript = ( tsConfig: TypeScriptConfiguration | null ): tsConfig is TypeScriptConfiguration => {