刘光辉
12 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import type { Plugin } from 'vite';
 
import { Buffer } from 'node:buffer';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
 
import vue from '@vitejs/plugin-vue';
import { defineConfig, loadEnv } from 'vite';
 
import { onlyOfficeDevelopmentPlugin, resolveOnlyOfficeDevOptions } from './vite/onlyoffice-plugin';
 
const PLUGIN_GUID = 'asc.{7F4E8F35-5D66-47F8-A5B4-6AB3FAACF565}';
const PLUGIN_VERSION = '0.1.0';
const pluginRoot = fileURLToPath(new URL('.', import.meta.url));
 
function onlyOfficeEnvironment(environment: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
  return Object.fromEntries(Object.entries(environment).filter(([name]) => name.startsWith('ONLYOFFICE_')));
}
 
function productionAssets(): Plugin {
  let manifest: Record<string, unknown> | undefined;
  let icon: Buffer | undefined;
  let retinaIcon: Buffer | undefined;
 
  return {
    name: 'eln-production-assets',
    apply: 'build',
    buildStart() {
      const parsed = JSON.parse(readFileSync(path.resolve(pluginRoot, 'config.json'), 'utf8')) as Record<string, unknown>;
      const variations = parsed.variations;
 
      if (parsed.version !== PLUGIN_VERSION || parsed.guid !== PLUGIN_GUID) {
        throw new Error('ONLYOFFICE plugin version or GUID changed during the production build');
      }
      if (!Array.isArray(variations) || typeof variations[0] !== 'object' || variations[0] === null) {
        throw new Error('ONLYOFFICE plugin manifest has no production variation');
      }
 
      manifest = parsed;
      icon = readFileSync(path.resolve(pluginRoot, 'resources/icon.png'));
      retinaIcon = readFileSync(path.resolve(pluginRoot, 'resources/icon@2x.png'));
    },
    generateBundle() {
      if (!manifest || !icon || !retinaIcon) {
        throw new Error('Production assets were not initialized');
      }
 
      this.emitFile({ type: 'asset', fileName: 'config.json', source: `${JSON.stringify(manifest, null, 2)}\n` });
      this.emitFile({ type: 'asset', fileName: 'resources/icon.png', source: icon });
      this.emitFile({ type: 'asset', fileName: 'resources/icon@2x.png', source: retinaIcon });
      this.emitFile({ type: 'asset', fileName: 'translations/langs.json', source: '[]\n' });
    },
  };
}
 
export default defineConfig(({ command, mode }) => {
  const developmentEnvironment = {
    ...onlyOfficeEnvironment(loadEnv(mode, pluginRoot, '')),
    ...onlyOfficeEnvironment(process.env),
  };
  const development = command === 'serve' ? resolveOnlyOfficeDevOptions(developmentEnvironment) : undefined;
 
  return {
    base: development ? `${development.basePath}/` : './',
    plugins: [vue(), ...(development ? [onlyOfficeDevelopmentPlugin(development)] : [productionAssets()])],
    server: development
      ? {
          hmr: {
            clientPort: development.hmrClientPort,
            host: development.hmrHost,
            ...(development.hmrPath ? { path: development.hmrPath } : {}),
            protocol: development.hmrProtocol,
          },
          host: development.host,
          port: development.port,
          strictPort: true,
        }
      : undefined,
    root: pluginRoot,
    build: {
      sourcemap: false,
      rollupOptions: {
        input: path.resolve(pluginRoot, 'index.html'),
      },
    },
  };
});