mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-10-15 11:09:02 +00:00
chore: implement custom url handler for edge to serve assets
This commit is contained in:
@@ -1,21 +1,12 @@
|
||||
import type { Plugin, ViteDevServer } from "vite";
|
||||
import type { OutputOptions, OutputBundle, OutputAsset, OutputChunk } from "rollup";
|
||||
import { createHmac } from "node:crypto";
|
||||
import path from "node:path";
|
||||
import fs from "node:fs";
|
||||
|
||||
function createTransformedTextSource2(varName: string, previousSource: string) {
|
||||
const hash = createHmac("sha256", previousSource);
|
||||
const digest = hash.digest("hex").substring(0, 16);
|
||||
|
||||
return `#pragma once
|
||||
|
||||
static inline const char* ${varName} = R"${digest}(${previousSource})${digest}";
|
||||
`;
|
||||
}
|
||||
|
||||
function createTransformedTextSource(varName: string, previousSource: string) {
|
||||
const str = [...previousSource].map((v) => `0x${v.charCodeAt(0).toString(16).padStart(2, "0")}`).join(", ");
|
||||
const str = [...previousSource]
|
||||
.map((v) => `0x${v.charCodeAt(0).toString(16).padStart(2, "0")}`)
|
||||
.join(", ");
|
||||
return `#pragma once
|
||||
|
||||
static inline const unsigned char ${varName}[] {
|
||||
@@ -33,8 +24,6 @@ function transformAsset(asset: OutputAsset) {
|
||||
|
||||
if (typeof asset.source === "string") {
|
||||
asset.source = createTransformedTextSource(varName, asset.source);
|
||||
return `${varName}, std::extent_v<decltype(${varName})>`;
|
||||
// return `${varName}, std::char_traits<char>::length(${varName})`;
|
||||
} else {
|
||||
const str = [...asset.source].map((v) => `0x${v.toString(16).padStart(2, "0")}`).join(", ");
|
||||
asset.source = `#pragma once
|
||||
@@ -43,15 +32,15 @@ static inline const unsigned char ${varName}[] {
|
||||
${str}
|
||||
};
|
||||
`;
|
||||
return `${varName}, std::extent_v<decltype(${varName})>`;
|
||||
}
|
||||
|
||||
return varName;
|
||||
}
|
||||
|
||||
function transformChunk(chunk: OutputChunk) {
|
||||
const varName = createVarName(chunk.fileName);
|
||||
chunk.code = createTransformedTextSource(varName, chunk.code);
|
||||
return `${varName}, std::extent_v<decltype(${varName})>`;
|
||||
// return `${varName}, std::char_traits<char>::length(${varName})`;
|
||||
return varName;
|
||||
}
|
||||
|
||||
export function headerTransformationPlugin(): Plugin {
|
||||
@@ -59,30 +48,35 @@ export function headerTransformationPlugin(): Plugin {
|
||||
name: "header-transformation",
|
||||
apply: "build",
|
||||
config(config) {
|
||||
config.base = "http://modman-resource/";
|
||||
config.base = "http://modman";
|
||||
},
|
||||
generateBundle(options: OutputOptions, bundle: OutputBundle, isWrite: boolean) {
|
||||
const includesStr: string[] = [];
|
||||
const uiFilesStr: string[] = [];
|
||||
const includesStr: string[] = [`#include "index.html.h"`];
|
||||
const uiFilesStr: string[] = [
|
||||
`{ "index.html", { "index.html", INDEX_HTML, std::extent_v<decltype(INDEX_HTML)> } }`,
|
||||
];
|
||||
|
||||
for (const curBundle of Object.values(bundle)) {
|
||||
let varStr: string;
|
||||
let varName: string;
|
||||
if (curBundle.type === "asset") {
|
||||
varStr = transformAsset(curBundle);
|
||||
varName = transformAsset(curBundle);
|
||||
} else {
|
||||
varStr = transformChunk(curBundle);
|
||||
varName = transformChunk(curBundle);
|
||||
}
|
||||
|
||||
includesStr.push(`#include "${curBundle.fileName}.h"`);
|
||||
uiFilesStr.push(`{ "${curBundle.fileName}", { "${curBundle.fileName}", ${varStr} } }`);
|
||||
uiFilesStr.push(
|
||||
`{ "${curBundle.fileName}", { "${curBundle.fileName}", ${varName}, std::extent_v<decltype(${varName})> } }`,
|
||||
);
|
||||
|
||||
curBundle.fileName = `${curBundle.fileName}.h`;
|
||||
}
|
||||
|
||||
this.emitFile({
|
||||
type: "asset",
|
||||
fileName: "modmanui.h",
|
||||
source: `#pragma once
|
||||
|
||||
#include "index.html.h"
|
||||
${includesStr.join("\n")}
|
||||
|
||||
#include <string>
|
||||
@@ -111,9 +105,8 @@ ${uiFilesStr.join(",\n")}
|
||||
chunk?: OutputChunk;
|
||||
},
|
||||
) {
|
||||
html = html
|
||||
.replaceAll("index.js.h", "index.js")
|
||||
.replaceAll("http://modman-resource/", "modman-resource://");
|
||||
html = html.replaceAll("index.js.h", "index.js");
|
||||
// .replaceAll("http://modman-resource/", "modman-resource://");
|
||||
|
||||
html = createTransformedTextSource(createVarName("index.html"), html);
|
||||
ctx.filename = `${ctx.filename}.h`;
|
||||
@@ -128,24 +121,6 @@ ${uiFilesStr.join(",\n")}
|
||||
curBundle.fileName += ".h";
|
||||
}
|
||||
}
|
||||
|
||||
this.emitFile({
|
||||
type: "asset",
|
||||
fileName: "index.h",
|
||||
source: `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<script src="" type="module"></script>
|
||||
<script src="" type="module"></script>
|
||||
<script src="" type="module"></script>
|
||||
</body>
|
||||
</html>`,
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user