I always wonder how to setup webpack to build a frontend react app from scratch? At the first glance, I felt so terrible about its document. It looks so big !!! Lots of things to do. But anything can be learned by step by step method. So I divided that progress into sub steps that I can follow.

First step is really simple. Just compile and copy code in src to

public/build.

// webpack configuration
import path from 'path';
import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';

const sourcePath = path.resolve(process.cwd(), 'src/index.js');
const buildPath = path.resolve(process.cwd(), 'public/build');

export default {
  devtool: 'cheap-module-source-map',
  entry: {
    app: [sourcePath]
  },
  target: 'web',
  output: {
    path: buildPath,
    filename: '[name].js'
  },
  module: {
    rules: [
      { test: /\.js?$/, exclude: /node_modules/, loaders: ['babel-loader'] }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: 'public/template.html'
    })
  ]
};

Here, we will compile codes at sourcePath and copy them to buildPath. The webpack will compile code bases on module/rules.

What is rules? Rules is array of rule that webpack will apply to our codebase. In the above code, the rule is

 { test: /\.js?$/, exclude: /node_modules/, loaders: ['babel-loader'] }

It uses regular expression to catch all files js and apply loader (babel-loader) to compile.

So what is loader? Loaders are transformations that are applied on the source code of a module. They allow you to pre-process files as you import or “load” them

So that’s all for first simple webpack configuration !