Q:

webpack compile javascript for browser

Thanks for your attention!
I have built a javascript file from my source code using webpack, I was mean to use it in brower, but it seems not working for me, so I post this for help
here is my project tree
project
├── dist
│   └── my-dist.js
├── index.js
├── lib
│   └── my-source.js
└── webpack.config.js

here is my-source.js
'use strict'

const somepackage = require("somepackage")

module.exports = MyPkg

function MyPkg(param) {
    this.myprop = param
}

MyPkg.prototype.Afunc = function () {
    consolg.log("hello from A ", this.myprop)
}

MyPkg.prototype.Bfunc = function (param) {
    // B do some thing
}

here is my index.js
exports = module.exports = require('./lib/MyPkg');

here is my webpack.config.js
const path = require('path')
module.exports = {
    entry: './index.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'my-dist.js'
    },
    module: {
        rules: [{
            test: /\.js$/,
            exclude: /(node_modules|bower_components)/,
            use: {
                loader: 'babel-loader',
                options: {
                    presets: ['babel-preset-env']
                }
            }
        }]
    }
}

then i run "webpack" to build the file, it seems ok, and the "my-dist.js" file was created, so i try to use it like this:
<html>
<head>
</head>
<body>
    <div>
        <button onclick="func()">click here</button>
    </div>
</body>
<script src="dist/my-dist.js"></script>
<script>
    var pkg = new MyPkg('haha')
    function func() {
        pkg.Afunc()
    }
</script>
</html>

but it throw error like "Uncaught ReferenceError: MyPkg is not defined", and i really don't know how to fix it, please give my a hand, thank you guys~

A:

Edit x2:
Here's a GitHub Repo with further explanations: 
https://github.com/superjose/webpack-simple-example

If I recall correctly, I had this issue in the past. Webpack the JavaScript from your entry file so that no other file or code has direct access to it. 
Therefore, in your index.html this will not work
<script>
    var pkg = new MyPkg('haha')
    function func() {
        pkg.Afunc()
    }
</script>

What you need to do is to use EventListeners so you can target the elements.
So you have this button: (Add a class or an id to better identify it)
 <button onclick="func()" id="js-pkg">click here</button>

Then, inside index.js:
document.addEventListener('DOMContentLoaded', () => {
   document.getElementById('js-pkg').addEventListener(MyPkg);
  // This also works:
  // document.querySelector('#js-pkg').addEventListener(MyPkg);
});

Note:
We add the 'DOMContentLoaded' event so we wait for the DOM to be loaded before making any operations. Not doing so, it may result in the button to be not defined, since it may not be parsed or rendered by the browser's engine
Edit: More detailed approach below
Suppose you have the following structure:
----index.html
----index.js
----Email/
---------email.js
----Validator/
---------validator.js

Whereas index.js is your main entry file (Where Webpack loads the JavaScript). 
Email.js contents:
// Ficticious/non-existent npm package
import email from 'send-email'
// Custom and ficticious email class that will send emails
export class Email {
   sendEmail(subject, message) { 
       // You would send the email somehow.
       email.send('contact@support.com' this.subject, message); 
   }
}

Validator.js contents:
// Checks if the value is undefined or null
module.exports = function (value) {
    return !!value
}

In your index.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <input type="name" id="js-name" placeholder="message" />
    <textarea id="js-msg" placeholder="Write us a message"></textarea>
    <button id="js-send">Send us a message!!</button>
    <!-- Awful Real-life example -->
    <button id="js-validate">Validate Text!</button>
    <script src="index.js"></script>
</body>
</html>

In index.js (Webpack's main file):
// We import the email class that we want to use
import { Email } from './Email/email.js'
// We also add validator
import validator from './Validator/validator.js'

/**
 * Now the actual magic. Webpack scopes the variable and function names, changing
 * the normal values, in order (I could be wrong with this) to avoid collisions with other functions, and variables. 
 * 
 * Nonetheless, JavaScript allow us to programatically attach those functions to the element via event listeners.
 * https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
 * This means that <input type="button" onclick="func()" /> the onclick() event is the same
 * as element.addEventListener('click', func); Where element is the <input type="button" found via a document.getElementById/querySelector/querySelectorAll/getElementsByTagName/etc

* Therefore, even if Webpack scopes the variable and function names, we would be able to attach it since it resides inside Webpack's code. 

* The first thing we do is to add a "DOMContentLoaded" event. This is similar to jQuery's $(document).ready(function() { }); We need for the HTML to be loaded in order for us to add the event listener. You didn't have that problem in the past because you would add it directly to the HTML.
**/

document.addEventListener('DOMContentLoaded', () => {
   let sendEmail = document.getElementById('js-send');
   let name = document.getElementById('js-name'); 
   let email = new Email(); 
   let validateBtn = document.getElementById('js-validate');
   // We pass the functions without parenthesis. 
   // Note that validator is a function, while Email is a class.
   // Email needs to be instantiated first, and then we assign 
   // the method that it calls. 
   validateBtn.addEventListener('click', validator);
   sendEmail.addEventListener('click', email.sendEmail);

});

