question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

🐛 BUG: Wrangler rebundles code that was already bundled with a custom build command.

See original GitHub issue

What version of Wrangler are you using?

2.0.7

What operating system are you using?

Windows

Describe the Bug

A Worker of mine uses esbuild as a bundler and generates source maps that I then use to upload to Sentry for error reporting. When I use Wrangler 2, it does invoke my custom build script, but then it also runs esbuild again over the output of my own build command. While the uploaded code functions, the source maps that my bundler generated are no longer useful.

I’m submitting this issue as a bug because I think it is unintentional that Wrangler would run esbuild again after it invokes my custom build specified with build.command.

Here is a repo with a small repro of the issue: https://github.com/evaera/w2-double-build

You’ll notice that after building, the index.mjs file is:

After build
var a,g=new Uint8Array(16);function s(){if(!a&&(a=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||typeof msCrypto<"u"&&typeof msCrypto.getRandomValues=="function"&&msCrypto.getRandomValues.bind(msCrypto),!a))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return a(g)}var p=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;function x(t){return typeof t=="string"&&p.test(t)}var m=x;var o=[];for(u=0;u<256;++u)o.push((u+256).toString(16).substr(1));var u;function v(t){var e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:0,r=(o[t[e+0]]+o[t[e+1]]+o[t[e+2]]+o[t[e+3]]+"-"+o[t[e+4]]+o[t[e+5]]+"-"+o[t[e+6]]+o[t[e+7]]+"-"+o[t[e+8]]+o[t[e+9]]+"-"+o[t[e+10]]+o[t[e+11]]+o[t[e+12]]+o[t[e+13]]+o[t[e+14]]+o[t[e+15]]).toLowerCase();if(!m(r))throw TypeError("Stringified UUID is invalid");return r}var i=v;function y(t,e,r){t=t||{};var n=t.random||(t.rng||s)();if(n[6]=n[6]&15|64,n[8]=n[8]&63|128,e){r=r||0;for(var f=0;f<16;++f)e[r+f]=n[f];return e}return i(n)}var d=y;function l(){return new Response(d())}var j={async fetch(t){return l()}};export{j as default};
//# sourceMappingURL=index.mjs.map

However, after publishing to Cloudflare via Wrangler:

After publish
// dist/index.mjs
var a;
var g = new Uint8Array(16);
function s() {
  if (!a && (a = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto) || typeof msCrypto < "u" && typeof msCrypto.getRandomValues == "function" && msCrypto.getRandomValues.bind(msCrypto), !a))
    throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
  return a(g);
}
var p = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
function x(t) {
  return typeof t == "string" && p.test(t);
}
var m = x;
var o = [];
for (u = 0; u < 256; ++u)
  o.push((u + 256).toString(16).substr(1));
var u;
function v(t) {
  var e = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0, r = (o[t[e + 0]] + o[t[e + 1]] + o[t[e + 2]] + o[t[e + 3]] + "-" + o[t[e + 4]] + o[t[e + 5]] + "-" + o[t[e + 6]] + o[t[e + 7]] + "-" + o[t[e + 8]] + o[t[e + 9]] + "-" + o[t[e + 10]] + o[t[e + 11]] + o[t[e + 12]] + o[t[e + 13]] + o[t[e + 14]] + o[t[e + 15]]).toLowerCase();
  if (!m(r))
    throw TypeError("Stringified UUID is invalid");
  return r;
}
var i = v;
function y(t, e, r) {
  t = t || {};
  var n = t.random || (t.rng || s)();
  if (n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, e) {
    r = r || 0;
    for (var f = 0; f < 16; ++f)
      e[r + f] = n[f];
    return e;
  }
  return i(n);
}
var d = y;
function l() {
  return new Response(d());
}
var j = { async fetch(t) {
  return l();
} };
export {
  j as default
};
//# sourceMappingURL=index.js.map

It is clear that Wrangler is rebundling the code an additional time.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
petebacondarwincommented, May 31, 2022

This is indeed how Wrangler 2.0.0+ works.

The bundling of the code is now built-in to Wrangler, so in most cases you do not even need to run a custom pre-build step. If you are bundling yourself in a custom pre-build step then mostly the only difference between the before and after is whitespace, which will not affect the behaviour of your code.

0reactions
petebacondarwincommented, May 31, 2022

Sorry for being vague 😄 Primarily we have this esbuild plugin, which captures and includes certain kinds of modules in the final upload:

https://github.com/cloudflare/wrangler2/blob/a8c509a200027bea212d461e8d67f7e1940cc71b/packages/wrangler/src/module-collection.ts

Read more comments on GitHub >

github_iconTop Results From Across the Web

Missing registration for Box, have you included the registration ...
I tested the exact code in my local seemingly without issue. This error usually means there is a custom component in Builder content...
Read more >
distsys-class/README.markdown at master - GitHub
The course aims to introduce software engineers to the practical basics of distributed systems, through lecture and discussion, and optional labwork.
Read more >
Paul Ford: What Is Code? | Bloomberg
What do you need to know about code to survive in a suspicious world?
Read more >
Untitled
Set featured image custom post type, Maison a vendre a quistinic 56310? ... piano solo, Code empress elsword skill build, Os milionarios do...
Read more >
Untitled
Divadlo kalich kontakt, Parliament building rome! Adu ayam kate sampai mati, Kafe radio playlist, Restaurant reviews ottawa gatineau, Custom 10in sub box, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found