2022-01-06 10:31:00 +01:00
|
|
|
import fs from 'fs';
|
|
|
|
import { IUnleashConfig } from '../server-impl';
|
|
|
|
import { rewriteHTML } from './rewriteHTML';
|
|
|
|
import path from 'path';
|
2022-04-28 10:57:52 +02:00
|
|
|
import fetch from 'make-fetch-happen';
|
2022-01-06 10:31:00 +01:00
|
|
|
|
|
|
|
export async function loadIndexHTML(
|
|
|
|
config: IUnleashConfig,
|
|
|
|
publicFolder: string,
|
|
|
|
): Promise<string> {
|
|
|
|
const { cdnPrefix, baseUriPath = '' } = config.server;
|
2022-12-07 13:31:27 +01:00
|
|
|
const uiFlags = JSON.stringify(config.ui.flags);
|
2022-01-06 10:31:00 +01:00
|
|
|
|
|
|
|
let indexHTML: string;
|
|
|
|
if (cdnPrefix) {
|
|
|
|
const res = await fetch(`${cdnPrefix}/index.html`);
|
|
|
|
indexHTML = await res.text();
|
|
|
|
} else {
|
|
|
|
indexHTML = fs
|
|
|
|
.readFileSync(path.join(publicFolder, 'index.html'))
|
|
|
|
.toString();
|
|
|
|
}
|
|
|
|
|
2022-12-07 13:31:27 +01:00
|
|
|
return rewriteHTML(indexHTML, baseUriPath, cdnPrefix, uiFlags);
|
2022-01-06 10:31:00 +01:00
|
|
|
}
|