diff --git a/scripts/bundles/internal.ts b/scripts/bundles/internal.ts index dbc1478a..8f391ad5 100644 --- a/scripts/bundles/internal.ts +++ b/scripts/bundles/internal.ts @@ -1,7 +1,7 @@ import fs from 'fs-extra'; import { join } from 'path'; -import { cleanDts } from '../utils/bundle-dts'; +import { bundleDts, cleanDts } from '../utils/bundle-dts'; import type { BuildOptions } from '../utils/options'; import { writePkgJson } from '../utils/write-pkg-json'; import { internalAppData } from './internal-app-data'; @@ -70,7 +70,16 @@ async function copyRindoInternalDts(opts: BuildOptions, outputInternalDir: strin // @rindo/core/internal/rindo-public-docs.d.ts const docsDtsSrcPath = join(declarationsInputDir, 'rindo-public-docs.d.ts'); const docsDtsDestPath = join(outputInternalDir, 'rindo-public-docs.d.ts'); - const docsDts = cleanDts(await fs.readFile(docsDtsSrcPath, 'utf8')); + // We bundle with `dts-bundle-generator` here to ensure that when the `docs-json` + // OT writes a `docs.d.ts` file based on this file it is fully portable. + const docsDts = await bundleDts(opts, docsDtsSrcPath, { + // we want to suppress the `dts-bundle-generator` banner here because we do + // our own later on + noBanner: true, + // we also don't want the types which are inlined into our bundled file to + // be re-exported, which will change the 'surface' of the module + exportReferencedTypes: false, + }); await fs.writeFile(docsDtsDestPath, docsDts); // @rindo/core/internal/rindo-public-runtime.d.ts diff --git a/scripts/utils/bundle-dts.ts b/scripts/utils/bundle-dts.ts index 9e7f5236..493bc876 100644 --- a/scripts/utils/bundle-dts.ts +++ b/scripts/utils/bundle-dts.ts @@ -1,9 +1,21 @@ -import { generateDtsBundle } from 'dts-bundle-generator/dist/bundle-generator.js'; +import { EntryPointConfig, generateDtsBundle, OutputOptions } from 'dts-bundle-generator/dist/bundle-generator.js'; import fs from 'fs-extra'; import { BuildOptions } from './options'; -export async function bundleDts(opts: BuildOptions, inputFile: string) { +/** + * A thin wrapper for `dts-bundle-generator` which uses our build options to + * set a few things up + * + * **Note**: this file caches its output to disk, and will return any + * previously cached file if not in a prod environment! + * + * @param opts an object holding information about the current build of Rindo + * @param inputFile the path to the file which should be bundled + * @param outputOptions options for bundling the file + * @returns a string containing the bundled typedef + */ +export async function bundleDts(opts: BuildOptions, inputFile: string, outputOptions?: OutputOptions): Promise { const cachedDtsOutput = inputFile + '-bundled.d.ts'; if (!opts.isProd) { @@ -12,15 +24,15 @@ export async function bundleDts(opts: BuildOptions, inputFile: string) { } catch (e) {} } - const entries = [ - { - filePath: inputFile, - }, - ]; + const config: EntryPointConfig = { + filePath: inputFile, + }; - let outputCode = generateDtsBundle(entries).join('\n'); + if (outputOptions) { + config.output = outputOptions; + } - outputCode = cleanDts(outputCode); + const outputCode = cleanDts(generateDtsBundle([config]).join('\n')); await fs.writeFile(cachedDtsOutput, outputCode); diff --git a/src/compiler/docs/json/index.ts b/src/compiler/docs/json/index.ts index 1ea963cb..91cf0857 100644 --- a/src/compiler/docs/json/index.ts +++ b/src/compiler/docs/json/index.ts @@ -14,7 +14,14 @@ export const generateJsonDocs = async ( return; } const docsDtsPath = join(config.sys.getCompilerExecutingPath(), '..', '..', 'internal', 'rindo-public-docs.d.ts'); - const docsDts = await compilerCtx.fs.readFile(docsDtsPath); + let docsDts = await compilerCtx.fs.readFile(docsDtsPath); + // this file was written by dts-bundle-generator, which uses tabs for + // indentation. Instead, let's replace those with spaces! + docsDts = docsDts + .split('\n') + .map((line) => line.replace(/\t/g, ' ')) + .join('\n'); + const typesContent = ` /** * This is an autogenerated file created by the Rindo compiler.