webpack.config.js 5.6 KB

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