Skip to content

Commit 9bbec5a

Browse files
committed
feat(mykarun): --includeJs for one-file html
1 parent 1cfd3d4 commit 9bbec5a

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

Tools/mykarun.nim

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,26 @@ proc exec(cmd: string) =
2727
if os.execShellCmd(cmd) != 0:
2828
quit "External command failed: " & cmd
2929

30+
const Buf = 1024
31+
proc extend(outf, inf: File) =
32+
var buf: array[Buf, uint8]
33+
while true:
34+
let n = inf.readBytes(buf, 0, Buf)
35+
if n == 0: break
36+
let wn = outf.writeBytes(buf, 0, n)
37+
assert n == wn
38+
proc extendNJsFileAndCompress(outf, inf: File) =
39+
var line: string
40+
while true:
41+
line = inf.readLine
42+
outf.writeLine line.strip(true, false, {' '}) # we know Nim current only use spaces for indentation
43+
if inf.endOfFile:
44+
break
45+
3046
proc build(ssr: bool, entry: string, rest: string, selectedCss: string, run: bool,
3147
watch: bool,
32-
jsDir, appName: string, htmlName=appName
48+
jsDir, appName: string, htmlName=appName, includeJs = false,
49+
release=false,
3350
) =
3451
echo("Building...")
3552
var cmd: string
@@ -48,10 +65,30 @@ proc build(ssr: bool, entry: string, rest: string, selectedCss: string, run: boo
4865
else:
4966
exec cmd
5067
let dest = htmlName & ".html"
51-
let script = if ssr:"" else: &"""<script type="text/javascript" src="{jsFp}"></script>""" # & (if watch: websocket else: "")
5268
if ssr:
5369
content = readFile(outTempPath)
54-
writeFile(dest, html % [if ssr: outHtmlName else:appName, selectedCss,content, script])
70+
if includeJs:
71+
defer: removeFile jsFp
72+
assert not ssr, "includeJs and ssr are exclusive"
73+
let parts = html.split("$4", 1)
74+
assert parts.len == 2
75+
var outF = open(dest, fmWrite)
76+
outF.write parts[0] % [appName, selectedCss, ""]
77+
outF.write"""<script type="text/javascript">"""
78+
outF.write '\n'
79+
let inF = open(jsFp)
80+
if release:
81+
outF.extendNJsFileAndCompress inf
82+
else:
83+
outF.extend inF
84+
inF.close()
85+
outF.write '\n'
86+
outF.write"""</script>"""
87+
outF.write parts[1]
88+
outF.close()
89+
else:
90+
let script = if ssr:"" else: &"""<script type="text/javascript" src="{jsFp}"></script>""" # & (if watch: websocket else: "")
91+
writeFile(dest, html % [if ssr: outHtmlName else:appName, selectedCss,content, script])
5592
if run: openDefaultBrowser("http://localhost:8080")
5693

5794
proc main =
@@ -62,6 +99,7 @@ proc main =
6299
appName = "app"
63100
htmlName = appName
64101
htmlNameGiven = false
102+
includeJs = false
65103
var file = ""
66104
var run = false
67105
var watch = false
@@ -86,6 +124,8 @@ proc main =
86124
appName = op.val
87125
of "jsDir":
88126
jsDir = op.val
127+
of "includeJs":
128+
includeJs = true
89129
of "run":
90130
run = true
91131
of "css":
@@ -123,6 +163,8 @@ proc main =
123163
htmlName = appName
124164
build(ssr, file, rest, selectedCss, run, watch,
125165
jsDir, appName, htmlName,
166+
includeJs,
167+
"-d:release" in rest # XXX: not exhaustive
126168
)
127169
# sync()
128170

0 commit comments

Comments
 (0)