webpack.config.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. const path = require("path");
  2. const HtmlWebpackPlugin = require("html-webpack-plugin");
  3. const webpack = require("webpack");
  4. const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
  5. const dotenv = require("dotenv");
  6. const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
  7. .BundleAnalyzerPlugin;
  8. const TerserPlugin = require("terser-webpack-plugin");
  9. module.exports = () => {
  10. const env = dotenv.config().parsed;
  11. const envKeys = Object.keys(env).reduce((prev, next) => {
  12. prev[`process.env.${next}`] = JSON.stringify(env[next]);
  13. return prev;
  14. }, {});
  15. // Check first the env file and if it's empty, check out the node env of the process.
  16. let isDevelopment = env.NODE_ENV !== "production";
  17. if (process.env.NODE_ENV !== env.NODE_ENV) {
  18. isDevelopment = process.env.NODE_ENV !== "production";
  19. }
  20. /**
  21. * @type {webpack.Configuration}
  22. */
  23. const config = {
  24. entry: ["./src/index.tsx"],
  25. target: "web",
  26. mode: isDevelopment ? "development" : "production",
  27. module: {
  28. rules: [
  29. {
  30. test: /\.(ts|tsx|mjs|js|jsx)$/,
  31. exclude: /node_modules/,
  32. use: [
  33. {
  34. loader: require.resolve("babel-loader"),
  35. options: {
  36. plugins: [
  37. isDevelopment && require.resolve("react-refresh/babel"),
  38. ].filter(Boolean),
  39. },
  40. },
  41. ],
  42. },
  43. {
  44. enforce: "pre",
  45. test: /\.js$/,
  46. loader: "source-map-loader",
  47. },
  48. {
  49. test: /\.(png|svg|jpg|gif|mp3)$/,
  50. use: ["file-loader"],
  51. },
  52. { test: /\.css$/, use: ["css-loader"] },
  53. {
  54. test: /\.(woff(2)?|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
  55. use: [
  56. {
  57. loader: "file-loader",
  58. options: {
  59. name: "[name].[ext]",
  60. outputPath: "fonts/",
  61. },
  62. },
  63. ],
  64. },
  65. ],
  66. },
  67. resolve: {
  68. modules: [path.resolve(__dirname, "src"), "node_modules"],
  69. extensions: ["*", ".tsx", ".ts", ".js", ".jsx", ".json"],
  70. },
  71. output: {
  72. filename: "bundle.js",
  73. path: path.resolve(__dirname, "build"),
  74. publicPath: "/",
  75. },
  76. devServer: {
  77. historyApiFallback: true,
  78. disableHostCheck: true,
  79. host: "0.0.0.0",
  80. port: env.DEV_SERVER_PORT || 8080,
  81. hot: true,
  82. },
  83. plugins: [
  84. new HtmlWebpackPlugin({
  85. template: path.resolve(__dirname, "src", "index.html"),
  86. segmentKey: `${process.env.SEGMENT_PUBLIC_KEY}`,
  87. }),
  88. new webpack.DefinePlugin(envKeys),
  89. isDevelopment && new ReactRefreshWebpackPlugin(),
  90. ].filter(Boolean),
  91. };
  92. if (!isDevelopment) {
  93. config.optimization = {
  94. minimize: true,
  95. minimizer: [
  96. new TerserPlugin({
  97. test: /\.(ts|tsx|mjs|js|jsx)$/,
  98. terserOptions: {
  99. parse: {
  100. // We want terser to parse ecma 8 code. However, we don't want it
  101. // to apply minification steps that turns valid ecma 5 code
  102. // into invalid ecma 5 code. This is why the `compress` and `output`
  103. ecma: 8,
  104. },
  105. compress: {
  106. ecma: 5,
  107. warnings: false,
  108. inline: 2,
  109. },
  110. mangle: {
  111. // Find work around for Safari 10+
  112. safari10: true,
  113. },
  114. output: {
  115. ecma: 5,
  116. comments: false,
  117. ascii_only: true,
  118. },
  119. },
  120. // Use multi-process parallel running to improve the build speed
  121. parallel: true,
  122. }),
  123. ],
  124. };
  125. }
  126. if (env.ENABLE_ANALYZER) {
  127. config.plugins.push(new BundleAnalyzerPlugin());
  128. }
  129. console.log(env);
  130. if (env.ENABLE_PROXY) {
  131. console.log("WORKED!");
  132. if (!env.API_SERVER) {
  133. throw new Error(
  134. "API_SERVER is not present on .env! Please setup the api server url if you want the proxy to work! API_SERVER example: http://localhost:8080"
  135. );
  136. }
  137. config.devServer.proxy = {
  138. "/api": {
  139. logLevel: "debug",
  140. target: env.API_SERVER, // target host
  141. changeOrigin: true, // needed for virtual hosted sites
  142. ws: true, // proxy websockets
  143. },
  144. };
  145. }
  146. return config;
  147. };