Skip to content

Commit 83c255a

Browse files
authored
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 #12009 (comment). 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.
1 parent 5282753 commit 83c255a

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

dev-packages/rollup-utils/bundleHelpers.mjs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,27 @@ export function makeBaseBundleConfig(options) {
8888
};
8989

9090
// used by `@sentry/aws-serverless`, when creating the lambda layer
91-
const nodeBundleConfig = {
91+
const awsLambdaBundleConfig = {
9292
output: {
9393
format: 'cjs',
9494
},
95-
plugins: [jsonPlugin, commonJSPlugin],
95+
plugins: [
96+
jsonPlugin,
97+
commonJSPlugin,
98+
// Temporary fix for the lambda layer SDK bundle.
99+
// This is necessary to apply to our lambda layer bundle because calling `new ImportInTheMiddle()` will throw an
100+
// that `ImportInTheMiddle` is not a constructor. Instead we modify the code to call `new ImportInTheMiddle.default()`
101+
// TODO: Remove this plugin once the weird import-in-the-middle exports are fixed, released and we use the respective
102+
// version in our SDKs. See: https://github.com/getsentry/sentry-javascript/issues/12009#issuecomment-2126211967
103+
{
104+
name: 'aws-serverless-lambda-layer-fix',
105+
transform: code => {
106+
if (code.includes('ImportInTheMiddle')) {
107+
return code.replaceAll(/new\s+(ImportInTheMiddle.*)\(/gm, 'new $1.default(');
108+
}
109+
},
110+
},
111+
],
96112
// Don't bundle any of Node's core modules
97113
external: builtinModules,
98114
};
@@ -124,7 +140,7 @@ export function makeBaseBundleConfig(options) {
124140
const bundleTypeConfigMap = {
125141
standalone: standAloneBundleConfig,
126142
addon: addOnBundleConfig,
127-
node: nodeBundleConfig,
143+
'aws-lambda': awsLambdaBundleConfig,
128144
'node-worker': workerBundleConfig,
129145
};
130146

packages/aws-serverless/rollup.aws.config.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default [
55
...makeBundleConfigVariants(
66
makeBaseBundleConfig({
77
// this automatically sets it to be CJS
8-
bundleType: 'node',
8+
bundleType: 'aws-lambda',
99
entrypoints: ['src/index.ts'],
1010
licenseTitle: '@sentry/aws-serverless',
1111
outputFileBase: () => 'index',
@@ -15,7 +15,6 @@ export default [
1515
sourcemap: false,
1616
},
1717
},
18-
preserveModules: false,
1918
}),
2019
// 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
2120
// plus its dependencies can be, and we might as well take up as little of that space as is necessary. We'll rename

0 commit comments

Comments
 (0)