From 81b55caa8533ad174ec009535e5a030be21e6cc1 Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Wed, 22 Feb 2023 06:30:22 -0500 Subject: [PATCH] fix(ts): ensure typechecks pass without peer deps installed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - @ts-* comments are meaningful comments, but typescript does not preserve them 🙃🙄 - re-implemented flex-development/mlly@779cddf91f6d4e0e04ecebab0e0c6942aeaa9286 as build plugin - microsoft/TypeScript#38628 - https://github.com/microsoft/TypeScript/issues/38628#issuecomment-1439749496 Signed-off-by: Lexus Drumgold --- build.config.ts | 36 +++++++++++++++++++++++++++- src/interfaces/options-get-format.ts | 1 - src/interfaces/options-get-source.ts | 1 - 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/build.config.ts b/build.config.ts index cf6fcf80..53b5c15f 100644 --- a/build.config.ts +++ b/build.config.ts @@ -4,6 +4,7 @@ */ import { defineBuildConfig, type Config } from '@flex-development/mkbuild' +import type { BuildResult, PluginBuild } from 'esbuild' import pkg from './package.json' assert { type: 'json' } /** @@ -13,7 +14,40 @@ import pkg from './package.json' assert { type: 'json' } */ const config: Config = defineBuildConfig({ entries: [ - { dts: 'only' }, + { + dts: 'only', + plugins: [ + { + name: 'ts-ignore-peers', + setup({ onEnd }: PluginBuild): void { + return void onEnd((result: BuildResult<{ write: false }>): void => { + const { outputFiles } = result + + /** + * Regular expression used to insert `// @ts-ignore` above + * property declarations. + * + * @const {RegExp} regex + */ + const regex: RegExp = /\n( +)(.+?\??: )(import\('node-fetch'\).+)/ + + /** + * Property declaration with `// @ts-ignore` prepended. + * + * @const {string} replacement + */ + const replacement: string = + '\n$1// @ts-ignore peer dependency\n$1$2$3' + + return void (result.outputFiles = outputFiles.map(output => ({ + ...output, + text: output.text.replace(regex, replacement) + }))) + }) + } + } + ] + }, { dts: false, pattern: ['**/index.ts', 'enums/*', 'internal/*', 'utils/*'], diff --git a/src/interfaces/options-get-format.ts b/src/interfaces/options-get-format.ts index a3b70478..7796f34c 100644 --- a/src/interfaces/options-get-format.ts +++ b/src/interfaces/options-get-format.ts @@ -74,7 +74,6 @@ interface GetFormatOptions { * * @default {} */ - // @ts-ignore peer dependency req?: import('node-fetch').RequestInit | undefined } diff --git a/src/interfaces/options-get-source.ts b/src/interfaces/options-get-source.ts index a1da80d2..a57ef4a8 100644 --- a/src/interfaces/options-get-source.ts +++ b/src/interfaces/options-get-source.ts @@ -52,7 +52,6 @@ interface GetSourceOptions { * * @default {} */ - // @ts-ignore peer dependency req?: import('node-fetch').RequestInit | undefined }