webpack.common.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. module.exports = {
  17. entry: "./src/index.tsx",
  18. output: {
  19. filename: "[name].[contenthash].bundle.js",
  20. chunkFilename: "[name].[contenthash].bundle.js",
  21. path: path.resolve(__dirname, "dist"),
  22. publicPath: "/",
  23. },
  24. performance: { hints: false },
  25. plugins: [
  26. new webpack.DefinePlugin(envKeys),
  27. new CleanWebpackPlugin(),
  28. new CopyPlugin({
  29. patterns: [
  30. {
  31. from: path.resolve(__dirname, "public"),
  32. globOptions: {
  33. ignore: ["**/index.html"],
  34. },
  35. noErrorOnMissing: true,
  36. },
  37. ]
  38. }),
  39. new HtmlWebpackPlugin({
  40. template: "./public/index.html"
  41. }),
  42. ],
  43. resolve: {
  44. modules: [__dirname, "src", "node_modules"],
  45. extensions: ["*", ".js", ".jsx", ".tsx", ".ts"],
  46. alias: {
  47. "@src": path.resolve(__dirname, "src/"),
  48. },
  49. },
  50. module: {
  51. rules: [
  52. {
  53. test: /\.tsx?$/,
  54. exclude: /node_modules/,
  55. loader: require.resolve("babel-loader"),
  56. },
  57. {
  58. test: /\.(png|jpe?g|svg|woff2?|ttf|eot)$/,
  59. type: "asset",
  60. parser: {
  61. dataUrlCondition: {
  62. maxSize: 8192,
  63. },
  64. },
  65. generator: {
  66. filename: "assets/[name].[contenthash][ext]",
  67. },
  68. },
  69. ],
  70. },
  71. optimization: {
  72. splitChunks: {
  73. cacheGroups: {
  74. vendor: {
  75. test: /node_modules/,
  76. chunks: "initial",
  77. name: "vendor",
  78. enforce: true,
  79. },
  80. },
  81. },
  82. },
  83. };