Skip to content

Commit

Permalink
fix(docs-json): use dts-bundle-generator to bundle types for docs-json
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhduy1407 committed Dec 4, 2023
1 parent f0db317 commit 9404cb6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
13 changes: 11 additions & 2 deletions scripts/bundles/internal.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand Down
30 changes: 21 additions & 9 deletions scripts/utils/bundle-dts.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
const cachedDtsOutput = inputFile + '-bundled.d.ts';

if (!opts.isProd) {
Expand All @@ -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);

Expand Down
9 changes: 8 additions & 1 deletion src/compiler/docs/json/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9404cb6

Please sign in to comment.