From 83c255a41b36d8083b09f66b9f813f09d93c75c9 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 27 May 2024 15:58:48 +0200 Subject: [PATCH] fix(aws): Ensure lambda layer uses default export from `ImportInTheMiddle` (#12233) Our lambda layer relies on one bundled Sentry SDK, which caused problems raised in #12089 and https://github.com/getsentry/sentry-javascript/issues/12009#issuecomment-2126211967. Specifically, by bundling `import-in-the-middle` code into one file, it seems like the library's way of declaring its exports conflict, causing the "ImportInTheMiddle is not a constructor" error to be thrown. While this should ideally be fixed soon in `import-in-the-middle`, for now this patch adds a small Rollup plugin to transform `new ImportInTheMiddle(...)` calls to `new ImportInTheMiddle.default(...)` to our AWS Lambda Layer bundle config. --- dev-packages/rollup-utils/bundleHelpers.mjs | 22 ++++++++++++++++--- packages/aws-serverless/rollup.aws.config.mjs | 3 +-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/dev-packages/rollup-utils/bundleHelpers.mjs b/dev-packages/rollup-utils/bundleHelpers.mjs index 3bf6b38b3457..9e507f7f65fc 100644 --- a/dev-packages/rollup-utils/bundleHelpers.mjs +++ b/dev-packages/rollup-utils/bundleHelpers.mjs @@ -88,11 +88,27 @@ export function makeBaseBundleConfig(options) { }; // used by `@sentry/aws-serverless`, when creating the lambda layer - const nodeBundleConfig = { + const awsLambdaBundleConfig = { output: { format: 'cjs', }, - plugins: [jsonPlugin, commonJSPlugin], + plugins: [ + jsonPlugin, + commonJSPlugin, + // Temporary fix for the lambda layer SDK bundle. + // This is necessary to apply to our lambda layer bundle because calling `new ImportInTheMiddle()` will throw an + // that `ImportInTheMiddle` is not a constructor. Instead we modify the code to call `new ImportInTheMiddle.default()` + // TODO: Remove this plugin once the weird import-in-the-middle exports are fixed, released and we use the respective + // version in our SDKs. See: https://github.com/getsentry/sentry-javascript/issues/12009#issuecomment-2126211967 + { + name: 'aws-serverless-lambda-layer-fix', + transform: code => { + if (code.includes('ImportInTheMiddle')) { + return code.replaceAll(/new\s+(ImportInTheMiddle.*)\(/gm, 'new $1.default('); + } + }, + }, + ], // Don't bundle any of Node's core modules external: builtinModules, }; @@ -124,7 +140,7 @@ export function makeBaseBundleConfig(options) { const bundleTypeConfigMap = { standalone: standAloneBundleConfig, addon: addOnBundleConfig, - node: nodeBundleConfig, + 'aws-lambda': awsLambdaBundleConfig, 'node-worker': workerBundleConfig, }; diff --git a/packages/aws-serverless/rollup.aws.config.mjs b/packages/aws-serverless/rollup.aws.config.mjs index 6f5cc7d581e5..6348d3b1ae74 100644 --- a/packages/aws-serverless/rollup.aws.config.mjs +++ b/packages/aws-serverless/rollup.aws.config.mjs @@ -5,7 +5,7 @@ export default [ ...makeBundleConfigVariants( makeBaseBundleConfig({ // this automatically sets it to be CJS - bundleType: 'node', + bundleType: 'aws-lambda', entrypoints: ['src/index.ts'], licenseTitle: '@sentry/aws-serverless', outputFileBase: () => 'index', @@ -15,7 +15,6 @@ export default [ sourcemap: false, }, }, - preserveModules: false, }), // We only need one copy of the SDK, and we pick the minified one because there's a cap on how big a lambda function // plus its dependencies can be, and we might as well take up as little of that space as is necessary. We'll rename