Webpack

When working with modules (i.e. importing modules into a .js file) the browser doesn't know how to handle that code (i.e. something like import {camelCase } from 'lodash'). This is where webpack comes in.

Initiate npm

  1. npm init -y
    • yarn doesn't seem to work with this
  2. lodash
  1. to install to your frontend package
    • npm install --save-dev webpack webpack-cli

to run

  1. add a script in package.json
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build":"webpack"
  },
  1. run from terminal npm run build

    • this will create /dist in the root of project.
  2. edit index.html file

    • previously it was pointing to <script src="../src/index.js"></script>
    • now <script src="../dist/main.js"></script>
  3. now you can open a browser and load the index.html file and see the imports from the index.js file work in the index.html.

configuration of webpack

webpack.config.js