@@ -6,19 +6,28 @@ import * as cheerio from "cheerio";
6
6
import { copyFile , mkdir , readFile , writeFile } from "node:fs/promises" ;
7
7
import { dirname , relative , resolve } from "node:path" ;
8
8
import { parseArgs } from "node:util" ;
9
+ import { fileURLToPath } from "node:url" ;
9
10
10
11
import { commentMarker , componentSelector , returnIfDefined } from "./helpers" ;
11
12
13
+ const RUNTIME_FILE = "define-html.js" ;
14
+
12
15
const options = {
13
16
outdir : {
14
17
type : "string" ,
15
18
short : "o" ,
16
19
default : "./dist" ,
17
20
} ,
21
+ remote : {
22
+ type : "boolean" ,
23
+ short : "r" ,
24
+ default : false ,
25
+ } ,
18
26
} as const ;
19
27
20
28
const { values } = parseArgs ( { args : process . argv . slice ( 2 ) , options } ) ;
21
29
30
+ const COPY_RUNTIME = ! returnIfDefined ( values . remote ) ;
22
31
const DIST_DIR = returnIfDefined ( values . outdir ) ;
23
32
24
33
const files = await fg ( "**/*.html" , {
@@ -52,6 +61,11 @@ for (const path of files) {
52
61
53
62
$ ( componentSelector ) . remove ( ) ;
54
63
64
+ if ( COPY_RUNTIME ) {
65
+ const runtime_path = relative ( dirname ( path ) , RUNTIME_FILE ) ;
66
+ $ ( `script[src$='define-html']` ) . attr ( "src" , runtime_path ) ;
67
+ }
68
+
55
69
const out = await prepareSave ( path ) ;
56
70
await writeFile ( out , $ . html ( ) ) ;
57
71
}
@@ -71,6 +85,12 @@ for (const asset of assets) {
71
85
await copyFile ( asset , out ) ;
72
86
}
73
87
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
+
74
94
console . log ( "Done" ) ;
75
95
76
96
async function prepareSave ( path : string ) {
0 commit comments