Skip to content

Commit

Permalink
build: Minimize bindings in ui
Browse files Browse the repository at this point in the history
  • Loading branch information
lauriro committed Jun 14, 2024
1 parent d1f04e0 commit 5cce19b
Show file tree
Hide file tree
Showing 75 changed files with 373 additions and 576 deletions.
8 changes: 6 additions & 2 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ function viewMin(str, attrs) {
, stack = [-1]

str
.replace(/^([ \t]+)(;\w+:+)[ \t]+/gm, "$1$2")
.replace(/^([ \t]+)(;.*)\n\1(;.*)/gm, "$1$2$3")
.replace(/^([ \t]+)([\S]+)\n\1\s+(;.*)/gm, "$1$2 $3")
.replace(templateRe, work).trim()

//return out.join("\n")
Expand Down Expand Up @@ -528,7 +528,11 @@ function viewMin(str, attrs) {
stack.unshift(q)
} else {
if (op === "/") return
out[parent] += " ".repeat(indent.length) + all.slice(indent.length) + "\n"
all = all.slice(indent.length)
if (op === ";" || op === "@") all = all.replace(/("|')(?:\\\1|.)*?\1|\s*[\],;]\s*|\[\s+/g, function(all) {
return all.trim()
})
out[parent] += " ".repeat(indent.length) + all + "\n"
}
}
}
Expand Down
60 changes: 41 additions & 19 deletions test/build.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@


describe("build", function() {
require("../snapshot.js")
var build = require("../lib/build.js")
, cli = require("..")

require("../snapshot.js")

describe("file {0}", [
["build-cp", [], [/*"build/a.svg", "build/b.svg",*/ "build/c.svg"]],
["build-inline", ["lib1.css", "lib2.css"], ["lib1.css", "lib2.css"]],
["build-simple", [], []],
//["test/ui/dev.html", "test/ui/index.html", [
// "test/ui/lib1.css", "test/ui/lib2.css"
//]],
//["test/ui/dev.html", "test/ui/min/index.html", [
// "test/ui/lib1.css", "test/ui/lib2.css",
// "test/ui/min/manifest.json",
// "test/ui/min/image.svg"
//]]
], function(name, sameFolderFiles, buildFolderFiles) {
test("to {0}", [
[ "same folder", "", sameFolderFiles ],
[ "build folder", "build/", buildFolderFiles ],
], function(x, outDir, files, assert, mock) {
var temp = "test/build-temp-" + assert.i + "/"
, prefix = "test/data/snap/" + assert.i + "-"

var table = [
["test/ui/dev.html", "test/ui/index.html", [
"test/ui/lib1.css", "test/ui/lib2.css"
]],
["test/ui/dev.html", "test/ui/min/index.html", [
"test/ui/lib1.css", "test/ui/lib2.css",
"test/ui/min/manifest.json",
"test/ui/min/image.svg"
]]
]

it ("should minimize html", table, function(inFile, outFile, arr, assert) {
assert.planned = arr.length + 2
build({ _: [inFile], out: outFile })
assert.matchSnapshot(inFile)
assert.matchSnapshot(outFile)
arr.forEach(function(name) {
assert.matchSnapshot(name)
mock.swap(console, "error", mock.fn())
cli.mkdirp(temp + outDir)
cli.cp("test/data/" + name, temp)

assert.planned = files.length + 2

build({ _: [temp + "dev.html"], out: temp + outDir + "index.html" })
assert.cmdSnapshot("cd " + temp + ";find .|sort", prefix + "ls")
assert.matchSnapshot(temp + outDir + "index.html", 0, prefix + "index.html")
files.forEach(function(name) {
try {
assert.matchSnapshot(temp + name, 0, prefix + name)
} catch(e) {
assert(false, e)
}
})
cli.rmrf(temp)
})

})
})

4 changes: 4 additions & 0 deletions test/data/build-cp/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.a {
background: url("a.svg");
}

1 change: 1 addition & 0 deletions test/data/build-cp/a.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/data/build-cp/b.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/data/build-cp/c.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions test/data/build-cp/dev.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="a.css">
<style>
.b {
background: url("b.svg");
}
</style>
</head>
<body >
<img src="c.svg">
</body >
</html>

3 changes: 3 additions & 0 deletions test/data/build-inline/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.a {
top: 1px;
}
1 change: 1 addition & 0 deletions test/data/build-inline/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var a = 1;
48 changes: 48 additions & 0 deletions test/data/build-inline/dev.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="lib1.css" cat="
dummy/base.css
%/grid.css
" inline>
<link rel="stylesheet" href="a.css" cat drop="demo">
<style cat>
.b {
background: red;
/*url("data:image/svg+xml;base64,[data]");*/
}
</style>
<style>
.c {
background: blue;
}
</style>

<link rel="stylesheet" href="lib2.css" cat="
dummy/base.css
%/grid.css
" inline min>
<link rel="stylesheet" href="a.css" cat drop="demo">
<style cat>
.b {
background: red;
/*url("data:image/svg+xml;base64,[data]");*/
}
</style>
<style min>
.c {
background: blue;
}
</style>
</head>
<body >
<script src="lib1.js" cat="dummy/load.js" inline></script>
<script src="a.js" cat></script>
<script cat>var b = 2</script>
<script src="lib1.js" cat="dummy/load.js" inline min></script>
<script src="a.js" cat></script>
<script cat>var b = 2</script>
</body >
</html>

3 changes: 3 additions & 0 deletions test/data/build-simple/a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"a": 1
}
48 changes: 48 additions & 0 deletions test/data/build-simple/dev.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

<!DOCTYPE html>
<html>
<head>
<!-- Replace hash in min -->
<link rel="a" href=a.json>
<link rel="b" href=a.json min="b.json?{h}">
<link rel="c" href=a.json min="c-{h}.json">
</head>
<body>
<style>
.a {
top: 1px;
}
</style>
<style min>
.b {
top: 2px;
}
</style>
<script>
var a = 1
</script>
<script min>
var b = 2
</script>

<script type="ui">
%el a
div
;title!"b" ;n "N"; m "M"
;txt "a", "c"
@click [ 1, 2 ], "bo"
p {hi}
;txt "a"
</script>
<script type="ui" min>
%el b
div
;title!"b" ;n "N"; m "M"
;txt "a", "c"
@click [ 1, 2 ], "bo"
p {hi}
;txt "a"
</script>
</body >
</html>

5 changes: 5 additions & 0 deletions test/data/snap/1-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE html><html><head><link rel=stylesheet href=a.css><style>
.b {
background: url("b.svg");
}
</style></head><body><img src=c.svg></body></html>
7 changes: 7 additions & 0 deletions test/data/snap/1-ls.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.
./a.css
./a.svg
./b.svg
./c.svg
./dev.html
./index.html
1 change: 1 addition & 0 deletions test/data/snap/2-build/c.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions test/data/snap/2-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE html><html><head><link rel=stylesheet href=a.css><style>
.b {
background: url("b.svg");
}
</style></head><body><img src=c.svg></body></html>
10 changes: 10 additions & 0 deletions test/data/snap/2-ls.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.
./a.css
./a.svg
./b.svg
./build
./build/a.css
./build/c.svg
./build/index.html
./c.svg
./dev.html
39 changes: 39 additions & 0 deletions test/data/snap/3-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html><html><head><style>
.base {
color: red;
}


.grid {
display: block;
}

.a {
top: 1px;
}


.b {
background: red;
/*url("data:image/svg+xml;base64,[data]");*/
}
</style><style>
.c {
background: blue;
}
</style><style>
.base{color:red}
.grid{display:block}
.a{top:1px}
.b{background:red}
.c{background:blue}
</style></head><body><script>
var load = []


var a = 1;

var b = 2
</script><script>
var load=[],a=1,b=2
</script></body></html>
11 changes: 3 additions & 8 deletions test/ui/lib2.css → test/data/snap/3-lib1.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@
display: block;
}

.app {
margin: 0 auto;
.a {
top: 1px;
}
/*** demo ***
.demo {
color: red;
}
/**/


.logo {
.b {
background: red;
/*url("data:image/svg+xml;base64,[data]");*/
}
Expand Down
11 changes: 3 additions & 8 deletions test/ui/lib2.css.snap → test/data/snap/3-lib2.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@
display: block;
}

.app {
margin: 0 auto;
.a {
top: 1px;
}
/*** demo ***
.demo {
color: red;
}
/**/


.logo {
.b {
background: red;
/*url("data:image/svg+xml;base64,[data]");*/
}
Expand Down
8 changes: 8 additions & 0 deletions test/data/snap/3-ls.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.
./a.css
./a.js
./dev.html
./index.html
./lib1.css
./lib1.js
./lib2.css
39 changes: 39 additions & 0 deletions test/data/snap/4-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html><html><head><style>
.base {
color: red;
}


.grid {
display: block;
}

.a {
top: 1px;
}


.b {
background: red;
/*url("data:image/svg+xml;base64,[data]");*/
}
</style><style>
.c {
background: blue;
}
</style><style>
.base{color:red}
.grid{display:block}
.a{top:1px}
.b{background:red}
.c{background:blue}
</style></head><body><script>
var load = []


var a = 1;

var b = 2
</script><script>
var load=[],a=1,b=2
</script></body></html>
Loading

0 comments on commit 5cce19b

Please sign in to comment.