webpack.common.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* eslint-disable @typescript-eslint/no-var-requires */
  2. const HtmlWebpackPlugin = require("html-webpack-plugin");
  3. const webpack = require("webpack");
  4. const path = require("path");
  5. const { CleanWebpackPlugin } = require("clean-webpack-plugin");
  6. const CopyPlugin = require("copy-webpack-plugin");
  7. const dotenv = require("dotenv");
  8. const dotenvConfig = dotenv && dotenv.config && dotenv.config();
  9. const env = (dotenvConfig && dotenvConfig.parsed) || process.env;
  10. const envKeys = Object.keys(env).reduce((prev, next) => {
  11. // eslint-disable-next-line no-param-reassign
  12. prev[`process.env.${next}`] = JSON.stringify(env[next]);
  13. return prev;
  14. }, {});
  15. envKeys["process.env"] = JSON.stringify(env);
  16. const isDevelopment = process.env.NODE_ENV === "development";
  17. module.exports = {
  18. entry: "./src/index.tsx",
  19. output: {
  20. filename: "[name].[contenthash].bundle.js",
  21. chunkFilename: "[name].[contenthash].bundle.js",
  22. path: path.resolve(__dirname, "dist"),
  23. publicPath: "/",
  24. },
  25. performance: { hints: false },
  26. plugins: [
  27. new webpack.DefinePlugin(envKeys),
  28. new CleanWebpackPlugin(),
  29. new CopyPlugin({
  30. patterns: [
  31. {
  32. from: path.resolve(__dirname, "public"),
  33. globOptions: {
  34. ignore: ["**/index.html"],
  35. },
  36. noErrorOnMissing: true,
  37. },
  38. ],
  39. }),
  40. new HtmlWebpackPlugin({
  41. template: "./public/index.html",
  42. }),
  43. ],
  44. resolve: {
  45. modules: [__dirname, "src", "node_modules"],
  46. extensions: ["*", ".js", ".jsx", ".tsx", ".ts"],
  47. alias: {
  48. "@src": path.resolve(__dirname, "src/"),
  49. },
  50. },
  51. module: {
  52. rules: [
  53. {
  54. test: /\.tsx?$/,
  55. exclude: /node_modules/,
  56. use: [
  57. {
  58. loader: require.resolve("babel-loader"),
  59. options: {
  60. plugins: [
  61. isDevelopment && require.resolve("react-refresh/babel"),
  62. ].filter(Boolean),
  63. },
  64. },
  65. ],
  66. },
  67. {
  68. test: /\.(png|jpe?g|svg|woff2?|ttf|eot)$/,
  69. type: "asset",
  70. parser: {
  71. dataUrlCondition: {
  72. maxSize: 8192,
  73. },
  74. },
  75. generator: {
  76. filename: "assets/[name].[contenthash][ext]",
  77. },
  78. },
  79. ],
  80. },
  81. optimization: {
  82. splitChunks: {
  83. cacheGroups: {
  84. defaultVendors: {
  85. test: /node_modules/,
  86. chunks: "initial",
  87. name: "vendor",
  88. enforce: true,
  89. },
  90. },
  91. },
  92. },
  93. };