webpack.config.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { dirname, join } from "path";
  2. import { exec } from "child_process";
  3. import { fileURLToPath } from "url";
  4. import dotenv from "dotenv";
  5. import MiniCssExtractPlugin from "mini-css-extract-plugin";
  6. import CssMinimizerPlugin from "css-minimizer-webpack-plugin";
  7. dotenv.config();
  8. const defaultMode = ["development", "production"].includes(process.env.NODE_ENV) ? process.env.NODE_ENV : "development";
  9. const outFileSuffix = process.env.OUTFILE_SUFFIX ?? "";
  10. /** Webpack configuration for the output file */
  11. const output = {
  12. filename: `BetterYTM${outFileSuffix}.user.js`,
  13. path: join(dirname(fileURLToPath(import.meta.url)), "/dist"),
  14. clean: true,
  15. module: true,
  16. };
  17. /** @param {import("./src/types").WebpackEnv} env */
  18. const getConfig = (env) => ({
  19. entry: "./src/index.ts",
  20. output,
  21. experiments: {
  22. // userscripts are automatically wrapped in an IIFE by the browser extension, so this can be enabled
  23. outputModule: true,
  24. },
  25. mode: env.mode ?? defaultMode,
  26. resolve: {
  27. extensions: [
  28. ".ts",
  29. ".js",
  30. ".css",
  31. ".md",
  32. ],
  33. },
  34. module: {
  35. rules: [
  36. {
  37. test: /\.tsx?$/,
  38. use: "ts-loader",
  39. exclude: /node_modules/,
  40. },
  41. {
  42. test: /\.html$/i,
  43. loader: "html-loader",
  44. },
  45. {
  46. test: /\.md$/,
  47. use: [
  48. {
  49. loader: "html-loader",
  50. },
  51. {
  52. loader: "markdown-loader",
  53. },
  54. ],
  55. },
  56. {
  57. test: /.css$/,
  58. use: [MiniCssExtractPlugin.loader, "css-loader" /*, "sass-loader"*/],
  59. },
  60. ],
  61. },
  62. optimization: {
  63. minimizer: [
  64. `...`,
  65. new CssMinimizerPlugin(),
  66. ],
  67. },
  68. plugins: [
  69. new MiniCssExtractPlugin(),
  70. {
  71. apply: (compiler) => {
  72. compiler.hooks.afterEmit.tap("AfterEmitPlugin", () => {
  73. exec("npm run post-build", (_err, stdout, stderr) => {
  74. stdout && process.stdout.write(stdout);
  75. stderr && process.stderr.write(stderr);
  76. });
  77. });
  78. },
  79. },
  80. ],
  81. devtool: "source-map",
  82. });
  83. export default getConfig;
  84. export { output };