/ scripts / prepare.ts
prepare.ts
 1  // generate stub index.html files for dev entry
 2  import { execSync } from "node:child_process";
 3  import fs from "fs-extra";
 4  import chokidar from "chokidar";
 5  import { isDev, log, port, r } from "./utils";
 6  
 7  /**
 8   * Stub index.html to use Vite in development
 9   */
10  async function stubIndexHtml() {
11    const views = ["options", "popup"];
12  
13    for (const view of views) {
14      await fs.ensureDir(r(`extension/dist/${view}`));
15      let data = await fs.readFile(r(`src/${view}/index.html`), "utf-8");
16      data = data
17        .replace('"./main.ts"', `"http://localhost:${port}/${view}/main.ts"`)
18        .replace(
19          '<div id="app"></div>',
20          '<div id="app">Vite server did not start</div>'
21        );
22      await fs.writeFile(r(`extension/dist/${view}/index.html`), data, "utf-8");
23      log("PRE", `stub ${view}`);
24    }
25  }
26  
27  function writeManifest() {
28    execSync("npx esno ./scripts/manifest.ts", { stdio: "inherit" });
29  }
30  
31  writeManifest();
32  
33  if (isDev) {
34    stubIndexHtml();
35    chokidar.watch(r("src/**/*.html")).on("change", () => {
36      stubIndexHtml();
37    });
38    chokidar.watch([r("src/manifest.ts"), r("package.json")]).on("change", () => {
39      writeManifest();
40    });
41  }