mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 10:53:31 +00:00
Emit runtime asset manifest as ESM module
This commit is contained in:
@@ -141,11 +141,15 @@ export function copyRootPublicFiles(
|
||||
}
|
||||
}
|
||||
|
||||
export function writePublicAssetManifestFile(
|
||||
export function writePublicAssetManifestModule(
|
||||
outDir: string,
|
||||
assetManifest: AssetManifest,
|
||||
): void {
|
||||
const manifestPath = path.join(outDir, "_assets", "asset-manifest.json");
|
||||
const manifestPath = path.join(outDir, "_assets", "asset-manifest.mjs");
|
||||
fs.mkdirSync(path.dirname(manifestPath), { recursive: true });
|
||||
fs.writeFileSync(manifestPath, `${JSON.stringify(assetManifest, null, 2)}\n`);
|
||||
const serializedManifest = JSON.stringify(assetManifest, null, 2);
|
||||
fs.writeFileSync(
|
||||
manifestPath,
|
||||
`const assetManifest = ${serializedManifest};\nexport { assetManifest };\nexport default assetManifest;\n`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,18 +1,25 @@
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import { fileURLToPath, pathToFileURL } from "url";
|
||||
import type { AssetManifest } from "../core/AssetUrls";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const staticDir = path.join(__dirname, "../../static");
|
||||
const manifestPath = path.join(staticDir, "_assets", "asset-manifest.json");
|
||||
const manifestPath = path.join(staticDir, "_assets", "asset-manifest.mjs");
|
||||
|
||||
let manifestPromise: Promise<AssetManifest> | null = null;
|
||||
let manifestVersion = 0;
|
||||
|
||||
async function readRuntimeAssetManifest(): Promise<AssetManifest> {
|
||||
const raw = await fs.readFile(manifestPath, "utf8");
|
||||
return JSON.parse(raw) as AssetManifest;
|
||||
async function importRuntimeAssetManifest(
|
||||
version: number,
|
||||
): Promise<AssetManifest> {
|
||||
const manifestModule = (await import(
|
||||
`${pathToFileURL(manifestPath).href}?v=${version}`
|
||||
)) as {
|
||||
assetManifest?: AssetManifest;
|
||||
default?: AssetManifest;
|
||||
};
|
||||
return manifestModule.assetManifest ?? manifestModule.default ?? {};
|
||||
}
|
||||
|
||||
export async function getRuntimeAssetManifest(): Promise<AssetManifest> {
|
||||
@@ -20,10 +27,11 @@ export async function getRuntimeAssetManifest(): Promise<AssetManifest> {
|
||||
return {};
|
||||
}
|
||||
|
||||
manifestPromise ??= readRuntimeAssetManifest();
|
||||
manifestPromise ??= importRuntimeAssetManifest(manifestVersion);
|
||||
return manifestPromise;
|
||||
}
|
||||
|
||||
export function clearRuntimeAssetManifestCache(): void {
|
||||
manifestVersion++;
|
||||
manifestPromise = null;
|
||||
}
|
||||
|
||||
+2
-2
@@ -11,7 +11,7 @@ import {
|
||||
copyRootPublicFiles,
|
||||
createHashedPublicAssetFiles,
|
||||
getResourcesDir,
|
||||
writePublicAssetManifestFile,
|
||||
writePublicAssetManifestModule,
|
||||
} from "./src/server/PublicAssetManifest";
|
||||
|
||||
// Vite already handles these, but its good practice to define them explicitly
|
||||
@@ -44,7 +44,7 @@ export default defineConfig(({ mode }) => {
|
||||
closeBundle() {
|
||||
const outDir = path.join(__dirname, "static");
|
||||
copyRootPublicFiles(resourcesDir, outDir);
|
||||
writePublicAssetManifestFile(outDir, assetManifest);
|
||||
writePublicAssetManifestModule(outDir, assetManifest);
|
||||
createHashedPublicAssetFiles(resourcesDir, outDir, assetManifest);
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user