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

🐛 BUG: Wrangler is injecting development code in production builds #4269

Closed
Skye-31 opened this issue Oct 24, 2023 · 1 comment · Fixed by #5960 or #5971
Closed

🐛 BUG: Wrangler is injecting development code in production builds #4269

Skye-31 opened this issue Oct 24, 2023 · 1 comment · Fixed by #5960 or #5971
Assignees
Labels
bug Something that isn't working

Comments

@Skye-31
Copy link
Contributor

Skye-31 commented Oct 24, 2023

Which Cloudflare product(s) does this pertain to?

Wrangler core

What version(s) of the tool(s) are you using?

3.14.0

What version of Node are you using?

v18.17.0

What operating system are you using?

Mac

Describe the Bug

Wrangler has started injecting some sort of "modules watch" code into production builds, when it shouldn't be. These functions are getting called for every import that is made in your worker script, which has an increase in both script size of ~5kb, and script startup time.

// wrangler-modules-watch:wrangler:modules-watch
var init_wrangler_modules_watch = __esm({
  "wrangler-modules-watch:wrangler:modules-watch"() {
    init_modules_watch_stub();
  }
});

// node_modules/.pnpm/wrangler@3.14.0/node_modules/wrangler/templates/modules-watch-stub.js
var init_modules_watch_stub = __esm({
  "node_modules/.pnpm/wrangler@3.14.0/node_modules/wrangler/templates/modules-watch-stub.js"() {
    init_wrangler_modules_watch();
  }
});

This behaviour started occurring in 3.11.0, and didn't occur in 3.10.0, so I'm almost certain it's the result of #3726, cc @petebacondarwin

Please provide a link to a minimal reproduction

Any worker that imports anything -> npx wrangler deploy --outdir dist, look at the outputted code to see these files which shouldn't be present

Please provide any relevant error logs

No response

@Skye-31 Skye-31 added the bug Something that isn't working label Oct 24, 2023
@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Oct 24, 2023
@petebacondarwin petebacondarwin self-assigned this Jun 3, 2024
@petebacondarwin petebacondarwin moved this from Untriaged to Backlog in workers-sdk Jun 3, 2024
@petebacondarwin
Copy link
Contributor

It appears this code is only added if esbuild is having to deal with a Common JS import. If all the code in a Worker is ESM then this virtual module gets stripped from the output.

petebacondarwin added a commit that referenced this issue Jun 3, 2024
When we added the ability to include additional modules in the deployed bundle of a Worker,
we inadvertently also included some boiler plate code that is only needed at development time.

This fix ensures that this code is only injected if we are running esbuild in watch mode
(e.g. `wrangler dev`) and not when building for deployment.

It is interesting to note that this boilerplate only gets included in the production code
if there is an import of CommonJS code in the Worker, which esbuild needs to convert to an
ESM import.

Fixes [#4269](#4269)
@petebacondarwin petebacondarwin moved this from Backlog to In Review in workers-sdk Jun 3, 2024
petebacondarwin added a commit that referenced this issue Jun 4, 2024
…5960)

When we added the ability to include additional modules in the deployed bundle of a Worker,
we inadvertently also included some boiler plate code that is only needed at development time.

This fix ensures that this code is only injected if we are running esbuild in watch mode
(e.g. `wrangler dev`) and not when building for deployment.

It is interesting to note that this boilerplate only gets included in the production code
if there is an import of CommonJS code in the Worker, which esbuild needs to convert to an
ESM import.

Fixes [#4269](#4269)
@github-project-automation github-project-automation bot moved this from In Review to Done in workers-sdk Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that isn't working
Projects
Archived in project
2 participants