Skip to content

Commit

Permalink
fix: bundled dependencies should lead to a Yarn nohoist directive (#760)
Browse files Browse the repository at this point in the history
* fix: bundled dependencies should lead to a Yarn nohoist directive

Without this directive, the dependency will be hoisted to a top-level
`node_modules` directory, but if it's there NPM will not bundle it
when running `npm pack`.

* chore: self mutation

Signed-off-by: github-actions <[email protected]>

---------

Signed-off-by: github-actions <[email protected]>
Co-authored-by: github-actions <[email protected]>
  • Loading branch information
rix0rrr and github-actions authored Jan 30, 2025
1 parent 59f0dff commit 49e85be
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
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

0 comments on commit 49e85be

Please sign in to comment.