@webwindowed/vite-plugin-cpp-header 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Serge Zaitsev
4
+ Copyright (c) 2022 Steffen André Langnes
5
+ Copyright (c) 2026 Laupetin
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
@@ -0,0 +1,10 @@
1
+ import { Plugin } from "vite";
2
+
3
+ //#region src/index.d.ts
4
+ interface CppHeaderPluginOptions {
5
+ outputPath?: string;
6
+ includeFileEnumeration?: boolean;
7
+ }
8
+ declare function pluginCppHeader(options?: CppHeaderPluginOptions): Plugin;
9
+ //#endregion
10
+ export { CppHeaderPluginOptions, pluginCppHeader as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,120 @@
1
+ import path from "node:path";
2
+ import fs from "node:fs";
3
+ //#region src/index.ts
4
+ const textEncoder = new TextEncoder();
5
+ function getPublicDirFiles(publicDir) {
6
+ if (!publicDir) return [];
7
+ const result = [];
8
+ const files = fs.readdirSync(publicDir, {
9
+ recursive: true,
10
+ withFileTypes: true
11
+ });
12
+ for (const file of files) {
13
+ if (!file.isFile()) continue;
14
+ const fullPath = path.join(file.parentPath, file.name);
15
+ let relativePath = path.relative(publicDir, fullPath).replaceAll(/\\/g, "/");
16
+ if (relativePath.startsWith("./")) relativePath = relativePath.substring(2);
17
+ result.push({
18
+ fullPath,
19
+ relativePath
20
+ });
21
+ }
22
+ return result;
23
+ }
24
+ function createVarName(fileName) {
25
+ return fileName.replaceAll(/[\\/]/g, "__").replaceAll(/[.-]/g, "_").toUpperCase();
26
+ }
27
+ function transformAsset(asset) {
28
+ const varName = createVarName(asset.fileName);
29
+ let buffer;
30
+ if (typeof asset.source === "string") buffer = textEncoder.encode(asset.source);
31
+ else buffer = asset.source;
32
+ return `constexpr const unsigned char ${varName}[] {${[...buffer].map((v) => String(v)).join(",")}};
33
+ `;
34
+ }
35
+ function transformChunk(chunk) {
36
+ return `constexpr const unsigned char ${createVarName(chunk.fileName)}[] {${[...textEncoder.encode(chunk.code)].map((v) => String(v)).join(",")}};
37
+ `;
38
+ }
39
+ function transformPublicFile(publicFile) {
40
+ return `constexpr const unsigned char ${createVarName(publicFile.relativePath)}[] {${[...fs.readFileSync(publicFile.fullPath)].map((v) => String(v)).join(",")}};
41
+ `;
42
+ }
43
+ function writeHeader(bundle, outputDir, options, publicDir, devServerPort) {
44
+ const outputPath = options?.outputPath ?? path.join(outputDir ?? "dist", "ViteAssets.h");
45
+ const outputPathParentDir = path.dirname(outputPath);
46
+ fs.mkdirSync(outputPathParentDir, { recursive: true });
47
+ const fd = fs.openSync(outputPath, "w");
48
+ const includeFileEnumeration = options?.includeFileEnumeration ?? true;
49
+ fs.writeSync(fd, `#pragma once
50
+
51
+ #include <webwindowed/plugin/asset_handler.hpp>
52
+
53
+ `);
54
+ if (includeFileEnumeration) fs.writeSync(fd, `#include <cstdlib>
55
+ #include <type_traits>
56
+
57
+ `);
58
+ fs.writeSync(fd, `constexpr auto VITE_DEV_SERVER = ${devServerPort ? "true" : "false"};
59
+ constexpr auto VITE_DEV_SERVER_PORT = ${devServerPort ? String(devServerPort) : "-1"};
60
+ `);
61
+ const fileNames = [];
62
+ for (const curBundle of Object.values(bundle)) {
63
+ if (curBundle.type === "asset") fs.writeSync(fd, transformAsset(curBundle));
64
+ else fs.writeSync(fd, transformChunk(curBundle));
65
+ fileNames.push(curBundle.fileName);
66
+ }
67
+ for (const publicDirFile of getPublicDirFiles(publicDir)) {
68
+ fs.writeSync(fd, transformPublicFile(publicDirFile));
69
+ fileNames.push(publicDirFile.relativePath);
70
+ }
71
+ if (includeFileEnumeration) {
72
+ fs.writeSync(fd, `
73
+ static inline const webwindowed::asset VITE_ASSETS[] {
74
+ `);
75
+ let index = 0;
76
+ for (const fileName of fileNames) {
77
+ const varName = createVarName(fileName);
78
+ let prefix = " ";
79
+ if (index > 0) prefix = `,
80
+ `;
81
+ fs.writeSync(fd, `${prefix}{ "${fileName}", ${varName}, std::extent_v<decltype(${varName})> }`);
82
+ index++;
83
+ }
84
+ fs.writeSync(fd, `
85
+ };
86
+ `);
87
+ fs.closeSync(fd);
88
+ }
89
+ }
90
+ function pluginCppHeader(options) {
91
+ let writeServerActive = false;
92
+ let writeBundleActive = false;
93
+ let publicDir = "public";
94
+ return {
95
+ name: "vite-plugin-cpp-header",
96
+ enforce: "post",
97
+ config(userOptions, env) {
98
+ if (env.command === "serve") writeServerActive = true;
99
+ else writeBundleActive = true;
100
+ if (typeof userOptions.publicDir === "string") publicDir = userOptions.publicDir;
101
+ else if (userOptions.publicDir === false) publicDir = void 0;
102
+ },
103
+ configureServer(server) {
104
+ if (!writeServerActive) return;
105
+ server.httpServer?.once("listening", () => {
106
+ writeHeader({ dummyfile: {
107
+ type: "chunk",
108
+ fileName: "dummyfile",
109
+ code: "dummy"
110
+ } }, server.config.build.outDir, options, publicDir, server.config.server.port);
111
+ });
112
+ },
113
+ writeBundle(outputOptions, bundle) {
114
+ if (!writeBundleActive) return;
115
+ writeHeader(bundle, outputOptions.dir, options, publicDir);
116
+ }
117
+ };
118
+ }
119
+ //#endregion
120
+ export { pluginCppHeader as default };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@webwindowed/vite-plugin-cpp-header",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "homepage": "https://github.com/Laupetin/webwindowed#readme",
6
+ "license": "MIT",
7
+ "bugs": {
8
+ "url": "https://github.com/Laupetin/webwindowed/issues"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/Laupetin/webwindowed.git",
13
+ "directory": "packages/vite-plugin-cpp-header"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "devDependencies": {
19
+ "@tsconfig/node24": "24.0.4",
20
+ "@types/node": "24.1.0",
21
+ "prettier": "3.8.4",
22
+ "rolldown": "1.1.1",
23
+ "tsdown": "0.22.2",
24
+ "typescript": "6.0.3",
25
+ "vite": "8.0.8"
26
+ },
27
+ "peerDependencies": {
28
+ "vite": ">=7.0.0"
29
+ },
30
+ "exports": {
31
+ ".": "./dist/index.mjs",
32
+ "./package.json": "./package.json"
33
+ },
34
+ "scripts": {
35
+ "build": "tsdown",
36
+ "format": "prettier --write .",
37
+ "lint": "prettier --check ."
38
+ }
39
+ }