Skip to content
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

fix: aws-cdk-lib imports from ESM modules are broken #23846

Merged
merged 13 commits into from
Jan 26, 2023

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Jan 26, 2023

PR #23813 made imports lazy, but in the resulting code, Nodejs no longer recognizes the exports when importing aws-cdk-lib from an ESM module.

To solve this, vend two different index files: one for use by CJS imports, one for use by ESM imports.

ESM modules will still try to load the entire library, so they don't benefit from the speed boost. This is unavoidable: we tried a more complex method that forced ESM to recognize the lazy module references anyway (by tricking the backwards compatibility lexer), but ESM did not experience a speed boost, indicating that it was crawling the entire module irrespective of the submodule accessor's laziness. So, we are opting for the simpler solution of vending two index files instead.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

PR #23813 made imports lazy, but in the resulting code, Nodejs no longer
recognizes the exports when importing `aws-cdk-lib` from an ESM module.

Apply a transformation on the source that makes Nodejs detect the
exports again.
@rix0rrr rix0rrr requested a review from a team January 26, 2023 10:32
@rix0rrr rix0rrr self-assigned this Jan 26, 2023
@gitpod-io
Copy link

gitpod-io bot commented Jan 26, 2023

@aws-cdk-automation aws-cdk-automation requested a review from a team January 26, 2023 10:33
@github-actions github-actions bot added the p2 label Jan 26, 2023
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Jan 26, 2023
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

@Naumel Naumel added pr-linter/exempt-test The PR linter will not require test changes pr-linter/exempt-integ-test The PR linter will not require integ test changes labels Jan 26, 2023
@Naumel Naumel changed the title fix(aws-cdk-lib): imports from ESM modules are broken fix(cdk-lib): imports from ESM modules are broken Jan 26, 2023
@aws-cdk-automation aws-cdk-automation dismissed their stale review January 26, 2023 11:09

✅ Updated pull request passes all PRLinter validations. Dissmissing previous PRLinter review.

@Naumel Naumel added the pr/do-not-merge This PR should not be merged at this time. label Jan 26, 2023
@mrgrain mrgrain marked this pull request as draft January 26, 2023 11:24
@rix0rrr rix0rrr marked this pull request as ready for review January 26, 2023 11:34
@rix0rrr rix0rrr changed the title fix(cdk-lib): imports from ESM modules are broken fix(aws-cdk-lib): imports from ESM modules are broken Jan 26, 2023
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

@rix0rrr rix0rrr changed the title fix(aws-cdk-lib): imports from ESM modules are broken fix(core): imports from ESM modules are broken Jan 26, 2023
@aws-cdk-automation aws-cdk-automation dismissed their stale review January 26, 2023 11:37

✅ Updated pull request passes all PRLinter validations. Dissmissing previous PRLinter review.

@rix0rrr rix0rrr changed the title fix(core): imports from ESM modules are broken fix: aws-cdk-lib imports from ESM modules are broken Jan 26, 2023
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 0a24bd2
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@rix0rrr rix0rrr removed the pr/do-not-merge This PR should not be merged at this time. label Jan 26, 2023
@mergify
Copy link
Contributor

mergify bot commented Jan 26, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit cf2e498 into main Jan 26, 2023
@mergify mergify bot deleted the huijbers/esm-lazy-imports branch January 26, 2023 13:00
mrgrain pushed a commit that referenced this pull request Jan 26, 2023
PR #23813 made imports lazy, but in the resulting code, Nodejs no longer recognizes the exports when importing `aws-cdk-lib` from an ESM module.

To solve this, vend two different index files: one for use by CJS imports, one for use by ESM imports.

ESM modules will still try to load the entire library, so they don't benefit from the speed boost. This is unavoidable: we tried a more complex method that forced ESM to recognize the lazy module references anyway (by tricking the backwards compatibility lexer), but ESM did not experience a speed boost, indicating that it was crawling the entire module irrespective of the submodule accessor's laziness. So, we are opting for the simpler solution of vending two index files instead.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS. p2 pr-linter/exempt-integ-test The PR linter will not require integ test changes pr-linter/exempt-test The PR linter will not require test changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants