Skip to content

Commit 6ba3249

Browse files
fix(docs-json): use dts-bundle-generator to bundle types for docs-json (#4619)
This fixes an issue that crops up when building documentation using the `docs-json` output target. When #4212 was merged it included a change to `src/declarations/stencil-public-docs.ts` which involved adding imports from private Stencil type declarations. Previously this file had no imports and so was "standalone", however, after this change it now imported some types from `"./private"`. This created a problem when using the `docs-json` output target because when building a Stencil project with that output target enabled the compiled typedef corresponding to `stencil-public-docs.ts` is copied out of `node_modules` and into the users project. See here: https://github.com/ionic-team/stencil/blob/43cfc584c8fbcb77122cbf2a813b1a0303085dce/src/compiler/docs/json/index.ts#L16-L18 This commit fixes the issue by using `dts-bundle-generator` to inline the types imported by `stencil-public-docs.ts` so that the resulting `.d.ts` file has no external dependencies and is therefore portable.
1 parent a5134df commit 6ba3249

File tree

4 files changed

+362
-252
lines changed

4 files changed

+362
-252
lines changed

scripts/bundles/internal.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs-extra';
22
import { join } from 'path';
33

4-
import { cleanDts } from '../utils/bundle-dts';
4+
import { bundleDts, cleanDts } from '../utils/bundle-dts';
55
import type { BuildOptions } from '../utils/options';
66
import { writePkgJson } from '../utils/write-pkg-json';
77
import { internalAppData } from './internal-app-data';
@@ -70,7 +70,16 @@ async function copyStencilInternalDts(opts: BuildOptions, outputInternalDir: str
7070
// @stencil/core/internal/stencil-public-docs.d.ts
7171
const docsDtsSrcPath = join(declarationsInputDir, 'stencil-public-docs.d.ts');
7272
const docsDtsDestPath = join(outputInternalDir, 'stencil-public-docs.d.ts');
73-
const docsDts = cleanDts(await fs.readFile(docsDtsSrcPath, 'utf8'));
73+
// We bundle with `dts-bundle-generator` here to ensure that when the `docs-json`
74+
// OT writes a `docs.d.ts` file based on this file it is fully portable.
75+
const docsDts = await bundleDts(opts, docsDtsSrcPath, {
76+
// we want to suppress the `dts-bundle-generator` banner here because we do
77+
// our own later on
78+
noBanner: true,
79+
// we also don't want the types which are inlined into our bundled file to
80+
// be re-exported, which will change the 'surface' of the module
81+
exportReferencedTypes: false,
82+
});
7483
await fs.writeFile(docsDtsDestPath, docsDts);
7584

7685
// @stencil/core/internal/stencil-public-runtime.d.ts

scripts/utils/bundle-dts.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
import { generateDtsBundle } from 'dts-bundle-generator/dist/bundle-generator.js';
1+
import { EntryPointConfig, generateDtsBundle, OutputOptions } from 'dts-bundle-generator/dist/bundle-generator.js';
22
import fs from 'fs-extra';
33

44
import { BuildOptions } from './options';
55

6-
export async function bundleDts(opts: BuildOptions, inputFile: string) {
6+
/**
7+
* A thin wrapper for `dts-bundle-generator` which uses our build options to
8+
* set a few things up
9+
*
10+
* **Note**: this file caches its output to disk, and will return any
11+
* previously cached file if not in a prod environment!
12+
*
13+
* @param opts an object holding information about the current build of Stencil
14+
* @param inputFile the path to the file which should be bundled
15+
* @param outputOptions options for bundling the file
16+
* @returns a string containing the bundled typedef
17+
*/
18+
export async function bundleDts(opts: BuildOptions, inputFile: string, outputOptions?: OutputOptions): Promise<string> {
719
const cachedDtsOutput = inputFile + '-bundled.d.ts';
820

921
if (!opts.isProd) {
@@ -12,15 +24,15 @@ export async function bundleDts(opts: BuildOptions, inputFile: string) {
1224
} catch (e) {}
1325
}
1426

15-
const entries = [
16-
{
17-
filePath: inputFile,
18-
},
19-
];
27+
const config: EntryPointConfig = {
28+
filePath: inputFile,
29+
};
2030

21-
let outputCode = generateDtsBundle(entries).join('\n');
31+
if (outputOptions) {
32+
config.output = outputOptions;
33+
}
2234

23-
outputCode = cleanDts(outputCode);
35+
const outputCode = cleanDts(generateDtsBundle([config]).join('\n'));
2436

2537
await fs.writeFile(cachedDtsOutput, outputCode);
2638

src/compiler/docs/json/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ export const generateJsonDocs = async (
1414
return;
1515
}
1616
const docsDtsPath = join(config.sys.getCompilerExecutingPath(), '..', '..', 'internal', 'stencil-public-docs.d.ts');
17-
const docsDts = await compilerCtx.fs.readFile(docsDtsPath);
17+
let docsDts = await compilerCtx.fs.readFile(docsDtsPath);
18+
// this file was written by dts-bundle-generator, which uses tabs for
19+
// indentation. Instead, let's replace those with spaces!
20+
docsDts = docsDts
21+
.split('\n')
22+
.map((line) => line.replace(/\t/g, ' '))
23+
.join('\n');
24+
1825
const typesContent = `
1926
/**
2027
* This is an autogenerated file created by the Stencil compiler.

0 commit comments

Comments
 (0)