No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

webpack.config.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const Webpack = require("webpack");
  2. const Path = require("path");
  3. const TerserPlugin = require("terser-webpack-plugin");
  4. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  5. const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
  6. const CopyWebpackPlugin = require("copy-webpack-plugin");
  7. const FileManagerPlugin = require("filemanager-webpack-plugin");
  8. const opts = {
  9. rootDir: process.cwd(),
  10. devBuild: process.env.NODE_ENV !== "production"
  11. };
  12. module.exports = {
  13. entry: {
  14. app: "./src/js/app.js"
  15. },
  16. mode: process.env.NODE_ENV === "production" ? "production" : "development",
  17. devtool:
  18. process.env.NODE_ENV === "production" ? "source-map" : "inline-source-map",
  19. output: {
  20. path: Path.join(opts.rootDir, "dist"),
  21. pathinfo: opts.devBuild,
  22. filename: "js/[name].js",
  23. chunkFilename: 'js/[name].js',
  24. },
  25. performance: { hints: false },
  26. optimization: {
  27. minimizer: [
  28. new TerserPlugin({
  29. parallel: true,
  30. terserOptions: {
  31. ecma: 5
  32. }
  33. }),
  34. new CssMinimizerPlugin({})
  35. ],
  36. runtimeChunk: false
  37. },
  38. plugins: [
  39. // Extract css files to seperate bundle
  40. new MiniCssExtractPlugin({
  41. filename: "css/app.css",
  42. chunkFilename: "css/app.css"
  43. }),
  44. // Copy fonts and images to dist
  45. new CopyWebpackPlugin({
  46. patterns: [
  47. { from: "src/fonts", to: "fonts" },
  48. { from: "src/img", to: "img" }
  49. ]
  50. }),
  51. // Copy dist folder to static
  52. new FileManagerPlugin({
  53. events: {
  54. onEnd: {
  55. copy: [
  56. { source: "./dist/", destination: "./static" }
  57. ]
  58. }
  59. }
  60. }),
  61. ],
  62. module: {
  63. rules: [
  64. // Babel-loader
  65. {
  66. test: /\.js$/,
  67. exclude: /(node_modules)/,
  68. use: {
  69. loader: "babel-loader",
  70. options: {
  71. cacheDirectory: true
  72. }
  73. }
  74. },
  75. // Css-loader & sass-loader
  76. {
  77. test: /\.(sa|sc|c)ss$/,
  78. use: [
  79. MiniCssExtractPlugin.loader,
  80. "css-loader",
  81. "postcss-loader",
  82. "sass-loader"
  83. ]
  84. },
  85. // Load fonts
  86. {
  87. test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
  88. type: "asset/resource",
  89. generator: {
  90. filename: "fonts/[name][ext]"
  91. }
  92. },
  93. // Load images
  94. {
  95. test: /\.(png|jpg|jpeg|gif)(\?v=\d+\.\d+\.\d+)?$/,
  96. type: "asset/resource",
  97. generator: {
  98. filename: "img/[name][ext]"
  99. }
  100. },
  101. ]
  102. },
  103. resolve: {
  104. extensions: [".js", ".scss"],
  105. modules: ["node_modules"],
  106. alias: {
  107. request$: "xhr"
  108. }
  109. },
  110. devServer: {
  111. static: {
  112. directory: Path.join(__dirname, "static")
  113. },
  114. compress: true,
  115. port: 8080,
  116. open: true
  117. }
  118. };