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

nodejs compat functions build #3133

Conversation

dario-piotrowicz
Copy link
Member

Note continuation of #3100


Fixes cloudflare/next-on-pages#199

What this PR solves / how to test:

  • Fixes passing the nodejs_compat flag through to wrangler pages functions build

  • Also improve the error message so that for pages it doesn't ask to update wrangler.toml

Reviewer has performed the following, where applicable:

  • Checked for inclusion of relevant tests
  • Checked for inclusion of a relevant changeset
  • Checked for creation of associated docs updates
  • Manually pulled down the changes and spot-tested

@dario-piotrowicz dario-piotrowicz requested review from a team as code owners May 3, 2023 17:31
@changeset-bot
Copy link

changeset-bot bot commented May 3, 2023

🦋 Changeset detected

Latest commit: dc560d9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

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

@github-actions
Copy link
Contributor

github-actions bot commented May 3, 2023

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/4892376248/npm-package-wrangler-3133

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/3133/npm-package-wrangler-3133

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/4892376248/npm-package-wrangler-3133 dev path/to/script.js
Additional artifacts:
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/4892376248/npm-package-cloudflare-pages-shared-3133

Note that these links will no longer work once the GitHub Actions artifact expires.

const instructionForUser = `${
forPages
? 'Add the "nodejs_compat" compatibility flag to your Pages project'
: 'Add "node_compat = true" to your wrangler.toml file'
Copy link
Contributor

@Skye-31 Skye-31 May 3, 2023

Choose a reason for hiding this comment

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

Might be misunderstanding the changes, but should this be compatibility_flags = ["nodejs_compat"]?
node_compat is the legacy polyfill implementation

Copy link
Member Author

Choose a reason for hiding this comment

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

@Skye-31 thanks a lot for the comment, I think you're right, I wasn't really really looking into updating that error message but just adding the Pages version (as you can see the non-pages error message is totally untouched)

I can also see that node_compat is used in a handful of places still so it might just be worth it to change all that in a followup PR instead and keep this one focused only on they Pages related changes?

Also I am not totally sure if here node_compat is used for a specific reason 🤷

Anyways I am happy to change it or keep it and do the cleanup at some other point 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

I think node_compat is just used here because the error message was written before the newer nodejs_compat work. Might be worth updating in a future PR!

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 just checked the "handful of places" I mentioned above and it seems like the only place that needs fixing is the error message I changed here 😓 🤦 (sorry should have checked the search result more carefully the first time around)

I'll open a PR to fix that 🙂

@@ -222,6 +222,7 @@ export function buildRawWorker({
targetConsumer: local ? "dev" : "publish",
local,
experimentalLocal: false,
forPages: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the only place Pages calls bundleWorker?

Copy link
Member Author

Choose a reason for hiding this comment

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

no sorry, I missed a couple of places 😓, I've added the option there as well 🙏

@codecov
Copy link

codecov bot commented May 3, 2023

Codecov Report

Merging #3133 (dc560d9) into main (e351afc) will increase coverage by 0.04%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3133      +/-   ##
==========================================
+ Coverage   74.83%   74.87%   +0.04%     
==========================================
  Files         179      179              
  Lines       10852    10858       +6     
  Branches     2877     2880       +3     
==========================================
+ Hits         8121     8130       +9     
+ Misses       2731     2728       -3     
Impacted Files Coverage Δ
packages/wrangler/src/pages/build.ts 68.08% <ø> (ø)
...ckages/wrangler/src/pages/functions/buildPlugin.ts 19.35% <ø> (ø)
...ckages/wrangler/src/pages/functions/buildWorker.ts 68.53% <ø> (ø)
packages/wrangler/src/bundle.ts 93.39% <100.00%> (+0.17%) ⬆️

... and 2 files with indirect coverage changes

@dario-piotrowicz dario-piotrowicz force-pushed the dario/nodejs-compat-functions-build branch from b5bd7ff to b9cb66c Compare May 4, 2023 14:24
@dario-piotrowicz
Copy link
Member Author

Local testing, wrangler pages publish before and after:

Screenshot 2023-05-04 at 22 09 05

Screenshot 2023-05-04 at 22 09 56

@dario-piotrowicz
Copy link
Member Author

Local testing, wrangler pages dev (without the compatibility flag), no difference between before and after:
Screenshot 2023-05-04 at 23 26 29

@dario-piotrowicz
Copy link
Member Author

Local testing, wrangler pages functions build (without the compatibility flag), before and after:
Screenshot 2023-05-04 at 23 32 51

Screenshot 2023-05-04 at 23 33 49

@GregBrimble
Copy link
Member

Can you cut a new issue to track exposing this error properly in dev too, please?

Copy link
Member

@GregBrimble GregBrimble left a comment

Choose a reason for hiding this comment

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

Would be good to test, but not the end of the world.

@dario-piotrowicz
Copy link
Member Author

@GregBrimble #3147 🙂👍

@dario-piotrowicz dario-piotrowicz force-pushed the dario/nodejs-compat-functions-build branch from 78d4dfd to 52f9745 Compare May 5, 2023 09:45
@dario-piotrowicz
Copy link
Member Author

Would be good to test, but not the end of the world.

@GregBrimble I've added a check in functions-build.test.ts 🙂👍

@GregBrimble
Copy link
Member

Perfect, thank you!

@dario-piotrowicz dario-piotrowicz force-pushed the dario/nodejs-compat-functions-build branch from d51d8a4 to dc560d9 Compare May 5, 2023 10:21
Copy link
Contributor

@penalosa penalosa left a comment

Choose a reason for hiding this comment

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

Approved, after discussion in-person. I'm slightly concerned about the precedent this sets (bundling being aware of the target), but I can't think of a better solution right now.

Longer term, @dario-piotrowicz could you make a ticket to shift our NodeJS module erroring into an esbuild plugin that can be different between Pages and Workers, and injected into the build process without bundling having to be aware of the source?

@dario-piotrowicz
Copy link
Member Author

@penalosa sure thanks! 😄👍

@GregBrimble GregBrimble merged commit d078800 into cloudflare:main May 5, 2023
11 checks passed
@dario-piotrowicz dario-piotrowicz deleted the dario/nodejs-compat-functions-build branch May 5, 2023 15:14
@github-actions github-actions bot mentioned this pull request May 5, 2023
@dario-piotrowicz
Copy link
Member Author

@penalosa I've created this issue: #3155

Please have a look at it and feel free to tweak it if needed 🙂

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.

[🐛 Bug]: Nextjs v13.3.1 fresh install worker threw exception
4 participants