I have two pieces of code that look almost identical, but when I build them with webpack, I notice a key difference in the resulting bundles.
In one case, the module is included in the bundle, while in the other case, it is excluded.
I expected that webpack would include all modules with potential side effects in the bundle, regardless of any conditional logic.
However, it seems this isn’t always the case. Could anyone explain why this difference occurs?
- Case where the module is included in the bundle
const flag = false;
if (flag) {
require("./myModule");
}
- Case where the module is excluded from the bundle
if (false) {
require("./myModule");
}