webpack.config.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const path = require("path");
  2. const HtmlWebpackPlugin = require("html-webpack-plugin");
  3. const webpack = require("webpack");
  4. const dotenv = require("dotenv");
  5. module.exports = () => {
  6. const env = dotenv.config().parsed;
  7. const envKeys = Object.keys(env).reduce((prev, next) => {
  8. prev[`process.env.${next}`] = JSON.stringify(env[next]);
  9. return prev;
  10. }, {});
  11. return {
  12. entry: "./src/index.tsx",
  13. target: "web",
  14. mode: "development",
  15. module: {
  16. rules: [
  17. {
  18. test: /\.(ts|tsx)$/,
  19. loader: "ts-loader",
  20. },
  21. {
  22. enforce: "pre",
  23. test: /\.js$/,
  24. loader: "source-map-loader",
  25. },
  26. {
  27. test: /\.(png|svg|jpg|gif|mp3)$/,
  28. use: ["file-loader"],
  29. },
  30. { test: /\.css$/, use: ["css-loader"] },
  31. {
  32. test: /\.(woff(2)?|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
  33. use: [
  34. {
  35. loader: "file-loader",
  36. options: {
  37. name: "[name].[ext]",
  38. outputPath: "fonts/",
  39. },
  40. },
  41. ],
  42. },
  43. ],
  44. },
  45. resolve: {
  46. modules: [path.resolve(__dirname, "src"), "node_modules"],
  47. extensions: ["*", ".tsx", ".ts", ".js", ".jsx", ".json"],
  48. },
  49. output: {
  50. filename: "bundle.js",
  51. path: path.resolve(__dirname, "build"),
  52. publicPath: "/",
  53. },
  54. devServer: {
  55. historyApiFallback: true,
  56. },
  57. plugins: [
  58. new HtmlWebpackPlugin({
  59. template: path.resolve(__dirname, "src", "index.html"),
  60. segmentKey: `${process.env.SEGMENT_PUBLIC_KEY}`,
  61. }),
  62. new webpack.DefinePlugin(envKeys),
  63. ],
  64. };
  65. };