webpack.config.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. uglifyOptions: {
  25. output: {
  26. comments: false
  27. }
  28. }
  29. }),
  30. new OptimizeCSSAssetsPlugin({})
  31. ],
  32. splitChunks: {
  33. cacheGroups: {
  34. commons: {
  35. chunks: "initial",
  36. minChunks: 2,
  37. maxInitialRequests: 5,
  38. minSize: 0
  39. },
  40. vendor: {
  41. test: /node_modules/,
  42. chunks: "initial",
  43. name: "vendor",
  44. priority: 10,
  45. enforce: true
  46. }
  47. }
  48. }
  49. },
  50. module: {
  51. rules : [
  52. {
  53. test: /\.js$/,
  54. exclude: /node_modules/,
  55. use: {
  56. loader: 'babel-loader',
  57. options: {
  58. presets: ['@babel/preset-env']
  59. }
  60. }
  61. },
  62. // CSS Files
  63. {
  64. test: /\.(sa|sc|c)ss$/,
  65. use: [
  66. devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
  67. 'css-loader',
  68. //'postcss-loader',
  69. 'sass-loader',
  70. ]
  71. },
  72. {
  73. test: /\.(png|jpg|gif|svg)$/i,
  74. use: [
  75. {
  76. loader: 'url-loader',
  77. options: {
  78. limit: 8192
  79. }
  80. }
  81. ]
  82. }
  83. ]
  84. },
  85. // Plugins
  86. plugins: [
  87. new HtmlWebpackPlugin({
  88. template: './src/template.html',
  89. filename: 'index.html',
  90. hash: true
  91. }),
  92. new MiniCssExtractPlugin({
  93. filename: devMode ? '[name].css' : '[name].[hash].css',
  94. chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
  95. })
  96. ],
  97. // OPTIONAL
  98. // Reload On File Change
  99. watch: true,
  100. // Development Tools (Map Errors To Source File)
  101. devtool: 'source-map',
  102. };
  103. // Exports
  104. module.exports = config;