webpack.config.js 4.4 KB

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