-
Notifications
You must be signed in to change notification settings - Fork 551
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
Disable format conversion when detecting format #2766
Conversation
Wrangler automatically detects whether your code is a `modules` or `service-worker` format Worker based on the presence of a `default` `export`. This check currently works by building your entrypoint with `esbuild` and looking at the output metafile. Previously, we were passing `format: "esm"` to `esbuild` when performing this check, which enables *format conversion*. This may introduce `export default` into the built code, even if it wasn't there to start with, resulting in incorrect format detections. This change removes `format: "esm"` which disables format conversion when bundling is disabled: https://esbuild.github.io/api/#format. We may want to use a package like `es-module-lexer` in the future, but this issue is affecting users, and this is probably the simplest fix. Closes #1668 Closes #2396 Ref #2737
🦋 Changeset detectedLatest commit: 0271af5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
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/4224956213/npm-package-wrangler-2766 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/2766/npm-package-wrangler-2766 Or you can use npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/4224956213/npm-package-wrangler-2766 dev path/to/script.js Additional artifacts:npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/4224956213/npm-package-cloudflare-pages-shared-2766 Note that these links will no longer work once the GitHub Actions artifact expires. |
Codecov Report
@@ Coverage Diff @@
## main #2766 +/- ##
==========================================
+ Coverage 68.12% 74.01% +5.89%
==========================================
Files 166 166
Lines 10161 10161
Branches 2705 2705
==========================================
+ Hits 6922 7521 +599
+ Misses 3239 2640 -599
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the situation if the file is supposed to be a service-worker format but contains an import
statement?
This shouldn't matter, as the detection is only looking for |
What this PR solves
Wrangler automatically detects whether your code is a
modules
orservice-worker
format Worker based on the presence of adefault
export
. This check currently works by building your entrypoint withesbuild
and looking at the output metafile.Previously, we were passing
format: "esm"
toesbuild
when performing this check, which enables format conversion. This may introduceexport default
into the built code, even if it wasn't there to start with, resulting in incorrect format detections.This change removes
format: "esm"
which disables format conversion when bundling is disabled: https://esbuild.github.io/api/#format.We may want to use a package like
es-module-lexer
in the future, but this issue is affecting users, and this is probably the simplest fix.How to test:
Run
wrangler dev worker.js
with the following:Before this PR, you'd see
When using module syntax, the 'fetch' event handler should be declared as an exported function on the root module as opposed to using the global addEventListener().
in the console, indicating Wrangler thought this was amodules
-format Worker, and uploaded it as such. Attempting to access this in the browser results inUncaught Error: Handler does not export a fetch() function.
.After this PR, the Worker should be deployed as a
service-worker
, and attempting to access it in the browser will result in a 200 OK response.Associated docs issues/PR:
N/A
Author has included the following, where applicable:
Reviewer has performed the following, where applicable:
Fixes #1668
Supersedes #2396
Supersedes #2737