webpack.prod.config.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const path = require("path");
  2. const TerserPlugin = require("terser-webpack-plugin");
  3. const BundleAnalyzerPlugin =
  4. require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
  5. const autoprefixer = require("autoprefixer");
  6. const webpack = require("webpack");
  7. const { parseEnvVariables } = require("./env");
  8. module.exports = {
  9. mode: "production",
  10. entry: {
  11. "excalidraw.production.min": "./entry.js",
  12. },
  13. output: {
  14. path: path.resolve(__dirname, "dist"),
  15. library: "Excalidraw",
  16. libraryTarget: "umd",
  17. filename: "[name].js",
  18. chunkFilename: "excalidraw-assets/[name]-[contenthash].js",
  19. publicPath: "",
  20. },
  21. resolve: {
  22. extensions: [".js", ".ts", ".tsx", ".css", ".scss"],
  23. },
  24. module: {
  25. rules: [
  26. {
  27. test: /\.(sa|sc|c)ss$/,
  28. exclude: /node_modules/,
  29. use: [
  30. "style-loader",
  31. {
  32. loader: "css-loader",
  33. },
  34. {
  35. loader: "postcss-loader",
  36. options: {
  37. postcssOptions: {
  38. plugins: [autoprefixer()],
  39. },
  40. },
  41. },
  42. "sass-loader",
  43. ],
  44. },
  45. {
  46. test: /\.(ts|tsx|js|jsx|mjs)$/,
  47. exclude: /node_modules/,
  48. use: [
  49. {
  50. loader: "ts-loader",
  51. options: {
  52. transpileOnly: true,
  53. configFile: path.resolve(__dirname, "../tsconfig.prod.json"),
  54. },
  55. },
  56. {
  57. loader: "babel-loader",
  58. options: {
  59. presets: [
  60. "@babel/preset-env",
  61. ["@babel/preset-react", { runtime: "automatic" }],
  62. "@babel/preset-typescript",
  63. ],
  64. plugins: [
  65. "@babel/plugin-proposal-object-rest-spread",
  66. "@babel/plugin-transform-arrow-functions",
  67. "transform-class-properties",
  68. "@babel/plugin-transform-async-to-generator",
  69. "@babel/plugin-transform-runtime",
  70. ],
  71. },
  72. },
  73. ],
  74. },
  75. {
  76. test: /\.(woff|woff2|eot|ttf|otf)$/,
  77. use: [
  78. {
  79. loader: "file-loader",
  80. options: {
  81. name: "[name].[ext]",
  82. outputPath: "excalidraw-assets",
  83. },
  84. },
  85. ],
  86. },
  87. ],
  88. },
  89. optimization: {
  90. minimize: true,
  91. minimizer: [
  92. new TerserPlugin({
  93. test: /\.js($|\?)/i,
  94. }),
  95. ],
  96. splitChunks: {
  97. chunks: "async",
  98. cacheGroups: {
  99. vendors: {
  100. test: /[\\/]node_modules[\\/]/,
  101. name: "vendor",
  102. },
  103. },
  104. },
  105. },
  106. plugins: [
  107. ...(process.env.ANALYZER === "true" ? [new BundleAnalyzerPlugin()] : []),
  108. new webpack.DefinePlugin({
  109. "process.env": parseEnvVariables(
  110. path.resolve(__dirname, "../../../.env.production"),
  111. ),
  112. }),
  113. ],
  114. externals: {
  115. react: {
  116. root: "React",
  117. commonjs2: "react",
  118. commonjs: "react",
  119. amd: "react",
  120. },
  121. "react-dom": {
  122. root: "ReactDOM",
  123. commonjs2: "react-dom",
  124. commonjs: "react-dom",
  125. amd: "react-dom",
  126. },
  127. },
  128. };