Skip to content

fix(aws): Ensure lambda layer uses default export from ImportInTheMiddle #12233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions dev-packages/rollup-utils/bundleHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -124,7 +140,7 @@ export function makeBaseBundleConfig(options) {
const bundleTypeConfigMap = {
standalone: standAloneBundleConfig,
addon: addOnBundleConfig,
node: nodeBundleConfig,
Copy link
Member

Choose a reason for hiding this comment

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

Is node not used anywhere else? Nice!

Copy link
Member Author

@Lms24 Lms24 May 27, 2024

Choose a reason for hiding this comment

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

Nope, the only thing we bundled with this config on the node side is the aws lambda layer bundle

'aws-lambda': awsLambdaBundleConfig,
'node-worker': workerBundleConfig,
};

Expand Down
3 changes: 1 addition & 2 deletions packages/aws-serverless/rollup.aws.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down
Loading