all repos — snow-editor @ 13d855e25e8510a57608b0ce77f62cbeed372d0e

small and cozy markdown, and orgmode editor

vite.shared.js (view raw)

 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
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
import fs from 'fs';
import path from 'path';
import { loadEnv } from 'vite';

export function parseAllowedHosts(value) {
  if (!value || value.trim() === '') {
    return ['localhost', '127.0.0.1'];
  }
  if (value.trim() === '*') {
    return true;
  }
  return value
    .split(',')
    .map((host) => host.trim())
    .filter(Boolean);
}

export function resolveAllowedHosts(mode) {
  const fileEnv = loadEnv(mode, process.cwd(), '');
  return parseAllowedHosts(process.env.ALLOWED_HOSTS ?? fileEnv.ALLOWED_HOSTS);
}

export function resolveAllowSearchIndexing(mode) {
  const fileEnv = loadEnv(mode, process.cwd(), '');
  const value = (
    process.env.VITE_ALLOW_SEARCH_INDEXING ?? fileEnv.VITE_ALLOW_SEARCH_INDEXING ?? ''
  )
    .trim()
    .toLowerCase();
  return value === 'true';
}

export function robotsSeoPlugin(allowIndexing) {
  let outDir = 'dist';

  return {
    name: 'snow-robots-seo',
    configResolved(config) {
      outDir = config.build.outDir;
    },
    transformIndexHtml(html) {
      if (allowIndexing) return html;
      return html.replace(
        '</head>',
        '    <meta name="robots" content="noindex, nofollow" />\n  </head>',
      );
    },
    closeBundle() {
      const content = allowIndexing
        ? 'User-agent: *\nAllow: /\n'
        : 'User-agent: *\nDisallow: /\n';
      const robotsPath = path.join(outDir, 'robots.txt');
      fs.writeFileSync(robotsPath, content, 'utf8');
      // Keep public/ in sync for the next dev session (Vite serves public/ as-is)
      fs.writeFileSync(path.join('public', 'robots.txt'), content, 'utf8');
    },
  };
}

export const previewServerOptions = {
  host: '0.0.0.0',
  port: 41737,
};