2
0

webpack.config.js 5.5 KB

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