Skip to content

Commit b5429b1

Browse files
committed
Minimize inline js
1 parent b0ac91c commit b5429b1

20 files changed

Lines changed: 67 additions & 40 deletions

File tree

lib/build.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ build.cssMin = cssMin
101101
build.parseView = parseView
102102

103103
function clean(str) {
104+
try {
105+
return child.execSync(
106+
"uglifyjs --beautify beautify=false --",
107+
{ input: "[" + str + "]", stdio: ["pipe", "pipe", "pipe"] }
108+
).toString("utf8").trim().replace(/^\[/, "").replace(/\];?$/, "")
109+
} catch(e) {}
104110
return str.replace(/([$\w] [!$\w])|(["'`\/])(?:\\.|(?!\2).)*\2|\s/gi, (match, p, q) => {
105111
return p || q ? match : ""
106112
})
@@ -492,6 +498,10 @@ function viewMin(str, attrs) {
492498
, templateRe = /([ \t]*)(%?)((?:("|')(?:\\\4|.)*?\4|[-\w:.#[\]]=?)*)[ \t]*([>^;@|\\\/]|!?=|)(([\])}]?).*?([[({]?))(?=\x1f|\n|$)+/g
493499
, parent = 0
494500
, stack = [-1]
501+
, jsBlock = -1
502+
, jsBlockBuf = ""
503+
, jsBlockPrefix = ""
504+
, jsBlockIndent = ""
495505
, joinBindingsRe = /(\n +)([;^][^\n]+)\1(?=[;^])/g
496506
, joinSelectorRe = /(\n +)([\w:.#[](?:[^"' \n]|("|')(?:\\\3|(?!\3).)*?\3)*)\1 (?=[;^])/g
497507

@@ -513,8 +523,16 @@ function viewMin(str, attrs) {
513523
}
514524
stack.shift()
515525
}
526+
if (jsBlock >= 0 && q <= jsBlock) {
527+
jsBlockBuf += "\n" + all.slice(indent.length)
528+
out[parent] += jsBlockIndent + jsBlockPrefix + clean(jsBlockBuf) + "\n"
529+
jsBlock = -1
530+
return
531+
}
516532

517-
if (!isString(out[parent])) {
533+
if (jsBlock >= 0) {
534+
jsBlockBuf += "\n" + all.slice(indent.length)
535+
} else if (!isString(out[parent])) {
518536
out[parent]._j += all + "\n"
519537
} else if (plugin && (name === "todo def")) {
520538
out[parent] += all
@@ -529,8 +547,16 @@ function viewMin(str, attrs) {
529547
} else {
530548
if (op === "/") return
531549
all = all.slice(indent.length)
532-
if (op === ";" || op === "^" || op === "@") all = clean(all)
533-
out[parent] += " ".repeat(indent.length) + all + "\n"
550+
if (mapStart && (op === ";" || op === "^" || op === "@")) {
551+
jsBlock = q
552+
jsBlockIndent = " ".repeat(indent.length)
553+
name = all.lastIndexOf("function")
554+
jsBlockPrefix = name > 0 ? clean(all.slice(0, name)) + " " : clean(all.slice(0, all.lastIndexOf(mapStart)))
555+
jsBlockBuf = all.slice(name > 0 ? name : all.lastIndexOf(mapStart))
556+
} else {
557+
if (op === ";" || op === "^" || op === "@") all = clean(all)
558+
out[parent] += " ".repeat(indent.length) + all + "\n"
559+
}
534560
}
535561
}
536562
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"lib"
2020
],
2121
"scripts": {
22-
"test": "node ./cli.js t test/index.js"
22+
"test": "node test/index.js"
2323
},
2424
"dependencies": {
2525
"@litejs/dom": "26.4.0"

test/build.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ describe("build", function() {
1313
["build-inline"],
1414
["build-simple"],
1515
["build-ui-css"],
16-
["build-ui-min-js"],
17-
//["test/ui/dev.html", "test/ui/index.html", [
18-
// "test/ui/lib1.css", "test/ui/lib2.css"
19-
//]],
20-
//["test/ui/dev.html", "test/ui/min/index.html", [
21-
// "test/ui/lib1.css", "test/ui/lib2.css",
22-
// "test/ui/min/manifest.json",
23-
// "test/ui/min/image.svg"
24-
//]]
2516
], function(name) {
2617
test("to {0}", [
2718
[ "same folder", "" ],

test/data/build-ui-css/dev.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
<body>
99
<script src="app.js" min="app.min.js"></script>
1010
<script src="view.ui" type="ui" min></script>
11+
<script src="toggle-theme.ui" type="ui" min="btn.js"></script>
1112
</body>
1213
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
%css
2+
.Theme { color: red }
3+
4+
%js
5+
function toggleTheme() {}
6+
7+
%el toggleTheme
8+
button.Theme.reset
9+
@click function(e){
10+
var html = document.documentElement
11+
, name = "is-dark"
12+
, set = !El.hasClass(html, name)
13+
localStorage.theme = set ? "dark" : "x"
14+
El.css(html, "transition", "all .6s", 0, 999)
15+
El.cls(html, name, set)
16+
if (e.pageX) El.blur()
17+
}

test/data/build-ui-min-js/dev.html

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/data/build-ui-min-js/toggle-theme.ui

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/data/snap/10/build/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
function toggleTheme(){}xhr.css(".Theme{color:red}"),xhr.ui("%el toggleThemebutton.Theme Toggle")
1+
function toggleTheme(){}xhr.css(".Theme{color:red}"),xhr.ui('%el toggleThemebutton.Theme.reset @click function(e){var html=document.documentElement,name="is-dark",set=!El.hasClass(html,name);localStorage.theme=set?"dark":"x";El.css(html,"transition","all .6s",0,999);El.cls(html,name,set);if(e.pageX)El.blur()}')
22

test/data/snap/7/btn.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
function toggleTheme(){}xhr.ui('%el toggleThemebutton.Theme.reset @click function(e){var html=document.documentElement,name="is-dark",set=!El.hasClass(html,name);localStorage.theme=set?"dark":"x";El.css(html,"transition","all .6s",0,999);El.cls(html,name,set);if(e.pageX)El.blur()}')
2+

test/data/snap/7/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
.style{color:#ffeedd}
44
h1{content:url(data:image/jpeg;base64,iRxVB0);offset-path:url(#path);src:url(data:font/woff;base64,d09GRg)}
55
.View{color:red}
6-
</style></head><body><script src=app.min.js></script></body></html>
6+
.Theme{color:red}
7+
</style></head><body><script src=app.min.js></script><script src=btn.js type=ui></script></body></html>

0 commit comments

Comments
 (0)