vite.config.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const fs = require("fs");
  2. const path = require("path");
  3. const { defineConfig } = require("vite");
  4. const { viteSingleFile } = require("vite-plugin-singlefile");
  5. const platformBuilds = {
  6. applovin: { adapter: "applovin", output: "applovin" },
  7. unity: { adapter: "unity", output: "unity" },
  8. playturbo: { adapter: "playturbo", output: "playturbo" },
  9. mintegral: { adapter: "playturbo", output: "mintegral" },
  10. google: { adapter: "google", output: "google" },
  11. };
  12. function patchSingleFileHtml(htmlPath, adapter) {
  13. if (!fs.existsSync(htmlPath)) return;
  14. let html = fs
  15. .readFileSync(htmlPath, "utf8")
  16. .replace(/<script\s+type="module"\s+crossorigin>/g, "<script>")
  17. .replace(/<script\s+crossorigin\s+type="module">/g, "<script>")
  18. .replace(/<script\s+type="module">/g, "<script>")
  19. .replace(/<script\s+crossorigin>/g, "<script>")
  20. .replace(/<style\s+rel="stylesheet"\s+crossorigin>/g, "<style>")
  21. .replace(/<style\s+crossorigin\s+rel="stylesheet">/g, "<style>")
  22. .replace(/<style\s+crossorigin>/g, "<style>");
  23. fs.writeFileSync(htmlPath, html);
  24. }
  25. function finalizeHtmlPlugin(outDir, adapter) {
  26. return {
  27. name: "finalize-html-output",
  28. closeBundle() {
  29. const htmlPath = path.resolve(__dirname, outDir, "index.html");
  30. patchSingleFileHtml(htmlPath, adapter);
  31. },
  32. };
  33. }
  34. module.exports = defineConfig(({ mode }) => {
  35. const platformBuild = platformBuilds[mode];
  36. const adapter = platformBuild?.adapter || "google";
  37. const output = platformBuild?.output;
  38. const outDir = output ? `dist/${output}` : "dist";
  39. // 预览模式下通过 PREVIEW_BASE_PATH 设置 base,如 /ads-preview/
  40. // 这会控制 Vite 注入的 @vite/client 路径 + 所有 JS import 模块路径的前缀
  41. const base = process.env.PREVIEW_BASE_PATH || undefined;
  42. if (base) console.log(`[vite:config] base = ${base}`);
  43. return {
  44. base,
  45. plugins: [viteSingleFile(), finalizeHtmlPlugin(outDir, adapter)],
  46. server: {
  47. allowedHosts: ["color2.jccytech.cn", "localhost", ".jccytech.cn"],
  48. },
  49. resolve: {
  50. alias: {
  51. "#ad-config": path.resolve(
  52. __dirname,
  53. process.env.AD_CONFIG_PATH || "src/filler/ad-config.ts",
  54. ),
  55. "./ad-platform/current": path.resolve(
  56. __dirname,
  57. `src/filler/ad-platform/adapters/${adapter}.ts`,
  58. ),
  59. },
  60. },
  61. build: {
  62. // 所有资源内联到 HTML,目标单文件广告
  63. assetsInlineLimit: 100 * 1024 * 1024, // 不限大小,全部内联
  64. target: "es2017",
  65. outDir,
  66. emptyOutDir: true,
  67. rollupOptions: {
  68. input: {
  69. main: "./index.html",
  70. },
  71. output: {
  72. entryFileNames: "assets/[name].js",
  73. chunkFileNames: "assets/[name].js",
  74. assetFileNames: "assets/[name][extname]",
  75. },
  76. },
  77. },
  78. };
  79. });