import {defineConfig} from "vite"; import {svelte} from "@sveltejs/vite-plugin-svelte"; import autoprefixer from "autoprefixer"; const entry = process?.env?.VITE_ENTRY; const parentDir = "xcode"; const assetsDir = "assets"; let root = "src"; if (entry === "app") root = "src/app"; if (entry === "content") root = "src/scripts"; if (entry === "page") root = "src/page"; if (entry === "popup") root = "src/popup"; let outDir = `../${parentDir}/Safari-Extension/Resources`; if (entry === "app") outDir = `../../${parentDir}/Shared (App)/`; if (entry === "content") outDir = `../${outDir}`; /** * Plugin that serves a couple different purposes: * 1. Updates the html bundle fileNames so that the path they are output to * matches the structure of the extension directory. For example, rather than * the path being page/index.html, it will be dist/page.html, etc... * 2. Transforms relative paths within these files. Since we are renaming files, * the paths reflected in these html files need to be updated to point t0 * the correct location of the files and assets. * * @returns {import("vite").Plugin} * @see {@link https://vitejs.dev/guide/api-plugin.html Vite Plugin API} * @see {@link https://rollupjs.org/plugin-development/ Rollup Plugin API} */ const processIndexFiles = () => ({ name: "rename-index-files", apply: "build", enforce: "post", generateBundle(_options, bundle) { for (const key of Object.keys(bundle)) { if (key.includes(".html")) { if (entry === "app") { bundle[key].fileName = "Base.lproj/app.html"; } else { const name = key.split("/")[0]; bundle[key].fileName = `${name}.html`; } } } }, transformIndexHtml(html) { if (entry === "app") return html; return html.replace(/(href|src)="\.\.\//g, '$1="./'); } }); /** * Plugin to create an index.html file when running `pnpm dev`. When using this * command the browser will serve the the src` directory. This plugin will * create an index.html file that lists the available entry points for the * extension without the need to create an index.html file for this purpose. * * @returns {import("vite").Plugin} * @see {@link https://vitejs.dev/guide/api-plugin.html Vite Plugin API} * @see {@link https://rollupjs.org/plugin-development/ Rollup Plugin API} */ const buildEntryIndex = () => ({ name: "build-entry-index", apply: "serve", configureServer(server) { server.middlewares.use((req, res, next) => { if (req.originalUrl === "/" && !entry) { res.setHeader("Content-Type", "text/html"); const html = `