webpack.config.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const path = require("path");
  2. const TerserJSPlugin = require("terser-webpack-plugin");
  3. const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
  4. const HtmlPlugin = require("html-webpack-plugin");
  5. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  6. const { CleanWebpackPlugin } = require("clean-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 = -3;
  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. template: path.resolve(__dirname, "masterpage_template.tpl"),
  29. filename: path.resolve(__dirname, "masterpage.tpl")
  30. }),
  31. new MiniCssExtractPlugin({ filename: "styles.[contenthash].css" }),
  32. new webpack.ProvidePlugin({
  33. $: 'jquery',
  34. jQuery: 'jquery',
  35. 'window.jQuery': 'jquery',
  36. toastr: 'toastr'
  37. })
  38. ],
  39. module: {
  40. rules: [
  41. {
  42. test: /\.(css|scss)$/,
  43. use: [
  44. { loader: MiniCssExtractPlugin.loader },
  45. { loader: "css-loader" },
  46. { loader: "sass-loader" }
  47. ]
  48. },
  49. // Import various assets
  50. {
  51. test: /\.(jpg|png|woff|woff2|eot|ttf|svg)$/i,
  52. type: "asset",
  53. parser: {
  54. dataUrlCondition: { maxSize: ASSET_INLINE_LIMIT }
  55. }
  56. }
  57. ]
  58. },
  59. optimization: {
  60. minimizer: [
  61. new TerserJSPlugin({
  62. terserOptions: { format: { comments: false } },
  63. parallel: TERSER_PARALLEL_WORKERS
  64. }),
  65. new CssMinimizerPlugin()
  66. ],
  67. },
  68. entry: "./assets/js/index.js",
  69. output: {
  70. publicPath: '/' + getCurrentProjectName() + '/assets/admin/',
  71. path: path.resolve(__dirname, "../../assets/admin/"),
  72. filename: "bundle.[contenthash].js"
  73. }
  74. };