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

Refactor transformChunk to use AST #23

Merged
merged 2 commits into from
Jun 6, 2023

Conversation

joarfish
Copy link
Contributor

@joarfish joarfish commented Jun 6, 2023

This PR refactors transformChunk so it is uses SWC's parser to collect all strings in the chunk's code that contain the base string that is supposed to be replaced with e.g. window.__dynamic_base__ (or whatever the user configures).

Some Notes and clarifications

  1. SWC's parse returns an AST where all elements have spans. These, however, do not refer to character indices in the original code, but to byte positions. That is why we need to encode the chunk's code-string into a Uint8Array. To handle this conveniently, the StringAsBytes class is introduced.
  2. If the chunk's code contains invalid javascript code the plugin will fail with an exception. Not sure if this case can actually occurs, though, given that the chunk code should, of course, be valid js.
  3. The parser's target is explicitly set to esnext to be as inclusive as possible. We could think about ways to translate the vite target to the parser target. This is not straightforward, because although the parser accepts esXXX strings as target, allows for more complex ways to define the target.

…of dynamic path. Adjust tests to changes as swc will not parse invalid js-code.

Signed-off-by: Jonas Klein <[email protected]>
…roper way. Add more tests and rename them to better reflect what they target. Adjust vitest.config.ts to only include tests in dedicated test directory.

Signed-off-by: Jonas Klein <[email protected]>
Copy link
Owner

@chenxch chenxch left a comment

Choose a reason for hiding this comment

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

LGTM.

Thank you for making it more robust.

@chenxch chenxch merged commit a4645d2 into chenxch:main Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants