Skip to content

Commit

Permalink
Fixes #33 Fixes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 2, 2022
1 parent 51e00ae commit 38a2914
Show file tree
Hide file tree
Showing 19 changed files with 163 additions and 29 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
},
"dependencies": {
"@11ty/eleventy": "^2.0.0-canary.18",
"@11ty/webc": "^0.7.1"
"@11ty/webc": "^0.7.1",
"dependency-graph": "^0.11.0"
},
"devDependencies": {
"ava": "^5.0.1"
Expand Down
23 changes: 22 additions & 1 deletion src/eleventyWebcTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ module.exports = function(eleventyConfig, options = {}) {
});

return async (data) => {
let setupObject = { data };
let setupObject = {
data,
};
if(data.webc?.components) {
let WebC = incremental.webc;
setupObject.components = WebC.getComponentsMap(relativePath(data.page.inputPath, data.webc.components));
Expand All @@ -147,22 +149,41 @@ module.exports = function(eleventyConfig, options = {}) {
let { ast, serializer } = setup;
let { html, css, js, buckets } = await serializer.compile(ast);

let hasAssets = false;
cssManager.addToPage(data.page.url, css, "default");
if(css.length > 0) {
hasAssets = true;
}

if(buckets.css) {
for(let bucket in buckets.css) {
cssManager.addToPage(data.page.url, buckets.css[bucket], bucket);
if(buckets.css[bucket] && buckets.css[bucket].length > 0) {
hasAssets = true;
}
}
}

jsManager.addToPage(data.page.url, js, "default");
if(js.length > 0) {
hasAssets = true;
}

if(buckets.js) {
for(let bucket in buckets.js) {
jsManager.addToPage(data.page.url, buckets.js[bucket], bucket);
if(buckets.js[bucket] && buckets.js[bucket].length > 0) {
hasAssets = true;
}
}
}

// Limit two pass compile for outermost layouts *only* that have JS/CSS assets
if(hasAssets && incremental.isOutermostLayoutInChain(inputPath)) {
let {html} = await serializer.compile(ast);
return html;
}

return html;
};
}
Expand Down
36 changes: 29 additions & 7 deletions src/incremental.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { DepGraph } = require("dependency-graph");

class WebCIncremental {
constructor() {
this.pages = {};
Expand All @@ -10,6 +12,32 @@ class WebCIncremental {

setLayouts(layouts) {
this.layouts = layouts;

let graph = new DepGraph();
for(let layout in layouts) {
if(!graph.hasNode(layout)) {
graph.addNode(layout);
}
for(let child of layouts[layout]) {
if(!layouts[child]) {
// not a layout
continue;
}
if(!graph.hasNode(child)) {
graph.addNode(child);
}
graph.addDependency(child, layout);
}
}

this.layoutDependencyGraph = graph;
}

isOutermostLayoutInChain(layoutFilePath) {
if(this.layouts && this.layouts[layoutFilePath]) {
return this.layoutDependencyGraph.dependenciesOf(layoutFilePath).length === 0;
}
return false;
}

isFileUsingLayout(templateFilePath, layoutFilePath) {
Expand All @@ -23,13 +51,7 @@ class WebCIncremental {
let WebC = this.webc;
let page = new WebC();

// Explicitly disable bundler mode for layouts
if(this.layouts && this.layouts[inputPath]) {
page.setBundlerMode(false);
} else {
page.setBundlerMode(true);
}

page.setBundlerMode(true);
page.setContent(inputContent, inputPath);

this.pages[inputPath] = page;
Expand Down
2 changes: 2 additions & 0 deletions test/components-in-layouts/_components/inner.webc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Test
<style>/* Inner CSS */</style>
2 changes: 2 additions & 0 deletions test/components-in-layouts/_components/outer.webc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Test
<style>/* Outer CSS */</style>
14 changes: 14 additions & 0 deletions test/components-in-layouts/_includes/base.webc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<title></title>
<style webc:keep @raw="getCss(page.url)"></style>
</head>
<body>
<template webc:nokeep @raw="content"></template>
<outer></outer>
</body>
</html>
5 changes: 5 additions & 0 deletions test/components-in-layouts/_includes/layout.webc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
layout: base.webc
---
<template webc:nokeep @raw="content"></template>
<inner></inner>
13 changes: 13 additions & 0 deletions test/components-in-layouts/eleventy.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const EleventyWebcPlugin = require("../../eleventyWebcPlugin.js");

module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(EleventyWebcPlugin, {
components: "test/components-in-layouts/_components/**/*.webc"
});

return {
dir: {
includes: "_includes",
}
}
}
3 changes: 3 additions & 0 deletions test/components-in-layouts/other-page.webc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
layout: layout.webc
---
3 changes: 3 additions & 0 deletions test/components-in-layouts/page.webc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
layout: layout.webc
---
2 changes: 1 addition & 1 deletion test/nested-layouts/_includes/base.webc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<title></title>
<style @raw="getCss(page.url)"></style>
<style webc:keep @raw="getCss(page.url)"></style>
</head>
<body>
<template webc:nokeep @raw="content"></template>
Expand Down
2 changes: 1 addition & 1 deletion test/nested-layouts/_includes/layout.webc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ layout: base.webc
---
Base
<template webc:nokeep @raw="content"></template>
<style @raw="getCss(page.url)"></style>
<style webc:keep @raw="getCss(page.url)"></style>
2 changes: 1 addition & 1 deletion test/raw-layout-html/_layouts/layout.webc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<title></title>
<style @raw="getCss(page.url)"></style>
<style webc:keep @raw="getCss(page.url)"></style>
</head>
<body>
<template webc:nokeep @raw="this.content"></template>
Expand Down
8 changes: 4 additions & 4 deletions test/sample-1/_layouts/layout.webc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<title></title>
<style @html="this.getCSS(this.page.url)"></style>
<script @html="this.getJS(this.page.url)"></script>
<style webc:keep @html="this.getCSS(this.page.url)"></style>
<script webc:keep @html="this.getJS(this.page.url)"></script>
</head>
<body>
<template webc:nokeep @html="this.content"></template>

<style @html="this.getCSS(this.page.url, 'defer')"></style>
<script @html="this.getJS(this.page.url, 'defer')"></script>
<style webc:keep @html="this.getCSS(this.page.url, 'defer')"></style>
<script webc:keep @html="this.getJS(this.page.url, 'defer')"></script>
</body>
</html>
8 changes: 4 additions & 4 deletions test/sample-2/_layouts/layout.webc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<title></title>
<style @html="this.getCSS(this.page.url)"></style>
<script @html="this.getJS(this.page.url)"></script>
<style webc:keep @html="this.getCSS(this.page.url)"></style>
<script webc:keep @html="this.getJS(this.page.url)"></script>
</head>
<body>
<template webc:nokeep @html="this.content"></template>

<style @html="this.getCSS(this.page.url, 'defer')"></style>
<script @html="this.getJS(this.page.url, 'defer')"></script>
<style webc:keep @html="this.getCSS(this.page.url, 'defer')"></style>
<script webc:keep @html="this.getJS(this.page.url, 'defer')"></script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<title></title>
<style @html="this.getCSS(this.page.url)"></style>
<script @html="this.getJS(this.page.url)"></script>
<style webc:keep @html="this.getCSS(this.page.url)"></style>
<script webc:keep @html="this.getJS(this.page.url)"></script>
</head>
<body>
<template webc:nokeep @html="this.content"></template>

<style @html="this.getCSS(this.page.url, 'defer')"></style>
<script @html="this.getJS(this.page.url, 'defer')"></script>
<style webc:keep @html="this.getCSS(this.page.url, 'defer')"></style>
<script webc:keep @html="this.getJS(this.page.url, 'defer')"></script>
</body>
</html>
8 changes: 4 additions & 4 deletions test/sample-page-global-components/_layouts/layout.webc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<title></title>
<style @html="this.getCSS(this.page.url)"></style>
<script @html="this.getJS(this.page.url)"></script>
<style webc:keep @html="this.getCSS(this.page.url)"></style>
<script webc:keep @html="this.getJS(this.page.url)"></script>
</head>
<body>
<template webc:nokeep @html="this.content"></template>

<style @html="this.getCSS(this.page.url, 'defer')"></style>
<script @html="this.getJS(this.page.url, 'defer')"></script>
<style webc:keep @html="this.getCSS(this.page.url, 'defer')"></style>
<script webc:keep @html="this.getJS(this.page.url, 'defer')"></script>
</body>
</html>
48 changes: 48 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,51 @@ Testing
</body>
</html>`);
});

test("Components in layouts #11", async t => {
let elev = new Eleventy("./test/components-in-layouts/", "./test/components-in-layouts/_site", {
configPath: "./test/components-in-layouts/eleventy.config.js"
});

let results = await elev.toJSON();
let [page1, page2] = results;
t.is(normalize(page1.content), `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content>
<title></title>
<style>/* Inner CSS */
/* Outer CSS */</style>
</head>
<body>
<inner>Test
</inner>
<outer>Test
</outer>
</body>
</html>`);

t.is(normalize(page2.content), `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content>
<title></title>
<style>/* Inner CSS */
/* Outer CSS */</style>
</head>
<body>
<inner>Test
</inner>
<outer>Test
</outer>
</body>
</html>`);
});
2 changes: 1 addition & 1 deletion test/uid-leftovers/_layouts/layout.webc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<title></title>
<style @html="getCSS(page.url)"></style>
<style webc:keep @html="getCSS(page.url)"></style>
</head>
<body></body>
</html>

0 comments on commit 38a2914

Please sign in to comment.