Could esbuild treat a function returns `null` like the one returns `undefined`
Example
If I have a code like this
function a() {}
const b = a();
esbuild will minify to something like this
function n(){}const c=void 0;
Turns out esbuild knows function returns undefined, then just assign undefined to varable b.
I was wondering if esbuild could take the same action to null
Suggestion
- Input code
function a() {
return null;
}
const b = a();
- Output
function n(){return null;}const c=null;
The rationale for treating undefined this way is that empty functions are often the result of dead code removal where the original function body consists of an if statement. Can you say more about your use case? What does the original code look like? I'm hesitant to add more special cases like this without more understanding of the problem.
Sorry I don't have use case. the reason that I raised this issue just want to optimize bundle size. Well I have to admit, this might be over optimize.
Say we have a function who has treeshakable internal logic
function test() {
// this code block can be treeshaked
const a = 1;
let b;
if (a) {
b = a;
}
//
return null;
}
const c = test();
const d = test();
const e = test();
We all know that variable c, d and e are null cause test has done nothing but returns a null.
The code above equals to
const c = null;
const d = null;
const e = null;
The code size has reduced a lot.
Further more, I think the follow is the 'ultimate' way:
- If a function returns a
constantvalue, then we can 'safely' remove function call.
// input
function test() {
// this code block can be treeshaked
const a = 1;
let b;
if (a) {
b = a;
}
//
return 1;
}
const c = test();
const d = test();
const e = test;
// output
const c = 1;
const d = 1;
const e = 1;
I'm not sure if I made myself clear, or I have ignored some case.
Hi @evanw , what do you think about this?