webpack.config.js 5.0 KB

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