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

replace regex replacemend logic in iifefyFunctionFile and handle _ENTRIES declarations #846

Closed
wants to merge 1 commit into from

Conversation

dario-piotrowicz
Copy link
Member

@dario-piotrowicz dario-piotrowicz commented Aug 2, 2024

This PR replaces the regex replacements introduced in the iifefyFunctionFile function in #836 with with more proper AST code modding

As part of this I also noticed that we do not want to update _ENTRIES identifiers if they are actually declared in the file (as it happens in older Vercel CLI releases) so I've addressed this too.

Note

This PR does not include a changeset since the changes in #836 have not yet been released and this PR simply improves that solution

Copy link

changeset-bot bot commented Aug 2, 2024

⚠️ No Changeset found

Latest commit: 895cd66

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dario-piotrowicz dario-piotrowicz marked this pull request as draft August 2, 2024 18:02
Copy link
Contributor

github-actions bot commented Aug 2, 2024

🧪 Prereleases are available for testing 🧪

@cloudflare/next-on-pages

You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/next-on-pages/runs/10223327767/npm-package-next-on-pages-846

@cloudflare/eslint-plugin-next-on-pages

You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/next-on-pages/runs/10223327767/npm-package-eslint-plugin-next-on-pages-846

…ENTRIES` declarations

in `iifefyFunctionFile` replace the regex replacement code that was present to prepare the
file contents to be embedded in the iife with more proper AST code modding

as part of this change make also sure that `_ENTRIES` identifiers are not modified if they
were actually declared inside the function file (as it happens in older Vercel CLI releases)
* @param fileContents the original file contents
* @returns the file contents updated and ready to be embedded into an iife
*/
function prepareFunctionFileContentForIifeConversion(fileContents: string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

i would be curious to know what the impact on build-time is with this change - are you able to get some numbers for that?

Copy link
Contributor

Choose a reason for hiding this comment

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

also, we already use acorn for parsing asts - would that help here as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

also, we already use acorn for parsing asts - would that help here as well?

I don't think so since there all we care about is getting (and analyzing) the AST while here we want to actually traverse and manipulate it

Copy link
Member Author

Choose a reason for hiding this comment

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

i would be curious to know what the impact on build-time is with this change - are you able to get some numbers for that?

I should be able to get some before vs after numbers if you want 🤔

In any case this runs only on function files (not all js files) so... I hope it's not to costly 😓
Even if this was slower I think that a slower but more correct and robust solution would be preferable than a quick but hacky and brittle one, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

i would be curious to know what the impact on build-time is with this change - are you able to get some numbers for that?

Oh wow! it's actually pretty bad! 😖 (thanks so much for the useful comment! I didn't imagine the impact could be this big! ❤️)

I've created a brand new Next.js app with C3 and run the build script, with the latest next-on-pages release it takes around 0.27s but with this PR's prerelease it takes around 2.95s...

I wanted to have a look at a more complex app so I gave the app-playground-edge app a try as well, in that case the build time goes from around 1.75s to around 4.35s... so a bit better but still pretty bad 😕

So maybe it might be best to stick with regex replacement for now... 😓 😖

@vicb what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought that might happen 😅. It was a big problem with how we originally did things before the big CLI rewrite. Back then, I changed it from two passes over the AST to one pass which helped speed things up quite a bit. So yeah... Umm... It'll slow it down a lot.

I wonder if it might be possible to try and do some of this during the original parsing of the AST? I'm not sure how much would need changing to make it work, but it'd be an interesting idea to look into

Copy link
Member Author

@dario-piotrowicz dario-piotrowicz Aug 5, 2024

Choose a reason for hiding this comment

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

I thought that might happen 😅. It was a big problem with how we originally did things before the big CLI rewrite. Back then, I changed it from two passes over the AST to one pass which helped speed things up quite a bit. So yeah... Umm... It'll slow it down a lot.

I see... 😓 I totally forgot that we had two AST passes 😅 (but yeah I do remember that the refactoring sped up things quite a bit)

I wonder if it might be possible to try and do some of this during the original parsing of the AST? I'm not sure how much would need changing to make it work, but it'd be an interesting idea to look into

That's an interesting idea... maybe? 😕 the issue is that again, there we only analyze the AST and don't have means for actually updating it... so if we were to do something there I think it would likely still be a regex replacement (maybe after we've identified relevant code snippets?), so it feels like no matter what we either take the perf hit or do rely on regex replacements 😕

@vicb
Copy link
Collaborator

vicb commented Aug 5, 2024

I see no reason to switch to AST for now as your experiment shows it's much slower.

But it sounds like the handle _ENTRIES declarations should be fixed.

Thanks!

@dario-piotrowicz
Copy link
Member Author

Closing in favour of #852

@dario-piotrowicz dario-piotrowicz deleted the avoid-iifefy-regex-replace branch August 5, 2024 12:18
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.

3 participants