Skip to content

fix: js library default export to a unique name instead of default key in self#36483

Merged
AmanAgarwal041 merged 6 commits intoreleasefrom
fix/js-library-default-export
Sep 24, 2024
Merged

fix: js library default export to a unique name instead of default key in self#36483
AmanAgarwal041 merged 6 commits intoreleasefrom
fix/js-library-default-export

Conversation

@AmanAgarwal041
Copy link
Contributor

@AmanAgarwal041 AmanAgarwal041 commented Sep 23, 2024

Description

Earlier since some of the libraries one of which is https://cdn.jsdelivr.net/npm/jspdf-autotable@3.5.28/dist/jspdf.plugin.autotable.js wasn't exporting the functionality to some key, instead it was exporting as default. Hence, after the import of the library the functionality was being set on the default key, which on refresh or on reintializing the application in the store got changed and the inner functions got attached to global object.

This PR fixes this issue and checks if the last accessor is the default then it maps all the functionality to a unique name generated from the library url and moves everything to the generated name as done by other libraries.

Fixes #20572
or
Fixes Issue URL

Warning

If no issue exists, please create an issue first, and check with the maintainers if the issue is valid.

Automation

/ok-to-test tags="@tag.JS"

🔍 Cypress test results

Tip

🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/11011513239
Commit: b4c0767
Cypress dashboard.
Tags: @tag.JS
Spec:


Tue, 24 Sep 2024 10:40:20 UTC

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Improved handling of library installations, enhancing the management of library exports and default exports.
    • Added a new test case to verify the installation of libraries with default exports.
  • Bug Fixes

    • Resolved issues related to key differentiation during library installation.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 23, 2024

Walkthrough

The changes in the installLibrary function enhance the management of library exports by refining how keys are handled after a library installation. The code now distinguishes between keys before and after installation, allowing for better handling of default exports. This includes generating unique names for accessors and managing the global scope more effectively.

Changes

File Change Summary
app/client/src/workers/Evaluation/handlers/jsLibrary.ts Modified installLibrary function to improve handling of library keys and exports.
app/client/src/workers/Evaluation/handlers/tests/jsLibrary.test.ts Enhanced mock implementation of self.importScripts and added a new test case for default export installation.

Assessment against linked issues

Objective Addressed Explanation
Library namespace should not change randomly after installation (20572)

Poem

In the realm of code, a change so bright,
Keys now dance in the soft moonlight.
Exports managed with care and grace,
Default names find their rightful place.
A library's journey, now smooth and clear,
Cheers to the changes, let’s give a cheer! 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added Bug Something isn't working Community Reported issues reported by community members Custom JS Libraries Issues related to adding custom JS library Javascript Product Issues related to users writing javascript in appsmith Medium Issues that frustrate users due to poor UX Query & JS Pod Issues related to the query & JS Pod labels Sep 23, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Outside diff range and nitpick comments (1)
app/client/src/workers/Evaluation/handlers/jsLibrary.ts (1)

151-155: Use forEach instead of map for side effects.

Since we're performing actions for their side effects and not utilizing the returned array from map, it's better to use forEach.

Apply this diff to make the change:

- differentiatingKeys.map((key) => {
+ differentiatingKeys.forEach((key) => {
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between cd3472a and f4e37cb.

Files selected for processing (1)
  • app/client/src/workers/Evaluation/handlers/jsLibrary.ts (1 hunks)
Additional context used
Biome
app/client/src/workers/Evaluation/handlers/jsLibrary.ts

[error] 149-149: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

@AmanAgarwal041
Copy link
Contributor Author

/build-deploy-preview skip-tests=true

@github-actions
Copy link

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/10995172142.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 36483.
recreate: .

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
app/client/src/workers/Evaluation/handlers/__tests__/jsLibrary.test.ts (1)

164-186: Excellent test case, but let's add a gold star to it!

Well done on crafting this test case, students! It's a wonderful addition to our test suite. Let's review what you've done right:

  1. You're testing the installation of the jspdf-autotable library, which is the focus of our lesson.
  2. You're verifying that importScripts is called, ensuring our mock is utilized.
  3. You're checking the structure of the installation response, including the accessor and definitions.

However, to earn that extra credit, let's make one small improvement:

Consider adding an assertion to check if the Cell function is available on the installed library. This will ensure that our flattening process for default exports is working correctly. Here's how you can do it:

expect(self.jspdf_plugin_autotable_js.Cell).toBeDefined();

Add this assertion just before the end of the test case. This will give us full confidence that our library is installed and accessible as expected.

Keep up the great work, class!

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f4e37cb and ceffc89.

Files selected for processing (2)
  • app/client/src/workers/Evaluation/handlers/tests/jsLibrary.test.ts (2 hunks)
  • app/client/src/workers/Evaluation/handlers/jsLibrary.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/client/src/workers/Evaluation/handlers/jsLibrary.ts
Additional comments not posted (2)
app/client/src/workers/Evaluation/handlers/__tests__/jsLibrary.test.ts (2)

14-24: Class, pay attention to this excellent mock implementation!

Well done, students! This mock implementation of importScripts is a shining example of how to prepare our test environment. Let's break it down:

  1. We're checking if the URL includes "jspdf-autotable". This is crucial for our specific test case.
  2. We're setting up a mock structure that mimics the real library's behavior.
  3. We're attaching both a default export and a Cell function to the global self object.

This change is vital for our lesson on testing library installations, especially those with default exports. It allows us to simulate the behavior of the jspdf-autotable library accurately.

Remember, class: proper mocking is key to effective unit testing!


Line range hint 1-186: Class, let's summarize today's lesson!

Excellent work on this test file, students! You've made significant improvements that directly address our lesson objectives. Let's recap what we've learned:

  1. We've enhanced our mock implementation to better simulate libraries with default exports, particularly the jspdf-autotable library.
  2. We've added a new test case that verifies the correct installation and accessibility of such libraries.

These changes are crucial in ensuring that our library installation process works correctly, especially for libraries that use default exports. This will help prevent the namespace issues we've been struggling with.

For homework, consider adding more test cases to cover different scenarios, such as:

  1. Installing multiple versions of the same library
  2. Handling conflicts with existing global objects
  3. Testing error cases, like network failures during installation

Remember, thorough testing is the key to robust code! Keep up the great work, and don't forget to submit your improved tests for extra credit!

@github-actions
Copy link

Deploy-Preview-URL: https://ce-36483.dp.appsmith.com

@AmanAgarwal041
Copy link
Contributor Author

/build-deploy-preview skip-tests=true

@AmanAgarwal041 AmanAgarwal041 added the ok-to-test Required label for CI label Sep 23, 2024
@github-actions
Copy link

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/10996267004.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 36483.
recreate: .

@github-actions
Copy link

Deploy-Preview-URL: https://ce-36483.dp.appsmith.com

@rishabhrathod01
Copy link
Contributor

@AmanAgarwal041 I tried to reinstall the library after uninstalling it and I was able to reproduce the issue. I demoed the same in the recording below.
https://github.com/user-attachments/assets/b6722a82-33bd-4cd4-b9f1-e218935e7010

Could you handle this case and add a test for it?

Copy link
Contributor

@rishabhrathod01 rishabhrathod01 left a comment

Choose a reason for hiding this comment

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

@AmanAgarwal041
Copy link
Contributor Author

@rishabhrathod01 Addressed the issue. It was acting weird because of the undefined being set to self property instead of just getting delete and hence on second time there were no differentiating keys in the before and after self.

@AmanAgarwal041
Copy link
Contributor Author

/build-deploy-preview skip-tests=true

@github-actions
Copy link

Deploying Your Preview: https://github.com/appsmithorg/appsmith/actions/runs/11011543756.
Workflow: On demand build Docker image and deploy preview.
skip-tests: true.
env: ``.
PR: 36483.
recreate: .

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Outside diff range and nitpick comments (2)
app/client/src/workers/Evaluation/handlers/jsLibrary.ts (2)

136-163: Excellent work on handling the "default" export, but let's improve our performance!

Your approach to handling the "default" export and other differentiating keys is spot on! It effectively addresses the issue mentioned in the PR objectives. However, we can make a small improvement:

On line 150, instead of using the delete operator, which can impact performance, let's set the property to undefined. This is a more efficient way to remove a property from an object.

Apply this change:

- delete self["default"];
+ self["default"] = undefined;

Keep up the great work, and always remember to optimize for performance!

Tools
Biome

[error] 150-150: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)


136-163: Let's add a helpful comment to explain this code block.

To help your fellow students (I mean, developers) understand the purpose of this code block, it would be beneficial to add a comment explaining what it does. Here's a suggestion:

// Handle the case where the library has a default export
// If present, map it to a unique name and reassign other exports
// If not present, simply add all new keys to the accessors array

Adding such comments makes your code more readable and easier to maintain. It's like leaving good notes for the next person who reads your code!

Tools
Biome

[error] 150-150: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2cc46ca and b4c0767.

Files selected for processing (1)
  • app/client/src/workers/Evaluation/handlers/jsLibrary.ts (1 hunks)
Additional context used
Biome
app/client/src/workers/Evaluation/handlers/jsLibrary.ts

[error] 150-150: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

Additional comments not posted (1)
app/client/src/workers/Evaluation/handlers/jsLibrary.ts (1)

131-133: Well done, class! This is a great way to identify new keys.

The use of the difference function to compute differentiatingKeys is an excellent approach. It efficiently identifies the new keys added by the library installation. Keep up the good work!

@github-actions
Copy link

Deploy-Preview-URL: https://ce-36483.dp.appsmith.com

Copy link
Contributor

@rishabhrathod01 rishabhrathod01 left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working Community Reported issues reported by community members Custom JS Libraries Issues related to adding custom JS library Javascript Product Issues related to users writing javascript in appsmith Medium Issues that frustrate users due to poor UX ok-to-test Required label for CI Query & JS Pod Issues related to the query & JS Pod

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Library namespace randomly changes to a different name and stops working

2 participants