Skip to content

Commit

Permalink
fix(declarations): bundle child_process type for portability (#5165)
Browse files Browse the repository at this point in the history
We need to make sure our type declarations don't depend on types we
don't bundle!

fixes #5162

STENCIL-1077
  • Loading branch information
alicewriteswrongs committed Jan 2, 2024
1 parent def7ef5 commit 59ecd9e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
24 changes: 24 additions & 0 deletions scripts/bundles/internal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { generateDtsBundle } from 'dts-bundle-generator';
import fs from 'fs-extra';
import { join } from 'path';

Expand Down Expand Up @@ -56,6 +57,29 @@ async function copyStencilInternalDts(opts: BuildOptions, outputInternalDir: str
const privateDtsDestPath = join(outputInternalDir, 'stencil-private.d.ts');
let privateDts = cleanDts(await fs.readFile(privateDtsSrcPath, 'utf8'));

// @stencil/core/internal/child_process.d.ts
const childProcessSrcPath = join(declarationsInputDir, 'child_process.d.ts');
const childProcessDestPath = join(outputInternalDir, 'child_process.d.ts');

// we generate a tiny tiny bundle here of just
// `src/declarations/child_process.ts` so that `internal/stencil-private.d.ts`
// can import from `'./child_process'` without worrying about resolving the
// types from `node_modules`.
const childProcessDts = generateDtsBundle([
{
filePath: childProcessSrcPath,
libraries: {
// we need to mark this library so that types imported from it are inlined
inlinedLibraries: ['child_process'],
},
output: {
noBanner: true,
exportReferencedTypes: false,
},
},
]).join('\n');
await fs.writeFile(childProcessDestPath, childProcessDts);

// the private `.d.ts` imports the `Result` type from the `@utils` module, so
// we need to rewrite the path so it imports from the right relative path
privateDts = privateDts.replace('@utils', './utils');
Expand Down
2 changes: 1 addition & 1 deletion scripts/utils/bundle-dts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntryPointConfig, generateDtsBundle, OutputOptions } from 'dts-bundle-generator/dist/bundle-generator.js';
import { EntryPointConfig, generateDtsBundle, OutputOptions } from 'dts-bundle-generator';
import fs from 'fs-extra';

import { BuildOptions } from './options';
Expand Down
5 changes: 5 additions & 0 deletions src/declarations/child_process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Serializable } from 'child_process';

// this is just so that we can bundle this type for `stencil-private.d.ts`
// without having to run dts-bundle-generator on that whole file
export { Serializable as CPSerializable };
2 changes: 1 addition & 1 deletion src/declarations/stencil-private.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { result } from '@utils';
import type { Serializable as CPSerializable } from 'child_process';

import type { InMemoryFileSystem } from '../compiler/sys/in-memory-fs';
import type { CPSerializable } from './child_process';
import type {
BuildEvents,
BuildLog,
Expand Down

0 comments on commit 59ecd9e

Please sign in to comment.