Skip to content

Commit 3104cca

Browse files
author
infodusha
committed
feat(compiler): add runtime copy
1 parent 717669c commit 3104cca

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ This project uses [Bun](https://bun.sh) as a build tool and package manager.
7070

7171
Improve compiler:
7272

73-
* Copy define-html script itself
7473
* Compile css
7574
* Compile scripts
7675

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"watch": "bun build ./src/index.ts --outdir ./dist --watch",
1313
"build": "bun build ./src/index.ts --outdir ./dist",
1414
"build:bin": "bun build ./src/bin.ts --target node --outdir ./dist",
15-
"compile": "cd example && bun --bun ../src/bin.ts",
15+
"compile": "cd example && bun ../src/bin.ts --remote",
1616
"lint": "bunx biome lint",
1717
"test": "playwright test",
1818
"test:local": "playwright test --ui"

src/bin.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,28 @@ import * as cheerio from "cheerio";
66
import { copyFile, mkdir, readFile, writeFile } from "node:fs/promises";
77
import { dirname, relative, resolve } from "node:path";
88
import { parseArgs } from "node:util";
9+
import { fileURLToPath } from "node:url";
910

1011
import { commentMarker, componentSelector, returnIfDefined } from "./helpers";
1112

13+
const RUNTIME_FILE = "define-html.js";
14+
1215
const options = {
1316
outdir: {
1417
type: "string",
1518
short: "o",
1619
default: "./dist",
1720
},
21+
remote: {
22+
type: "boolean",
23+
short: "r",
24+
default: false,
25+
},
1826
} as const;
1927

2028
const { values } = parseArgs({ args: process.argv.slice(2), options });
2129

30+
const COPY_RUNTIME = !returnIfDefined(values.remote);
2231
const DIST_DIR = returnIfDefined(values.outdir);
2332

2433
const files = await fg("**/*.html", {
@@ -52,6 +61,11 @@ for (const path of files) {
5261

5362
$(componentSelector).remove();
5463

64+
if (COPY_RUNTIME) {
65+
const runtime_path = relative(dirname(path), RUNTIME_FILE);
66+
$(`script[src$='define-html']`).attr("src", runtime_path);
67+
}
68+
5569
const out = await prepareSave(path);
5670
await writeFile(out, $.html());
5771
}
@@ -71,6 +85,12 @@ for (const asset of assets) {
7185
await copyFile(asset, out);
7286
}
7387

88+
if (COPY_RUNTIME) {
89+
const main = resolve(dirname(fileURLToPath(import.meta.url)), "index.js");
90+
await copyFile(main, resolve(DIST_DIR, RUNTIME_FILE));
91+
console.log("Runtime copied");
92+
}
93+
7494
console.log("Done");
7595

7696
async function prepareSave(path: string) {

0 commit comments

Comments
 (0)