mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-11-17 18:52:06 +00:00
chore: transform public dir files for cpp inclusion
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import type { Plugin } from "vite";
|
import type { Plugin, UserConfig } from "vite";
|
||||||
import type { OutputAsset, OutputChunk } from "rollup";
|
import type { OutputAsset, OutputChunk } from "rollup";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
@@ -7,8 +7,35 @@ type MinimalOutputAsset = Pick<OutputAsset, "type" | "fileName" | "source">;
|
|||||||
type MinimalOutputChunk = Pick<OutputChunk, "type" | "fileName" | "code">;
|
type MinimalOutputChunk = Pick<OutputChunk, "type" | "fileName" | "code">;
|
||||||
type MinimalOutputBundle = Record<string, MinimalOutputAsset | MinimalOutputChunk>;
|
type MinimalOutputBundle = Record<string, MinimalOutputAsset | MinimalOutputChunk>;
|
||||||
|
|
||||||
|
interface PublicDirFile {
|
||||||
|
fullPath: string;
|
||||||
|
relativePath: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPublicDirFiles(publicDir?: string): PublicDirFile[] {
|
||||||
|
if (!publicDir) return [];
|
||||||
|
|
||||||
|
const result: PublicDirFile[] = [];
|
||||||
|
const files = fs.readdirSync(publicDir, { recursive: true, withFileTypes: true });
|
||||||
|
for (const file of files) {
|
||||||
|
if (!file.isFile()) continue;
|
||||||
|
const fullPath = path.join(file.parentPath, file.name);
|
||||||
|
let relativePath = path.relative(publicDir, fullPath).replaceAll(/\\/g, "/");
|
||||||
|
if (relativePath.startsWith("./")) {
|
||||||
|
relativePath = relativePath.substring(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
result.push({
|
||||||
|
fullPath,
|
||||||
|
relativePath,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
function createVarName(fileName: string) {
|
function createVarName(fileName: string) {
|
||||||
return fileName.replaceAll(/[\.-]/g, "_").toUpperCase();
|
return fileName.replaceAll(/[\\/]/g, "__").replaceAll(/[\.-]/g, "_").toUpperCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
function transformAsset(asset: MinimalOutputAsset) {
|
function transformAsset(asset: MinimalOutputAsset) {
|
||||||
@@ -33,10 +60,19 @@ function transformChunk(chunk: MinimalOutputChunk) {
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function transformPublicFile(publicFile: PublicDirFile) {
|
||||||
|
const varName = createVarName(publicFile.relativePath);
|
||||||
|
const bytes = [...fs.readFileSync(publicFile.fullPath)].map((v) => String(v)).join(",");
|
||||||
|
|
||||||
|
return `constexpr const unsigned char ${varName}[] {${bytes}};
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
function writeHeader(
|
function writeHeader(
|
||||||
bundle: MinimalOutputBundle,
|
bundle: MinimalOutputBundle,
|
||||||
outputDir?: string,
|
outputDir?: string,
|
||||||
options?: HeaderTransformationPluginOptions,
|
options?: HeaderTransformationPluginOptions,
|
||||||
|
publicDir?: string,
|
||||||
devServerPort?: number,
|
devServerPort?: number,
|
||||||
) {
|
) {
|
||||||
const outputPath = options?.outputPath ?? path.join(outputDir ?? "dist", "ViteAssets.h");
|
const outputPath = options?.outputPath ?? path.join(outputDir ?? "dist", "ViteAssets.h");
|
||||||
@@ -71,12 +107,18 @@ constexpr auto VITE_DEV_SERVER_PORT = ${devServerPort ? String(devServerPort) :
|
|||||||
`,
|
`,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const fileNames: string[] = [];
|
||||||
for (const curBundle of Object.values(bundle)) {
|
for (const curBundle of Object.values(bundle)) {
|
||||||
if (curBundle.type === "asset") {
|
if (curBundle.type === "asset") {
|
||||||
fs.writeSync(fd, transformAsset(curBundle));
|
fs.writeSync(fd, transformAsset(curBundle));
|
||||||
} else {
|
} else {
|
||||||
fs.writeSync(fd, transformChunk(curBundle));
|
fs.writeSync(fd, transformChunk(curBundle));
|
||||||
}
|
}
|
||||||
|
fileNames.push(curBundle.fileName);
|
||||||
|
}
|
||||||
|
for (const publicDirFile of getPublicDirFiles(publicDir)) {
|
||||||
|
fs.writeSync(fd, transformPublicFile(publicDirFile));
|
||||||
|
fileNames.push(publicDirFile.relativePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (includeFileEnumeration) {
|
if (includeFileEnumeration) {
|
||||||
@@ -95,8 +137,7 @@ static inline const UiFile MOD_MAN_UI_FILES[] {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let index = 0;
|
let index = 0;
|
||||||
for (const curBundle of Object.values(bundle)) {
|
for (const fileName of fileNames) {
|
||||||
const fileName = curBundle.fileName;
|
|
||||||
const varName = createVarName(fileName);
|
const varName = createVarName(fileName);
|
||||||
|
|
||||||
let prefix = " ";
|
let prefix = " ";
|
||||||
@@ -133,16 +174,21 @@ export default function headerTransformationPlugin(
|
|||||||
): Plugin {
|
): Plugin {
|
||||||
let writeServerActive = false;
|
let writeServerActive = false;
|
||||||
let writeBundleActive = false;
|
let writeBundleActive = false;
|
||||||
|
let publicDir: string | undefined = undefined;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: "vite-plugin-header-transformation",
|
name: "vite-plugin-header-transformation",
|
||||||
enforce: "post",
|
enforce: "post",
|
||||||
config(_userOptions, env) {
|
config(userOptions: UserConfig, env) {
|
||||||
if (env.command === "serve") {
|
if (env.command === "serve") {
|
||||||
writeServerActive = true;
|
writeServerActive = true;
|
||||||
} else {
|
} else {
|
||||||
writeBundleActive = true;
|
writeBundleActive = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof userOptions.publicDir === "string") {
|
||||||
|
publicDir = userOptions.publicDir;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
configureServer(server) {
|
configureServer(server) {
|
||||||
if (!writeServerActive) {
|
if (!writeServerActive) {
|
||||||
@@ -156,6 +202,7 @@ export default function headerTransformationPlugin(
|
|||||||
},
|
},
|
||||||
server.config.build.outDir,
|
server.config.build.outDir,
|
||||||
options,
|
options,
|
||||||
|
publicDir,
|
||||||
server.config.server.port,
|
server.config.server.port,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -165,7 +212,7 @@ export default function headerTransformationPlugin(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
writeHeader(bundle, outputOptions.dir, options);
|
writeHeader(bundle, outputOptions.dir, options, publicDir);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user