Advertisement
Guest User

Basic Webpack Config

a guest
Jan 20th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. var webpack = require("webpack");
  3. var CleanPlugin = require("clean-webpack-plugin");
  4. var ExtractTextPlugin = require("extract-text-webpack-plugin");
  5.  
  6. var production = process.env.NODE_ENV === "production";
  7.  
  8. var plugins = [
  9.     new ExtractTextPlugin("bundle.css"),
  10.     new webpack.optimize.CommonsChunkPlugin({
  11.         name: "main",
  12.         children: true,
  13.         minChunks: 2
  14.     })
  15. ];
  16.  
  17. if(production)
  18. {
  19.     plugins = plugins.concat([
  20.  
  21.         // Cleanup the builds/ folder before
  22.         // compiling our final assets
  23.         new CleanPlugin('builds'),
  24.  
  25.         // This plugin looks for similar chunks and files
  26.         // and merges them for better caching by the user
  27.         new webpack.optimize.DedupePlugin(),
  28.  
  29.         // This plugins optimizes chunks and modules by
  30.         // how much they are used in your app
  31.         new webpack.optimize.OccurenceOrderPlugin(),
  32.  
  33.         // This plugin prevents Webpack from creating chunks
  34.         // that would be too small to be worth loading separately
  35.         new webpack.optimize.MinChunkSizePlugin({
  36.             minChunkSize: 51200, // ~50kb
  37.         }),
  38.  
  39.         // This plugin minifies all the Javascript code of the final bundle
  40.         new webpack.optimize.UglifyJsPlugin({
  41.             mangle:   true,
  42.             compress: {
  43.                 warnings: false, // Suppress uglification warnings
  44.             },
  45.         }),
  46.  
  47.         // This plugins defines various variables that we can set to false
  48.         // in production to avoid code related to them from being compiled
  49.         // in our final bundle
  50.         new webpack.DefinePlugin({
  51.             __SERVER__:      !production,
  52.             __DEVELOPMENT__: !production,
  53.             __DEVTOOLS__:    !production,
  54.             'process.env':   {
  55.                 BABEL_ENV: JSON.stringify(process.env.NODE_ENV),
  56.             },
  57.         }),
  58.  
  59.     ]);
  60. }
  61.  
  62. module.exports = {
  63.     entry: "./src",
  64.     output: {
  65.         path: "builds",
  66.         filename: production ? "[name]-[hash].js" : "bundle.js",
  67.         chunkFilename: "[name]-[chunkhash].js",
  68.         publicPath: "builds/"
  69.     },
  70.     debug: !production,
  71.     devtool: production ? false : "eval",
  72.     devServer: {
  73.       hot: true
  74.     },
  75.     module: {
  76.         loaders: [
  77.             {
  78.                 test: /\.js$/,
  79.                 exclude: /(node_modules)/,
  80.                 loader: "babel-loader",
  81.                 query: {
  82.                     presets: ["es2015", "react"],
  83.                     plugins: ["transform-class-properties"]
  84.                 }
  85.             },
  86.             {
  87.                 test: /\.scss/,
  88.                 loader: ExtractTextPlugin.extract("style", "css!sass")
  89.             },
  90.             {
  91.                 test: /\.html/,
  92.                 loader: "html"
  93.             },
  94.             {
  95.                 test: /\.(png|gif|jpe?g|svg)$/i,
  96.                 loader: "url",
  97.                 query: {
  98.                     limit: 10000,
  99.                 }
  100.             }
  101.         ]
  102.     },
  103.     plugins: plugins
  104. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement