webpack.config.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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: /\.css$/i,
  89. use: [
  90. { loader: "style-loader", options: { injectType: "linkTag" } },
  91. { loader: "file-loader" },
  92. ],
  93. },
  94. {
  95. test: /\.(woff(2)?|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
  96. use: [
  97. {
  98. loader: "file-loader",
  99. options: {
  100. name: "[name].[ext]",
  101. outputPath: "fonts/",
  102. },
  103. },
  104. ],
  105. },
  106. ],
  107. },
  108. resolve: {
  109. modules: [path.resolve(__dirname, "src"), "node_modules"],
  110. extensions: ["*", ".tsx", ".ts", ".js", ".jsx", ".json"],
  111. },
  112. output: {
  113. filename: "bundle.js",
  114. path: path.resolve(__dirname, "build"),
  115. publicPath: "/",
  116. },
  117. devServer: {
  118. historyApiFallback: true,
  119. disableHostCheck: true,
  120. host: "0.0.0.0",
  121. port: env.DEV_SERVER_PORT || 8080,
  122. hot: true,
  123. },
  124. plugins: [
  125. new HtmlWebpackPlugin(htmlPluginOpts),
  126. new webpack.DefinePlugin(envKeys),
  127. isDevelopment && new ReactRefreshWebpackPlugin(),
  128. ].filter(Boolean),
  129. };
  130. if (!isDevelopment) {
  131. config.optimization = {
  132. minimize: true,
  133. minimizer: [
  134. new TerserPlugin({
  135. test: /\.(ts|tsx|mjs|js|jsx)$/,
  136. terserOptions: {
  137. parse: {
  138. // We want terser to parse ecma 8 code. However, we don't want it
  139. // to apply minification steps that turns valid ecma 5 code
  140. // into invalid ecma 5 code. This is why the `compress` and `output`
  141. ecma: 8,
  142. },
  143. compress: {
  144. ecma: 5,
  145. warnings: false,
  146. inline: 2,
  147. },
  148. mangle: {
  149. // Find work around for Safari 10+
  150. safari10: true,
  151. },
  152. output: {
  153. ecma: 5,
  154. comments: false,
  155. ascii_only: true,
  156. },
  157. },
  158. // Use multi-process parallel running to improve the build speed
  159. parallel: true,
  160. }),
  161. ],
  162. };
  163. }
  164. if (env.ENABLE_ANALYZER) {
  165. config.plugins.push(new BundleAnalyzerPlugin());
  166. }
  167. if (env.ENABLE_PROXY) {
  168. if (!env.API_SERVER) {
  169. throw new Error(
  170. "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"
  171. );
  172. }
  173. config.devServer.proxy = {
  174. "/api": {
  175. logLevel: "debug",
  176. target: env.API_SERVER, // target host
  177. changeOrigin: true, // needed for virtual hosted sites
  178. ws: true, // proxy websockets
  179. },
  180. };
  181. }
  182. return config;
  183. };