Skip to content

Commit

Permalink
Use eleventy-plugin-bundle internally
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Feb 10, 2023
1 parent 69d1435 commit 78e9f18
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 206 deletions.
10 changes: 10 additions & 0 deletions eleventyWebcPlugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const eleventyBundlePlugin = require("@11ty/eleventy-plugin-bundle");
const pkg = require("./package.json");
const templatePlugin = require("./src/eleventyWebcTemplate.js");
const transformPlugin = require("./src/eleventyWebcTransform.js");
Expand All @@ -9,6 +10,7 @@ module.exports = function(eleventyConfig, options = {}) {
console.log( `WARN: Eleventy Plugin (${pkg.name}) Compatibility: ${e.message}` );
}

// Deprecated: this lives in @11ty/eleventy-plugin-bundle now
let filters = Object.assign({
css: "webcGetCss",
js: "webcGetJs",
Expand All @@ -20,6 +22,7 @@ module.exports = function(eleventyConfig, options = {}) {
transformData: {}, // extra global data for transforms specifically
}, options);

// Deprecated: this lives in @11ty/eleventy-plugin-bundle now
options.filters = filters;

if(options.components) {
Expand All @@ -33,6 +36,13 @@ module.exports = function(eleventyConfig, options = {}) {
eleventyConfig.ignores.add(options.components);
}

// TODO Remove this when @11ty/eleventy-plugin-bundle is moved to core.
// If the bundle plugin has not been added, we add it here:
let bundlePlugin = eleventyConfig.plugins.find(entry => entry.plugin.eleventyPackage === "@11ty/eleventy-plugin-bundle");
if(!bundlePlugin) {
eleventyConfig.addPlugin(eleventyBundlePlugin);
}

templatePlugin(eleventyConfig, options);

if(options.useTransform) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
]
},
"dependencies": {
"@11ty/eleventy": "^2.0.0-canary.19",
"@11ty/eleventy": "2.0.0-beta.3",
"@11ty/eleventy-plugin-bundle": "^1.0.1",
"@11ty/webc": "^0.8.0"
},
"devDependencies": {
Expand Down
50 changes: 0 additions & 50 deletions src/bundleAssets.js

This file was deleted.

33 changes: 0 additions & 33 deletions src/codeManager.js

This file was deleted.

59 changes: 21 additions & 38 deletions src/eleventyWebcTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ const path = require("path");
const { EleventyRenderPlugin } = require("@11ty/eleventy");
const CompileString = EleventyRenderPlugin.String;

const CodeManager = require("./codeManager.js");
const BundleAssetsToContent = require("./bundleAssets.js");

function relativePath(inputPath, newGlob) {
// project root
if(newGlob.startsWith("~/")) {
Expand All @@ -20,19 +17,14 @@ function relativePath(inputPath, newGlob) {
}

module.exports = function(eleventyConfig, options = {}) {
// TODO remove this when WebC is moved out of plugin-land into core.
eleventyConfig.addTemplateFormats("webc");

let cssManager = new CodeManager();
let jsManager = new CodeManager();

let _WebC;
let componentsMap = false; // cache the glob search
let moduleScript;

eleventyConfig.on("eleventy.before", async () => {
cssManager.reset();
jsManager.reset();

// For ESM in CJS
let { WebC, ModuleScript } = await import("@11ty/webc");
moduleScript = ModuleScript;
Expand All @@ -43,22 +35,18 @@ module.exports = function(eleventyConfig, options = {}) {
}
});

// Expose bundled CSS code to other template languages
// Deprecated (backwards compat only): this lives in @11ty/eleventy-plugin-bundle now
if(options.filters.css) {
function getCss(pageUrl, bucket = "default") {
return cssManager.getForPage(pageUrl, bucket);
}

eleventyConfig.addFilter(options.filters.css, (url, bucket) => getCss(url, bucket));
eleventyConfig.addFilter(options.filters.css, (url, bucket) => {
return eleventyConfig.javascriptFunctions.getBundle("css", bucket);
});
}

// Expose bundled JS code to other template languages
// Deprecated (backwards compat only): this lives in @11ty/eleventy-plugin-bundle now
if(options.filters.js) {
function getJs(pageUrl, bucket = "default") {
return jsManager.getForPage(pageUrl, bucket);
}

eleventyConfig.addFilter(options.filters.js, (url, bucket) => getJs(url, bucket));
eleventyConfig.addFilter(options.filters.js, (url, bucket) => {
return eleventyConfig.javascriptFunctions.getBundle("js", bucket)
});
}

eleventyConfig.addExtension("webc", {
Expand All @@ -85,17 +73,17 @@ module.exports = function(eleventyConfig, options = {}) {
page.defineComponents(componentsMap);
}

// Add Eleventy JavaScript Functions as WebC helpers (Universal Filters also populate into these)
// Add Eleventy JavaScript Functions as WebC helpers (Universal Filters and Shortcodes also populate into these)
for(let helperName in this.config.javascriptFunctions) {
page.setHelper(helperName, this.config.javascriptFunctions[helperName]);
}

// Support both casings (I prefer getCss, but yeah)
page.setHelper("getCss", (url, bucket) => BundleAssetsToContent.getAssetKey("css", bucket));
page.setHelper("getCSS", (url, bucket) => BundleAssetsToContent.getAssetKey("css", bucket));
page.setHelper("getCss", (url, bucket) => this.config.javascriptFunctions.getBundle("css", bucket));
page.setHelper("getCSS", (url, bucket) => this.config.javascriptFunctions.getBundle("css", bucket));

page.setHelper("getJs", (url, bucket) => BundleAssetsToContent.getAssetKey("js", bucket));
page.setHelper("getJS", (url, bucket) => BundleAssetsToContent.getAssetKey("js", bucket));
page.setHelper("getJs", (url, bucket) => this.config.javascriptFunctions.getBundle("js", bucket));
page.setHelper("getJS", (url, bucket) => this.config.javascriptFunctions.getBundle("js", bucket));

page.setTransform("11ty", async function(content) {
let syntax = this["11ty:type"];
Expand Down Expand Up @@ -126,30 +114,25 @@ module.exports = function(eleventyConfig, options = {}) {
// 2.0.0-canary.19+
this.addDependencies(inputPath, components);

cssManager.addToPage(data.page.url, css, "default");
// Add CSS to bundle
this.config.javascriptFunctions.css(css, "default", data.page.url);

if(buckets.css) {
for(let bucket in buckets.css) {
cssManager.addToPage(data.page.url, buckets.css[bucket], bucket);
this.config.javascriptFunctions.css(css, buckets.css[bucket], data.page.url);
}
}

jsManager.addToPage(data.page.url, js, "default");
// Add JS to bundle
this.config.javascriptFunctions.js(js, "default", data.page.url);

if(buckets.js) {
for(let bucket in buckets.js) {
jsManager.addToPage(data.page.url, buckets.js[bucket], bucket);
this.config.javascriptFunctions.js(js, buckets.js[bucket], data.page.url);
}
}

// Always do a two pass render for assets to catch any CSS/JS that were compiled *in* the same template.
// https://github.com/11ty/eleventy-plugin-webc/issues/33
// This unlocks use of bundled asset code anywhere in the WebC component tree (not just Eleventy Layouts)
let bundler = new BundleAssetsToContent(html);
bundler.setAssetManager("css", cssManager);
bundler.setAssetManager("js", jsManager);

return bundler.replaceAll(data.page.url);
return html;
};
}
});
Expand Down
5 changes: 5 additions & 0 deletions test/bundler-helpers/eleventy.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const EleventyWebcPlugin = require("../../eleventyWebcPlugin.js");

module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(EleventyWebcPlugin);
}
6 changes: 6 additions & 0 deletions test/bundler-helpers/index.webc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<style>/* Bundle 1 */</style>
<template webc:type="11ty" 11ty:type="liquid">
{% css %}/* Bundle 2 */{% endcss %}
</template>
<style @raw="getCss(page.url)" webc:keep></style>
<style @raw="getBundle('css')" webc:keep></style>
84 changes: 0 additions & 84 deletions test/test-bundle-assets.js

This file was deleted.

15 changes: 15 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,18 @@ t.is(normalize(page3.content), `No layouts here
<outer>Test
</outer>`);
});


test("Helpers in the bundler", async t => {
let elev = new Eleventy("./test/bundler-helpers/index.webc", "./test/bundler-helpers/_site", {
configPath: "./test/bundler-helpers/eleventy.config.js"
});

let results = await elev.toJSON();
let [result] = results;

t.is(normalize(result.content), `<style>/* Bundle 2 */
/* Bundle 1 */</style>
<style>/* Bundle 2 */
/* Bundle 1 */</style>`);
});

0 comments on commit 78e9f18

Please sign in to comment.