Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

husseinelturkey's avatar

vite removes functions during compiling

I am using vite to handle browser script file but it removes functions during compiling which in original file and not found any more in compiled version.

for example it removed this function

const copyToClipboard = () => {
    const textCopy = document.querySelector('blockquote').innerText;
    navigator.clipboard.writeText(textCopy);
    document.querySelector('.tooltip-text').innerHTML = 'copied: ' + textCopy.substring(0, (textCopy + ' ').lastIndexOf(' ', 100)) + ' ...';
};

did anyone faced this before?

0 likes
3 replies
rodrigo.pedra's avatar
Level 56

Do you call this function anywhere in your code?

Vite uses tree-shaking when compiling which will get rid of unused code.

Try adding this line after this function declaration, to make it a globally available function to see if it works:

window.copyToClipboard = copyToClipboard;
1 like
rodrigo.pedra's avatar

@husseinelturkey you're welcome.

Sometimes if we use it in a blade template, or somewhere where vite compiler does not look by default it will think it is not used.

In case you don't want it exported to the global object, you can try adding it as a method to any Vue component, and check if the compiler sees it.

Have a nice day =)

1 like

Please or to participate in this conversation.