File size: 1,477 Bytes
674c130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48bed25
674c130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import fs from 'node:fs';
import path from 'node:path';

const root = process.cwd();
const dist = path.join(root, 'dist');
const indexPath = path.join(dist, 'index.html');
const outPath = path.join(dist, 'standalone-preview.html');

const index = fs.readFileSync(indexPath, 'utf8');
const scriptMatch = index.match(/<script[^>]+src="([^"]+)"[^>]*><\/script>/);
const styleMatch = index.match(/<link[^>]+href="([^"]+)"[^>]*>/);

if (!scriptMatch || !styleMatch) {
  throw new Error('Could not find built JS/CSS assets in dist/index.html');
}

const toAssetPath = (assetUrl) => path.join(dist, assetUrl.replace(/^\/ui\//, '').replace(/^\.\//, ''));
let js = fs.readFileSync(toAssetPath(scriptMatch[1]), 'utf8');
const css = fs.readFileSync(toAssetPath(styleMatch[1]), 'utf8');

js = js.replace(/(["'`])\/ui\/assets\/([^"'`]+\.(png|jpg|jpeg|webp|gif))\1/g, (match, quote, assetName, extension) => {
  const assetPath = path.join(dist, 'assets', assetName);
  if (!fs.existsSync(assetPath)) {
    return match;
  }

  return `${quote}/gradio_api/file=dist/assets/${assetName}${quote}`;
});

const html = `<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>CultureLens Preview</title>
    <style>${css}</style>
  </head>
  <body>
    <div id="root"></div>
    <script type="module">${js}</script>
  </body>
</html>
`;

fs.writeFileSync(outPath, html);
console.log(outPath);