Incorrect treeshaking when passing detail to global CustomEvent #5122
Closed
Description
Rollup Version
3.28.1
Operating System (or Browser)
macos
Node Version (if applicable)
No response
Link To Reproduction
Expected Behaviour
When dispatching a CustomEvent like so:
const detail = {value: null}
const canceled = dispatchEvent(new CustomEvent('test', {cancelable: true, detail}))
if (!detail.value) return
The event listeners can mutate the detail
causing detail.value
to be truthy instead. Meaning the following if (!detail.value) return
we can't know for sure if detail.value
is falsy to treeshake away.
The console.log('this should not be missing')
in the repro should not be treeshaken away.
Actual Behaviour
The console.log('this should not be missing')
is treeshaken away incorrectly.
This was originally found in vitejs/vite#14298 and affects the package - https://github.com/github/text-expander-element/blob/f970a70434a53b3d1005617e2db57a31b72f2196/src/text-expander-element.ts#L123-L127