snow-editor

small markdown and org-mode editor
Log | Files | Refs | README

vite.shared.js (1685B)


      1 import fs from 'fs';
      2 import path from 'path';
      3 import { loadEnv } from 'vite';
      4 
      5 export function parseAllowedHosts(value) {
      6   if (!value || value.trim() === '') {
      7     return ['localhost', '127.0.0.1'];
      8   }
      9   if (value.trim() === '*') {
     10     return true;
     11   }
     12   return value
     13     .split(',')
     14     .map((host) => host.trim())
     15     .filter(Boolean);
     16 }
     17 
     18 export function resolveAllowedHosts(mode) {
     19   const fileEnv = loadEnv(mode, process.cwd(), '');
     20   return parseAllowedHosts(process.env.ALLOWED_HOSTS ?? fileEnv.ALLOWED_HOSTS);
     21 }
     22 
     23 export function resolveAllowSearchIndexing(mode) {
     24   const fileEnv = loadEnv(mode, process.cwd(), '');
     25   const value = (
     26     process.env.VITE_ALLOW_SEARCH_INDEXING ?? fileEnv.VITE_ALLOW_SEARCH_INDEXING ?? ''
     27   )
     28     .trim()
     29     .toLowerCase();
     30   return value === 'true';
     31 }
     32 
     33 export function robotsSeoPlugin(allowIndexing) {
     34   let outDir = 'dist';
     35 
     36   return {
     37     name: 'snow-robots-seo',
     38     configResolved(config) {
     39       outDir = config.build.outDir;
     40     },
     41     transformIndexHtml(html) {
     42       if (allowIndexing) return html;
     43       return html.replace(
     44         '</head>',
     45         '    <meta name="robots" content="noindex, nofollow" />\n  </head>',
     46       );
     47     },
     48     closeBundle() {
     49       const content = allowIndexing
     50         ? 'User-agent: *\nAllow: /\n'
     51         : 'User-agent: *\nDisallow: /\n';
     52       const robotsPath = path.join(outDir, 'robots.txt');
     53       fs.writeFileSync(robotsPath, content, 'utf8');
     54       // Keep public/ in sync for the next dev session (Vite serves public/ as-is)
     55       fs.writeFileSync(path.join('public', 'robots.txt'), content, 'utf8');
     56     },
     57   };
     58 }
     59 
     60 export const previewServerOptions = {
     61   host: '0.0.0.0',
     62   port: 41737,
     63 };