webpack.config.js 5.0 KB

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