diff --git a/packages/sst/src/stacks/build.ts b/packages/sst/src/stacks/build.ts index eb31551e3..9812ac9c4 100644 --- a/packages/sst/src/stacks/build.ts +++ b/packages/sst/src/stacks/build.ts @@ -18,6 +18,24 @@ declare module "../bus.js" { } } +/** + * This function is used to remove workspace dependencies from the dependencies + * object. This is because esbuild must not mark them as external, otherwise + * the bundling will fail. + */ +function exceptWorkspaceDependencies( + deps: Record +): Record { + let result: Record = {}; + for (const key of Object.keys(deps)) { + let version = deps[key]; + if (!version.startsWith("workspace:")) { + result[key] = version; + } + } + return result; +} + export async function load(input: string, shallow?: boolean) { const parsed = path.parse(input); const root = await findAbove(input, "package.json"); @@ -48,11 +66,13 @@ export async function load(input: string, shallow?: boolean) { external: [ "aws-cdk-lib", "sst", - ...Object.keys({ + // workspace dependencies must be internal + // because they haven't been built yet + ...Object.keys(exceptWorkspaceDependencies({ ...pkg.devDependencies, ...pkg.dependencies, ...pkg.peerDependencies, - }), + })), ], plugins: [ { diff --git a/www/docs/constructs/Stack.about.md b/www/docs/constructs/Stack.about.md index f3ebd9d36..4a3302bfc 100644 --- a/www/docs/constructs/Stack.about.md +++ b/www/docs/constructs/Stack.about.md @@ -1,5 +1,7 @@ The Stack construct extends cdk.Stack. It automatically prefixes the stack names with the stage and app name to ensure that they can be deployed to multiple regions in the same AWS account. It also ensure that the stack uses the same AWS profile and region as the app. They're defined using functions that return resources that can be imported by other stacks. +When the stack construct is built in a monorepo, any packages marked as `workspace` in package.json will be included in the build. All other packages are marked as external and must be pre-built. + ## Examples ### Creating a new stack