Wrangler compiling in an incompatible way?

As mentioned here https://discord.com/channels/595317990191398933/846453104382836766/1079084350437589063 I'm using wrangler to publish a worker, but there seems to be a bug. In my code index.ts I export default like so
export default { email };
export default { email };
When using wrangler publish it compiles to:
var src_default = { email };
export {
src_default as default
};
var src_default = { email };
export {
src_default as default
};
And in the dashboard when I try to test it it tells me
Email Trigger not available to this workers
Email Trigger not available to this workers
Though when I change it manually to the former again:
export default { email };
export default { email };
It seems to work and goes to the next error about a constructor. Changing the tsconfig.json file doesn't seem to make any difference at all. Looks like a bug to me. Any ideas of what I can try?
7 Replies
Webber
WebberOP2y ago
Feel free to explore the code and configuration here: https://github.com/webbertakken/email-worker
GitHub
GitHub - webbertakken/email-worker: Lightweight tool to process ema...
Lightweight tool to process emails directly from Cloudflare - GitHub - webbertakken/email-worker: Lightweight tool to process emails directly from Cloudflare
Webber
WebberOP2y ago
I can reproduce it using Wrangler 2.7.1, 2.11.0 and 2.11.1.
Webber
WebberOP2y ago
Even compiling a console.log only shows the same behaviour:
Webber
WebberOP2y ago
If only it compiled to
async function email(message, env, ctx) {
console.log(message.from);
}

export default { email }
async function email(message, env, ctx) {
console.log(message.from);
}

export default { email }
It would simply work (note the default part). Also tried using this template https://github.com/cloudflare/miniflare-typescript-esbuild-jest which uses ESBuild. I've added a minimal method like so:
// @ts-ignore
import PostalMime from 'postal-mime';

...handleRequest...

export async function email(message: any, env: any, ctx: any): Promise<void> {
console.log(message.from);

const rawBody = await new Response(message.raw).arrayBuffer();
const parser = await new PostalMime();
const email = await parser.parse(rawBody);

console.log(email.from);
}

const worker = { fetch: handleRequest, email };
export default worker;
// @ts-ignore
import PostalMime from 'postal-mime';

...handleRequest...

export async function email(message: any, env: any, ctx: any): Promise<void> {
console.log(message.from);

const rawBody = await new Response(message.raw).arrayBuffer();
const parser = await new PostalMime();
const email = await parser.parse(rawBody);

console.log(email.from);
}

const worker = { fetch: handleRequest, email };
export default worker;
Which has exactly the same result. Definitely looks like something is broken on CF side I've also tried Vercel NCC, which uses Webpack. Same result.
sakuta
sakuta2y ago
i'm not familiar with email workers and stuff, but writing
export { variable as default }
export { variable as default }
instead of
export default variable
export default variable
should make no difference as it's exactly the same, just with a little different syntax.
Webber
WebberOP2y ago
Agreed. The operative word here being "should". That's why I mentioned it's likely a bug.
Webber
WebberOP2y ago
TL;DR: After a lot of debugging I've managed to get a fully working email worker. https://github.com/webbertakken/email-worker Note that even though the worker is fully functional, the dashboard view of email workers is still bugged. The only thing that solves that issue is to manually change from
var src_default = { email };
export {
src_default as default,
email
};
var src_default = { email };
export {
src_default as default,
email
};
to
export default { email }
export default { email }
I'm really hoping this helps CF easily solve this issue. Or at least helps other people facing the same problem wasting a lot less time by reading this.
GitHub
GitHub - webbertakken/email-worker: Lightweight tool to process ema...
Lightweight tool to process emails directly from Cloudflare - GitHub - webbertakken/email-worker: Lightweight tool to process emails directly from Cloudflare
Want results from more Discord servers?
Add your server