Jacky Zhao
2023-08-07 cd9dc6ecb570c08291e73d9db001e6068df4d88a
quartz/bootstrap-cli.mjs
@@ -12,10 +12,10 @@
import chokidar from "chokidar"
import prettyBytes from "pretty-bytes"
import { execSync, spawnSync } from "child_process"
import { transform as cssTransform } from "lightningcss"
import http from "http"
import serveHandler from "serve-handler"
import { WebSocketServer } from "ws"
import { randomUUID } from "crypto"
const ORIGIN_NAME = "origin"
const UPSTREAM_NAME = "upstream"
@@ -297,7 +297,8 @@
      outfile: path.join("quartz", cacheFile),
      bundle: true,
      keepNames: true,
      minify: true,
      minifyWhitespace: true,
      minifySyntax: true,
      platform: "node",
      format: "esm",
      jsx: "automatic",
@@ -305,18 +306,11 @@
      packages: "external",
      metafile: true,
      sourcemap: true,
      sourcesContent: false,
      plugins: [
        sassPlugin({
          type: "css-text",
          cssImports: true,
          async transform(css) {
            const { code } = cssTransform({
              filename: "style.css",
              code: Buffer.from(css),
              minify: true,
            })
            return code.toString()
          },
        }),
        {
          name: "inline-script-loader",
@@ -353,9 +347,7 @@
      ],
    })
    let clientRefresh = () => {}
    let closeHandler = null
    const build = async () => {
    const build = async (clientRefresh) => {
      const result = await ctx.rebuild().catch((err) => {
        console.error(`${chalk.red("Couldn't parse Quartz configuration:")} ${fp}`)
        console.log(`Reason: ${chalk.grey(err)}`)
@@ -374,21 +366,19 @@
      }
      // bypass module cache
      const { default: buildQuartz } = await import(cacheFile + `?update=${new Date()}`)
      if (closeHandler) {
        await closeHandler()
      }
      closeHandler = await buildQuartz(argv, clientRefresh)
      // https://github.com/nodejs/modules/issues/307
      const { default: buildQuartz } = await import(cacheFile + `?update=${randomUUID()}`)
      await buildQuartz(argv, clientRefresh)
      clientRefresh()
    }
    await build()
    if (argv.serve) {
      const wss = new WebSocketServer({ port: 3001 })
      const connections = []
      wss.on("connection", (ws) => connections.push(ws))
      clientRefresh = () => connections.forEach((conn) => conn.send("rebuild"))
      const clientRefresh = () => connections.forEach((conn) => conn.send("rebuild"))
      await build(clientRefresh)
      const server = http.createServer(async (req, res) => {
        await serveHandler(req, res, {
          public: argv.output,
@@ -412,9 +402,10 @@
        })
        .on("all", async () => {
          console.log(chalk.yellow("Detected a source code change, doing a hard rebuild..."))
          await build()
          await build(clientRefresh)
        })
    } else {
      await build(() => {})
      ctx.dispose()
    }
  })