Skip to content

Add Cdk.NodejsFunction#2458

Open
zbrydon wants to merge 29 commits into
mainfrom
add-cdk-NodejsFunction
Open

Add Cdk.NodejsFunction#2458
zbrydon wants to merge 29 commits into
mainfrom
add-cdk-NodejsFunction

Conversation

@zbrydon

@zbrydon zbrydon commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Motivation

The main driver is making the CDK NodejsFunction construct work with the strict pnpm settings we want to enforce, and those enforced by default in pnpm 11. The upstream construct doesn't play nicely with these, and thought we can use rolldown instead of esbuild while we're at it.

Related upstream issues:

Overview

A NodejsFunction CDK construct under @skuba-lib/api/cdk, a drop-in alternative to aws-cdk-lib/aws-lambda-nodejs.NodejsFunction, built around rolldown and pnpm . At synth time it bundles a TS/JS entry into a single ESM index.mjs handler, optionally installs a subset of real node_modules, and packages the result as a Lambda asset.

Flow: Cdk.NodejsFunctionfunction.ts (validation) → bundling.ts (tryBundle) → spawns the rolldown bridge → optional pnpm installoutputDir becomes the asset.

Bits worth noting

  • cdk/index.ts is a lazy require() getter, not a normal export. aws-cdk-lib and constructs are optional peers; the getter defers loading them so consumers who never use the construct don't need them installed. See discussion on potential options
  • Local bundling only, no Docker fallback. Bundling implements CDK's BundlingOptions but the image is a dummy and tryBundle always returns true. Synth therefore requires node, pnpm, and rolldown in the environment.
  • rolldown runs out-of-process via a compiled bridge (bridges/rolldown.mts) resolved from the package root. This isolates the bundler's native bits and module resolution from the CDK app process. The bridge also enforces the single-handler contract: it rejects input, output.preserveModules, output.entryFileNames, multi-entry output, and non-ESM format rather than overriding them.
  • pnpm-only by assertion, not just default. package-manager.ts throws if a non-pnpm package manager is declared.
  • nodeModules install path has real subtlety: pins exact versions via findPackageJSON, filters patchedDependencies down to the requested modules, copies patch files with path-traversal/symlink guards, and cleans staged files in a finally.
  • The bridge path lib/cdk/bridges/rolldown.mjs couples source layout to build output
  • ESM only Sam suggested simplify things and make it esm only and I extended that to the whole @skuba-lib/api because it seems odd to have half esm only 🤷

Tests

@changeset-bot

changeset-bot Bot commented Jun 15, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 19cf4a8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
skuba Minor
@skuba-lib/api Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment thread config/tsconfig.json
"noUnusedLocals": false,
"noUnusedParameters": false,
"resolveJsonModule": true,
"skipLibCheck": true,

@zbrydon zbrydon Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a result of trying to work around the optional peer deps added in this PR.

I am not sure about which approach we want to go with 🤷 :

  • make the deps non-optional (I've make rolldown a dep but the other two aren't feasible and could result in version conflicts 🤔 )
  • lazy load and skip type checking (current PR)
  • create a dedicated subpath export via skuba (Seeing what that would look like here See what a sub path import looks like #2474)
  • just import from the skuba-api (Means we have to make all the consumers of this have another dep 🫠 )

Comment thread packages/api/scripts/fix-types.sh Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zbrydon zbrydon mentioned this pull request Jun 21, 2026
@zbrydon zbrydon changed the title Add cdk nodejs function Add Cdk.NodejsFunction Jun 21, 2026
@zbrydon
zbrydon marked this pull request as ready for review June 21, 2026 08:03
@zbrydon
zbrydon requested a review from a team as a code owner June 21, 2026 08:03
@72636c 72636c self-assigned this Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants