aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/config/backendConfigVitePlugin.ts
blob: 3bffce3ab8a5ccd5ac679075e8841842c0a20065 (plain) (blame)
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
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import type { PluginOption } from 'vite';

import type BackendConfig from '../src/xtext/BackendConfig';
import { ENDPOINT } from '../src/xtext/BackendConfig';

export default function backendConfigVitePlugin(
  backendConfig: BackendConfig,
): PluginOption {
  return {
    name: 'backend-config',
    apply: 'serve',
    configureServer(server) {
      const config = JSON.stringify(backendConfig);
      server.middlewares.use((req, res, next) => {
        if (req.url === `/${ENDPOINT}`) {
          res.setHeader('Content-Type', 'application/json');
          res.end(config);
        } else {
          next();
        }
      });
    },
  };
}

export type { default as BackendConfig } from '../src/xtext/BackendConfig';
export { ENDPOINT as CONFIG_ENDPOINT } from '../src/xtext/BackendConfig';