Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs-json): use dts-bundle-generator to bundle types for docs-json #4619

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 copyStencilInternalDts(opts: BuildOptions, outputInternalDir: str
// @stencil/core/internal/stencil-public-docs.d.ts
const docsDtsSrcPath = join(declarationsInputDir, 'stencil-public-docs.d.ts');
const docsDtsDestPath = join(outputInternalDir, 'stencil-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);

// @stencil/core/internal/stencil-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 Stencil
* @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', 'stencil-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 Stencil compiler.
Expand Down
Loading