webpack.config.js 5.5 KB

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