webpack.config.js 2.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
'use strict';
let webpack = require('webpack');
let path = require('path');
let exec = require('executive');
let nodeExternals = require('webpack-node-externals');
let isWebpackDevServer = process.argv.some(a => path.basename(a) === 'webpack-dev-server');
let HtmlWebpackPlugin = require('html-webpack-plugin');
let CleanWebpackPlugin = require('clean-webpack-plugin');
let isWatch = process.argv.some(a => a === '--watch');

let dist = path.join(__dirname, 'dist');
let src = path.join(__dirname, 'src');
let test = path.join(__dirname, 'test');

Justin Woo's avatar
Justin Woo committed
15
module.exports = (env) =>{
16 17 18 19 20 21 22 23 24
    let config = {
      cache: true,
      mode: 'development',
      target: "web",
      devtool: 'inline-source-map',
      devServer: {
        disableHostCheck: true,
        contentBase: dist,
        compress: true,
Alexandre Delanoë's avatar
Alexandre Delanoë committed
25
        port: 8000
26 27 28 29 30 31 32 33 34 35 36 37 38 39
      },
      output: {
        path: dist,
        filename: 'bundle.js'
      },
      module: {
        rules: [
          {test: /\.css$/,
           exclude: /(node_modules)/,
           use: ["style-loader", "css-loader"]},
          {test: /\.(png|jpg|gif|svg)$/,
           exclude: /(node_modules)/,
           use: [ "file-loader" ]},
          {test: /\.js$/,
Justin Woo's avatar
Justin Woo committed
40
           exclude: [/(node_modules)/, /(output)/],
41 42 43 44 45
           use: ["babel-loader", "source-map-loader"]}
        ]
      },
      resolve: {
        modules: [ 'node_modules' ],
Justin Woo's avatar
Justin Woo committed
46
        extensions: [ '.js']
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
      },
      plugins: [
      // TODO: can we put the checked-in assets in dist somewhere else
      // and move them into place so we can clean?
      //   new CleanWebpackPlugin(['dist']),
        new webpack.LoaderOptionsPlugin({debug: true})
      ],
      entry: path.join(src, "index.js")
    };
    switch(env) {
    case 'dev':
      console.log("Serving index.html from template src/index.html")
      config.plugins.push(new HtmlWebpackPlugin({
        template: path.join(src, "index.html")
      }));
      break;
    // TODO: testing environments - browser and headless
    // case 'browser':
    //   config.plugins.push(new HtmlWebpackPlugin({
    //     title: "Reactix",
    //     template: path.join(test, "browser.html")
    //   }));
    //   break;
    // case 'headless': break;
    default:
      console.log("unknown env: ", env);
    }
    return config;
Justin Woo's avatar
Justin Woo committed
75
  };