Guest User

webpack.config.js

a guest
Jan 11th, 2026
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. const path = require('path');
  2. const glob = require('glob');
  3. const webpack = require('webpack');
  4. const HtmlWebpackPlugin = require('html-webpack-plugin');
  5. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  6. const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
  7. const TerserPlugin = require('terser-webpack-plugin');
  8. const CopyWebpackPlugin = require('copy-webpack-plugin');
  9. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  10. const HandlebarsWebpackPlugin = require('handlebars-webpack-plugin');
  11.  
  12. module.exports = (env, argv) => {
  13. const isDevMode = argv.mode === 'development';
  14.  
  15. return {
  16. entry: {
  17. vendor: './webpack.vendor.js',
  18. app: './webpack.app.js'
  19. },
  20. output: {
  21. path: path.resolve(__dirname, 'dist'),
  22. filename: 'assets/js/[name].min.js'
  23. },
  24. module: {
  25. rules: [
  26. {
  27. test: /\.css$/,
  28. use: [
  29. MiniCssExtractPlugin.loader,
  30. 'css-loader',
  31. {
  32. loader: 'postcss-loader',
  33. options: {
  34. postcssOptions: {
  35. plugins: [
  36. require('autoprefixer')()
  37. ]
  38. }
  39. }
  40. }
  41. ]
  42. },
  43. {
  44. test: /\.scss$/,
  45. use: [
  46. MiniCssExtractPlugin.loader,
  47. 'css-loader',
  48. {
  49. loader: 'postcss-loader',
  50. options: {
  51. postcssOptions: {
  52. plugins: [
  53. require('autoprefixer')()
  54. ]
  55. }
  56. }
  57. },
  58. {
  59. loader: 'sass-loader',
  60. options: {
  61. sassOptions: {
  62. silenceDeprecations: ['color-functions', 'global-builtin', 'import', 'legacy-js-api', 'if-function']
  63. }
  64. }
  65. }
  66. ]
  67. },
  68. {
  69. test: /\.(png|svg|jpg|jpeg|gif)$/,
  70. type: 'asset/resource',
  71. generator: {
  72. filename: 'assets/img/[name][ext][query]'
  73. }
  74. },
  75. {
  76. test: /\.(woff|woff2|eot|ttf|otf)$/,
  77. type: 'asset/resource',
  78. generator: {
  79. filename: 'assets/fonts/[name][ext][query]'
  80. }
  81. }
  82. ]
  83. },
  84. plugins: [
  85. new CleanWebpackPlugin(),
  86. new MiniCssExtractPlugin({
  87. filename: 'assets/css/[name].min.css'
  88. }),
  89. new HandlebarsWebpackPlugin({
  90. entry: path.join(process.cwd(), 'src', 'html', '*.hbs'),
  91. output: path.join(process.cwd(), 'dist', '[name].html'),
  92. partials: [
  93. path.join(process.cwd(), 'src', 'html', 'partial', '*.hbs')
  94. ]
  95. }),
  96. ...glob.sync('./src/html/*.hbs').map((templateFile) => {
  97. const filename = path.basename(templateFile, '.hbs') + '.html';
  98. return new HtmlWebpackPlugin({
  99. template: templateFile,
  100. filename: filename,
  101. minify: false,
  102. excludeChunks: ['app', 'vendor']
  103. });
  104. }),
  105. new CopyWebpackPlugin({
  106. patterns: [
  107. { from: 'src/img', to: 'assets/img' },
  108. { from: 'src/data', to: 'assets/data' },
  109. { from: 'src/js/demo', to: 'assets/js/demo' },
  110. { from: 'node_modules/bootstrap-icons/font/fonts/', to: 'assets/css/fonts/' },
  111. { from: 'node_modules/@fortawesome/fontawesome-free/webfonts', to: 'assets/webfonts' },
  112. { from: 'node_modules/@fortawesome/', to: 'assets/plugins/@fortawesome/' },
  113. { from: 'node_modules/@fullcalendar/', to: 'assets/plugins/@fullcalendar/' },
  114. { from: 'node_modules/@highlightjs/cdn-assets/', to: 'assets/plugins/@highlightjs/cdn-assets/' },
  115. { from: 'node_modules/apexcharts/', to: 'assets/plugins/apexcharts/' },
  116. { from: 'node_modules/blueimp-canvas-to-blob/', to: 'assets/plugins/blueimp-canvas-to-blob/' },
  117. { from: 'node_modules/blueimp-file-upload/', to: 'assets/plugins/blueimp-file-upload/' },
  118. { from: 'node_modules/blueimp-gallery/', to: 'assets/plugins/blueimp-gallery/' },
  119. { from: 'node_modules/blueimp-load-image/', to: 'assets/plugins/blueimp-load-image/' },
  120. { from: 'node_modules/blueimp-tmpl/', to: 'assets/plugins/blueimp-tmpl/' },
  121. { from: 'node_modules/bootstrap/', to: 'assets/plugins/bootstrap/' },
  122. { from: 'node_modules/bootstrap-datepicker/', to: 'assets/plugins/bootstrap-datepicker/' },
  123. { from: 'node_modules/bootstrap-daterangepicker/', to: 'assets/plugins/bootstrap-daterangepicker/' },
  124. { from: 'node_modules/bootstrap-icons/', to: 'assets/plugins/bootstrap-icons/' },
  125. { from: 'node_modules/bootstrap-slider/', to: 'assets/plugins/bootstrap-slider/' },
  126. { from: 'node_modules/bootstrap-table/', to: 'assets/plugins/bootstrap-table/' },
  127. { from: 'node_modules/bootstrap-timepicker/', to: 'assets/plugins/bootstrap-timepicker/' },
  128. { from: 'node_modules/chart.js/', to: 'assets/plugins/chart.js/' },
  129. { from: 'node_modules/datatables.net/', to: 'assets/plugins/datatables.net/' },
  130. { from: 'node_modules/datatables.net-bs5/', to: 'assets/plugins/datatables.net-bs5/' },
  131. { from: 'node_modules/datatables.net-autofill/', to: 'assets/plugins/datatables.net-autofill/' },
  132. { from: 'node_modules/datatables.net-autofill-bs5/', to: 'assets/plugins/datatables.net-autofill-bs5/' },
  133. { from: 'node_modules/datatables.net-buttons/', to: 'assets/plugins/datatables.net-buttons/' },
  134. { from: 'node_modules/datatables.net-buttons-bs5/', to: 'assets/plugins/datatables.net-buttons-bs5/' },
  135. { from: 'node_modules/datatables.net-colreorder/', to: 'assets/plugins/datatables.net-colreorder/' },
  136. { from: 'node_modules/datatables.net-colreorder-bs5/', to: 'assets/plugins/datatables.net-colreorder-bs5/' },
  137. { from: 'node_modules/datatables.net-fixedcolumns/', to: 'assets/plugins/datatables.net-fixedcolumns/' },
  138. { from: 'node_modules/datatables.net-fixedcolumns-bs5/', to: 'assets/plugins/datatables.net-fixedcolumns-bs5/' },
  139. { from: 'node_modules/datatables.net-fixedheader/', to: 'assets/plugins/datatables.net-fixedheader/' },
  140. { from: 'node_modules/datatables.net-fixedheader-bs5/', to: 'assets/plugins/datatables.net-fixedheader-bs5/' },
  141. { from: 'node_modules/datatables.net-keytable/', to: 'assets/plugins/datatables.net-keytable/' },
  142. { from: 'node_modules/datatables.net-keytable-bs5/', to: 'assets/plugins/datatables.net-keytable-bs5/' },
  143. { from: 'node_modules/datatables.net-responsive/', to: 'assets/plugins/datatables.net-responsive/' },
  144. { from: 'node_modules/datatables.net-responsive-bs5/', to: 'assets/plugins/datatables.net-responsive-bs5/' },
  145. { from: 'node_modules/datatables.net-rowgroup/', to: 'assets/plugins/datatables.net-rowgroup/' },
  146. { from: 'node_modules/datatables.net-rowgroup-bs5/', to: 'assets/plugins/datatables.net-rowgroup-bs5/' },
  147. { from: 'node_modules/datatables.net-rowreorder-bs5/', to: 'assets/plugins/datatables.net-rowreorder-bs5/' },
  148. { from: 'node_modules/datatables.net-scroller/', to: 'assets/plugins/datatables.net-scroller/' },
  149. { from: 'node_modules/datatables.net-scroller-bs5/', to: 'assets/plugins/datatables.net-scroller-bs5/' },
  150. { from: 'node_modules/datatables.net-select/', to: 'assets/plugins/datatables.net-select/' },
  151. { from: 'node_modules/datatables.net-select-bs5/', to: 'assets/plugins/datatables.net-select-bs5/' },
  152. { from: 'node_modules/jquery/', to: 'assets/plugins/jquery/' },
  153. { from: 'node_modules/jquery-migrate/', to: 'assets/plugins/jquery-migrate/' },
  154. { from: 'node_modules/jquery-typeahead/', to: 'assets/plugins/jquery-typeahead/' },
  155. { from: 'node_modules/jquery-ui-dist/', to: 'assets/plugins/jquery-ui-dist/' },
  156. { from: 'node_modules/jquery.maskedinput/', to: 'assets/plugins/jquery.maskedinput/' },
  157. { from: 'node_modules/js-cookie/', to: 'assets/plugins/js-cookie/' },
  158. { from: 'node_modules/jszip/', to: 'assets/plugins/jszip/' },
  159. { from: 'node_modules/jvectormap-content/', to: 'assets/plugins/jvectormap-content/' },
  160. { from: 'node_modules/jvectormap-next/', to: 'assets/plugins/jvectormap-next/' },
  161. { from: 'node_modules/lity/', to: 'assets/plugins/lity/' },
  162. { from: 'node_modules/kbw-countdown/', to: 'assets/plugins/kbw-countdown/' },
  163. { from: 'node_modules/masonry-layout/', to: 'assets/plugins/masonry-layout/' },
  164. { from: 'node_modules/moment/', to: 'assets/plugins/moment/' },
  165. { from: 'node_modules/perfect-scrollbar/', to: 'assets/plugins/perfect-scrollbar/' },
  166. { from: 'node_modules/photoswipe/', to: 'assets/plugins/photoswipe/' },
  167. { from: 'node_modules/select-picker/', to: 'assets/plugins/select-picker/' },
  168. { from: 'node_modules/spectrum-colorpicker2/', to: 'assets/plugins/spectrum-colorpicker2/' },
  169. { from: 'node_modules/summernote/', to: 'assets/plugins/summernote/' },
  170. { from: 'node_modules/tag-it/', to: 'assets/plugins/tag-it' }
  171. ]
  172. })
  173. ],
  174. optimization: {
  175. minimizer: [
  176. new TerserPlugin({
  177. exclude: /assets\/plugins\/jquery-migrate\/test\/runner\/command\.js/,
  178. }),
  179. new CssMinimizerPlugin()
  180. ],
  181. splitChunks: {
  182. cacheGroups: {
  183. vendorStyles: {
  184. type: 'css/mini-extract',
  185. test: /\.css$/,
  186. name: 'vendor',
  187. chunks: 'all',
  188. enforce: true,
  189. }
  190. }
  191. },
  192. },
  193. devServer: {
  194. static: {
  195. directory: path.join(__dirname, 'dist'),
  196. },
  197. devMiddleware: {
  198. writeToDisk: true,
  199. },
  200. liveReload: true,
  201. compress: false,
  202. port: 8000,
  203. hot: true
  204. },
  205. performance: {
  206. maxEntrypointSize: 1024000,
  207. maxAssetSize: 1024000,
  208. }
  209. }
  210. };
Advertisement
Add Comment
Please, Sign In to add comment