Webpack removing the method even when I am using it

12 Views Asked by At

I am new to webpack and I set up this simple code. Please help me with following issue

html code

<head>
    <script src="./dist/bundle.js"></script>
</head>
<body>
    <button onclick="launchAlert()">Launch alert</button>    
</body>

js code

const launchAlert = () => {
    alert("Hello world");
}

export default launchAlert;

webpack config

const path = require('path');

module.exports = {
    entry: './script.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
    },
};

Now when I click on my button it is not showing any alert. I checked my bundle.js file and it is empty. I understand this is happening due to tree shaking. But I believe it should not as I using this method and also exporting it from my js file.

Note: Just to make sure my config is correct. I added a simple console.log in script and that is showing up in bundle.js file. But the launchAlert method is getting removed due to tree shaking.

Someone please suggest what step I am doing incorrectly or is this default behavior.

0

There are 0 best solutions below