create-hackhub-mod 0.7.0 → 0.8.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/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { build, context } from "esbuild";
|
|
1
|
+
import { build, context, Plugin } from "esbuild";
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
|
|
@@ -25,6 +25,40 @@ function prepareDist() {
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Scans HTML files for local asset references (script src, link href, img src)
|
|
30
|
+
* and copies them into dist/ so they're served via mod-asset:// at runtime.
|
|
31
|
+
*/
|
|
32
|
+
function htmlAssetsPlugin(): Plugin {
|
|
33
|
+
const assetPattern = /(?:src|href)\s*=\s*["'](\.[^"']+)["']/g;
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
name: "html-assets",
|
|
37
|
+
setup(build) {
|
|
38
|
+
build.onLoad({ filter: /\.html$/ }, (args) => {
|
|
39
|
+
const html = fs.readFileSync(args.path, "utf-8");
|
|
40
|
+
const htmlDir = path.dirname(args.path);
|
|
41
|
+
let match: RegExpExecArray | null;
|
|
42
|
+
|
|
43
|
+
while ((match = assetPattern.exec(html)) !== null) {
|
|
44
|
+
const ref = match[1];
|
|
45
|
+
if (ref.startsWith("./") || !ref.startsWith("/")) {
|
|
46
|
+
const absPath = path.resolve(htmlDir, ref);
|
|
47
|
+
if (fs.existsSync(absPath)) {
|
|
48
|
+
const rel = ref.startsWith("./") ? ref.slice(2) : ref;
|
|
49
|
+
const dest = path.join("dist", rel);
|
|
50
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
51
|
+
fs.copyFileSync(absPath, dest);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return { contents: html, loader: "text" };
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
28
62
|
const config = {
|
|
29
63
|
entryPoints: ["src/index.ts"],
|
|
30
64
|
bundle: true,
|
|
@@ -36,6 +70,7 @@ const config = {
|
|
|
36
70
|
loader: {
|
|
37
71
|
".html": "text" as const,
|
|
38
72
|
},
|
|
73
|
+
plugins: [htmlAssetsPlugin()],
|
|
39
74
|
};
|
|
40
75
|
|
|
41
76
|
async function main() {
|