🐛 BUG: Wrangler rebundles code that was already bundled with a custom build command.
See original GitHub issueWhat 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:
- Created a year ago
- Comments:10 (6 by maintainers)
Top GitHub Comments
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.
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