Skip to content

Commit c960ce8

Browse files
committed
Add test for inline code extractions
1 parent 41de2ed commit c960ce8

7 files changed

Lines changed: 76 additions & 4 deletions

File tree

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
dist/
2+
test/**/expected
3+
test/**/output

src/staticbuild.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,11 @@ function collectInlineCodeFromDocument(
411411

412412
for (const element of document.querySelectorAll("style, script:not([src])")) {
413413
if (element.hasAttribute("sb:buildtime")) continue
414-
if (element.hasAttribute("sb:ignore")) continue
414+
415+
if (element.hasAttribute("sb:ignore")) {
416+
element.removeAttribute("sb:ignore")
417+
continue
418+
}
415419

416420
const textContent = element.textContent
417421
element.remove()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
body {
3+
background: red;
4+
}
5+
6+
7+
body {
8+
background: green;
9+
}
10+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(function() {
2+
console.log("Hello")
3+
})();
4+
(function() {
5+
console.log("World")
6+
})();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
4+
5+
6+
7+
8+
9+
<script>
10+
console.log("Don't extract me!")
11+
</script>
12+
13+
<style>
14+
body {
15+
content: "Don't extract me either!"
16+
}
17+
</style>
18+
<link rel="stylesheet" href="/assets/css/cd0d4bbfe756b191d9df2f498713d3b1.css"/ ><script src="/assets/js/5afe3ad231347777bcd28103902f31db.js" async defer></script>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<script>
2+
console.log("Hello")
3+
</script>
4+
5+
<script>
6+
console.log("World")
7+
</script>
8+
9+
<style>
10+
body {
11+
background: red;
12+
}
13+
</style>
14+
15+
<style>
16+
body {
17+
background: green;
18+
}
19+
</style>
20+
21+
<script sb:ignore>
22+
console.log("Don't extract me!")
23+
</script>
24+
25+
<style sb:ignore>
26+
body {
27+
content: "Don't extract me either!"
28+
}
29+
</style>

test/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ function expectFilesEqual(actualFilePath: string, expectedFilePath: string) {
1818
expectFileExists(actualFilePath)
1919
expectFileExists(expectedFilePath)
2020

21-
const actualContents = fs.readFileSync(actualFilePath, "utf8")
22-
const expectedContents = fs.readFileSync(expectedFilePath, "utf8")
21+
// @TODO: These are trimmed to avoid checking for newline differences at the
22+
// end of a file, but actually it might be nice to actually add a newline
23+
// when generating to be good computer citizens.
24+
const actualContents = fs.readFileSync(actualFilePath, "utf8").trim()
25+
const expectedContents = fs.readFileSync(expectedFilePath, "utf8").trim()
2326

2427
let isDifferent: boolean = false
2528

@@ -124,7 +127,7 @@ async function test() {
124127
stdout.write(`\x1b[38;2;0;255;0m[pass]\x1b[0m ${directory.name} \n`)
125128
} catch (err: unknown) {
126129
stdout.write(`\x1b[38;2;255;0;0m[fail]\x1b[0m ${directory.name} \n`)
127-
break
130+
throw err
128131
}
129132

130133
for (const [name, time] of Object.entries(timings)) {

0 commit comments

Comments
 (0)