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: bundled dependencies should lead to a Yarn nohoist directive #760

Merged
merged 2 commits into from
Jan 30, 2025
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
11 changes: 11 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/yarn/monorepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export class Monorepo extends typescript.TypeScriptProject {
this.package.addField('private', true);
this.package.addField('workspaces', {
packages: this.projects.map((p) => p.workspaceDirectory),
...this.renderNoHoist(),
});

this.tsconfig?.file.addOverride('include', []);
Expand All @@ -183,6 +184,21 @@ export class Monorepo extends typescript.TypeScriptProject {
});
}

/**
* Render the 'nohoist' directive
*
* Bundled dependencies must be nohoist'ed, otherwise NPM silently won't bundle them.
*
* Renders an object that should be mixed into the `workspaces` object.
*/
private renderNoHoist(): any {
const nohoist = this.projects.flatMap(p => p.bundledDeps.flatMap(dep => [
`${p.name}/${dep}`,
`${p.name}/${dep}/**`,
]));
return nohoist.length > 0 ? { nohoist } : undefined;
}

/**
* Hooks into the install dependencies cycle
*/
Expand Down
3 changes: 3 additions & 0 deletions src/yarn/typescript-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TypeScriptWorkspaceOptions } from './typescript-workspace-options';
*/
export class TypeScriptWorkspace extends typescript.TypeScriptProject {
public readonly workspaceDirectory: string;
public readonly bundledDeps: string[] = [];

private readonly monorepo: Monorepo;
private readonly isPrivatePackage: boolean;
Expand Down Expand Up @@ -189,6 +190,8 @@ export class TypeScriptWorkspace extends typescript.TypeScriptProject {
this.addTsconfigDevFix();
this.addEslintRcFix();

this.bundledDeps.push(...options.bundledDeps ?? []);

options.parent.register(this);
}

Expand Down
27 changes: 27 additions & 0 deletions test/cdklabs-monorepo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ describe('CdkLabsMonorepo', () => {
expect(outdir).toMatchSnapshot();
});

test('bundled dependencies lead to a nohoist directive', () => {
// GIVEN
const parent = new yarn.CdkLabsMonorepo({
name: 'monorepo',
defaultReleaseBranch: 'main',
});

new yarn.TypeScriptWorkspace({
parent,
name: '@cdklabs/one',
// WHEN
bundledDeps: ['jsonschema'],
});

// THEN
const outdir = Testing.synth(parent);

expect(outdir['package.json']).toEqual(expect.objectContaining({
workspaces: expect.objectContaining({
nohoist: [
'@cdklabs/one/jsonschema',
'@cdklabs/one/jsonschema/**',
],
}),
}));
});

test('workspaces get monorepo repository configuration', () => {
const testRepository = 'https://github.com/cdklabs/cdklabs-projen-project-types';
const parent = new yarn.CdkLabsMonorepo({
Expand Down