From f9c618e0462c3f75593b0a980f4babcb265ffc0c Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 10 Feb 2022 18:37:04 +0800 Subject: [PATCH] fix(cli): generate ExternalObject type on demand --- cli/src/build.ts | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/cli/src/build.ts b/cli/src/build.ts index fba5c764..da665e23 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -493,18 +493,6 @@ async function processIntermediateTypeFile( return idents } - const dtsHeader = `/* tslint:disable */ -/* eslint-disable */ - -/* auto-generated by NAPI-RS */ - -export class ExternalObject { - readonly '': { - readonly '': unique symbol - [K: symbol]: T - } -}\n` - const allDefs = lines.map((line) => JSON.parse(line) as TypeDef) function convertDefs(defs: TypeDef[], nested = false): string { @@ -597,8 +585,29 @@ export class ExternalObject { return acc + `export namespace ${mod} {\n${convertDefs(defs, true)}}\n` }, '') + const dtsHeader = `/* tslint:disable */ +/* eslint-disable */ + +/* auto-generated by NAPI-RS */\n +` + + const externalDef = + topLevelDef.indexOf('ExternalObject<') > -1 || + namespaceDefs.indexOf('ExternalObject<') > -1 + ? `export class ExternalObject { + readonly '': { + readonly '': unique symbol + [K: symbol]: T + } +}\n` + : '' + await unlinkAsync(source) - await writeFileAsync(target, dtsHeader + topLevelDef + namespaceDefs, 'utf8') + await writeFileAsync( + target, + dtsHeader + externalDef + topLevelDef + namespaceDefs, + 'utf8', + ) return idents }