| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- const fs = require("fs");
- const path = require("path");
- const { defineConfig } = require("vite");
- const { viteSingleFile } = require("vite-plugin-singlefile");
- const platformBuilds = {
- applovin: { adapter: "applovin", output: "applovin" },
- unity: { adapter: "unity", output: "unity" },
- playturbo: { adapter: "playturbo", output: "playturbo" },
- mintegral: { adapter: "playturbo", output: "mintegral" },
- google: { adapter: "google", output: "google" },
- };
- function patchSingleFileHtml(htmlPath) {
- if (!fs.existsSync(htmlPath)) return;
- const html = fs
- .readFileSync(htmlPath, "utf8")
- .replace(/<script\s+type="module"\s+crossorigin>/g, "<script>")
- .replace(/<script\s+crossorigin\s+type="module">/g, "<script>")
- .replace(/<script\s+type="module">/g, "<script>")
- .replace(/<script\s+crossorigin>/g, "<script>")
- .replace(/<style\s+rel="stylesheet"\s+crossorigin>/g, "<style>")
- .replace(/<style\s+crossorigin\s+rel="stylesheet">/g, "<style>")
- .replace(/<style\s+crossorigin>/g, "<style>");
- fs.writeFileSync(htmlPath, html);
- }
- function finalizeHtmlPlugin(outDir, fileName) {
- return {
- name: "finalize-html-output",
- closeBundle() {
- const indexPath = path.resolve(__dirname, outDir, "index.html");
- const targetPath = path.resolve(__dirname, outDir, fileName);
- if (fileName !== "index.html" && fs.existsSync(indexPath)) {
- fs.renameSync(indexPath, targetPath);
- }
- patchSingleFileHtml(targetPath);
- },
- };
- }
- module.exports = defineConfig(({ mode }) => {
- const platformBuild = platformBuilds[mode];
- const adapter = platformBuild?.adapter || "google";
- const output = platformBuild?.output;
- const outDir = output ? `dist/${output}` : "dist";
- const htmlFileName = output ? `${output}.html` : "index.html";
- return {
- plugins: [viteSingleFile(), finalizeHtmlPlugin(outDir, htmlFileName)],
- resolve: {
- alias: {
- "./ad-platform/current": path.resolve(
- __dirname,
- `src/filler/ad-platform/adapters/${adapter}.ts`,
- ),
- },
- },
- build: {
- // 所有资源内联到 HTML,目标单文件广告
- assetsInlineLimit: 100 * 1024 * 1024, // 不限大小,全部内联
- target: "es2017",
- outDir,
- emptyOutDir: true,
- rollupOptions: {
- input: {
- [path.basename(htmlFileName, ".html")]: "./index.html",
- },
- output: {
- entryFileNames: "assets/[name].js",
- chunkFileNames: "assets/[name].js",
- assetFileNames: (assetInfo) => {
- if (assetInfo.name && assetInfo.name.endsWith(".html")) {
- return htmlFileName;
- }
- return "assets/[name][extname]";
- },
- },
- },
- },
- };
- });
|