webpack.config.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Imports
  2. const path = require('path');
  3. const HtmlWebpackPlugin = require('html-webpack-plugin')
  4. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  5. const BabelRegister = require("babel-register");
  6. const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
  7. const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
  8. const devMode = process.env.NODE_ENV !== 'production'
  9. // Webpack Configuration
  10. const config = {
  11. // Entry
  12. entry: './src/app.js',
  13. // Output
  14. output: {
  15. path: path.resolve(__dirname, './dist'),
  16. filename: '[name].[chunkhash].js'
  17. },
  18. optimization: {
  19. minimizer: [
  20. new UglifyJsPlugin({
  21. cache: true,
  22. parallel: true,
  23. sourceMap: true // set to true if you want JS source maps
  24. }),
  25. new OptimizeCSSAssetsPlugin({})
  26. ],
  27. splitChunks: {
  28. cacheGroups: {
  29. commons: {
  30. chunks: "initial",
  31. minChunks: 2,
  32. maxInitialRequests: 5,
  33. minSize: 0
  34. },
  35. vendor: {
  36. test: /node_modules/,
  37. chunks: "initial",
  38. name: "vendor",
  39. priority: 10,
  40. enforce: true
  41. }
  42. }
  43. }
  44. },
  45. module: {
  46. rules : [
  47. {
  48. test: /\.js$/,
  49. exclude: /node_modules/,
  50. use: ['babel-loader']
  51. },
  52. // CSS Files
  53. {
  54. test: /\.(sa|sc|c)ss$/,
  55. use: [
  56. devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
  57. 'css-loader',
  58. //'postcss-loader',
  59. 'sass-loader',
  60. ]
  61. }
  62. ]
  63. },
  64. // Plugins
  65. plugins: [
  66. new HtmlWebpackPlugin({
  67. template: './src/template.html',
  68. filename: 'index.html',
  69. hash: true
  70. }),
  71. new MiniCssExtractPlugin({
  72. filename: devMode ? '[name].css' : '[name].[hash].css',
  73. chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
  74. })
  75. ],
  76. // OPTIONAL
  77. // Reload On File Change
  78. watch: true,
  79. // Development Tools (Map Errors To Source File)
  80. devtool: 'source-map',
  81. };
  82. // Exports
  83. module.exports = config;