刘光辉
13 小时以前 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
/** @typedef {ReturnType<import('./registry.mjs').parseRegistry>} Registry */
/** @typedef {Registry['plugins'][number]} RegistryPlugin */
 
/** @param {Registry} registry @param {{ host: string, port: number }} options */
export function createGatewayConfig(registry, options) {
  /** @type {Record<string, { changeOrigin: boolean, target: string, ws?: boolean }>} */
  const proxy = {};
  for (const plugin of registry.plugins) {
    const httpTarget = `http://127.0.0.1:${plugin.devPort}`;
    proxy[`/onlyoffice-plugins/${registry.release}/${plugin.code}`] = { changeOrigin: true, target: httpTarget, ws: true };
  }
  proxy['/onlyoffice-sdk'] = { changeOrigin: true, target: `http://127.0.0.1:${registry.plugins[0].devPort}` };
  return /** @type {import('vite').UserConfig} */ ({
    appType: 'custom',
    server: { host: options.host, port: options.port, proxy, strictPort: true },
  });
}
 
/** @param {RegistryPlugin} plugin @param {{ gatewayPort: number, publicHost: string }} options */
export function createPluginEnvironment(plugin, options) {
  return {
    ONLYOFFICE_PLUGIN_DEV_HOST: '127.0.0.1',
    ONLYOFFICE_PLUGIN_DEV_PORT: String(plugin.devPort),
    ONLYOFFICE_PLUGIN_HMR_CLIENT_PORT: String(options.gatewayPort),
    ONLYOFFICE_PLUGIN_HMR_HOST: options.publicHost,
    ONLYOFFICE_PLUGIN_HMR_PATH: `/__hmr/${plugin.code}`,
    ONLYOFFICE_PLUGIN_PUBLIC_HOST: options.publicHost,
  };
}