webpack.common.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. module.exports = {
  16. entry: "./src/index.tsx",
  17. output: {
  18. filename: "[name].[hash].bundle.js",
  19. chunkFilename: "[name].[hash].bundle.js",
  20. path: path.resolve(__dirname, "dist"),
  21. publicPath: "/",
  22. },
  23. performance: { hints: false },
  24. plugins: [
  25. new webpack.DefinePlugin(envKeys),
  26. new CleanWebpackPlugin(),
  27. new CopyPlugin({ patterns: ["./public"] }),
  28. new HtmlWebpackPlugin({ template: "./public/index.html" }),
  29. ],
  30. resolve: {
  31. modules: [__dirname, "src", "node_modules"],
  32. extensions: ["*", ".js", ".jsx", ".tsx", ".ts"],
  33. alias: {
  34. "@src": path.resolve(__dirname, "src/"),
  35. },
  36. },
  37. module: {
  38. rules: [
  39. {
  40. test: /\.tsx?$/,
  41. exclude: /node_modules/,
  42. loader: require.resolve("babel-loader"),
  43. },
  44. {
  45. test: /\.(png|jpe?g|svg|woff2?|ttf|eot)$/,
  46. loader: "url-loader",
  47. options: {
  48. limit: 8192,
  49. name: "./assets/[hash].[ext]",
  50. },
  51. },
  52. ],
  53. },
  54. optimization: {
  55. splitChunks: {
  56. cacheGroups: {
  57. vendor: {
  58. test: /node_modules/,
  59. chunks: "initial",
  60. name: "vendor",
  61. enforce: true,
  62. },
  63. },
  64. },
  65. },
  66. };