WebPack - Basics

# cd project
# mkdir js_modules
# cd js_modules
# npm init -f

or

# npm init -y


// Possible will work only with version 2.x
# yarn add -D webpack webpack-cli
# touch webpack.config.js


# vi webpack.config.js


const path = require('path');

const config = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'bundle.js',
  },
};

module.exports = config;


# vi package.json


"scripts": {
    "build": "webpack"
},


# vi src/index.js


const sum = require('./sum');

const total = sum(10, 5);

console.log(total);


# vi src/sum.js


const sum = (a, b) => a + b;

module.exports = sum;


<html>
<head>

</head>
<body>
    <script src="build/bundle.js"></script>
</body>
</html>


# npm run build