webpack.config.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. const path = require("path");
  2. const TerserJSPlugin = require('terser-webpack-plugin');
  3. const HtmlPlugin = require("html-webpack-plugin");
  4. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  5. const { CleanWebpackPlugin } = require("clean-webpack-plugin");
  6. const OptimizeCSSAssetsPlugin = require('css-minimizer-webpack-plugin');
  7. const webpack = require("webpack");
  8. const ASSET_INLINE_LIMIT = 100000;
  9. const TERSER_PARALLEL_WORKERS = 4;
  10. const PROJECT_NAME_PATH_OFFSET = -1;
  11. /**
  12. * Gets the project name from the webpack configuration directory.
  13. *
  14. * @returns {string} The project directory name.
  15. * @sideEffects None.
  16. */
  17. function getCurrentProjectName() {
  18. const pathParts = __dirname.split(path.sep);
  19. return pathParts[pathParts.length + PROJECT_NAME_PATH_OFFSET];
  20. }
  21. module.exports = {
  22. stats: {
  23. errorDetails: true,
  24. },
  25. plugins: [
  26. new CleanWebpackPlugin(),
  27. new HtmlPlugin(
  28. {
  29. template: "./ui/frontend/errors_template.tpl",
  30. filename: path.resolve(__dirname, './ui/frontend/errors.tpl'),
  31. chunks: ["frontend"]
  32. }
  33. ),
  34. new HtmlPlugin(
  35. {
  36. template: "./ui/frontend/masterpage_template.tpl",
  37. filename: path.resolve(__dirname, './ui/frontend/masterpage.tpl'),
  38. chunks: ["frontend"]
  39. }
  40. ),
  41. new MiniCssExtractPlugin({ filename: "styles.[contenthash].css" }),
  42. new webpack.ProvidePlugin({
  43. $: 'jquery',
  44. jQuery: 'jquery',
  45. "window.jQuery": 'jquery'
  46. })
  47. ],
  48. module: {
  49. rules: [
  50. {
  51. test: /\.(css|scss)$/,
  52. use: [
  53. { loader: MiniCssExtractPlugin.loader },
  54. { loader: "css-loader" },
  55. { loader: "sass-loader" }
  56. ]
  57. },
  58. // Import various assets
  59. {
  60. test: /\.(jpg|png|woff|woff2|eot|ttf|svg)$/,
  61. use: [
  62. { loader: 'url-loader', options: `?limit=${ASSET_INLINE_LIMIT}` }
  63. ]
  64. }
  65. ]
  66. },
  67. optimization: {
  68. minimizer: [
  69. new TerserJSPlugin(
  70. { terserOptions: { output: { comments: false } }, parallel: TERSER_PARALLEL_WORKERS }
  71. ),
  72. new OptimizeCSSAssetsPlugin(
  73. { minimizerOptions: { preset: ['default', { discardComments: { removeAll: true } }] } }
  74. )
  75. ],
  76. },
  77. entry: {
  78. "frontend": "./ui/frontend/js/frontend.js"
  79. },
  80. output: {
  81. publicPath: '/' + getCurrentProjectName() + '/assets/frontend/',
  82. path: path.resolve(__dirname, "assets/frontend/"),
  83. filename: "[name].[contenthash].js"
  84. },
  85. };