Skip to content

Commit

Permalink
Remove some configurable properties to make things more predictable i…
Browse files Browse the repository at this point in the history
…n other plugins
  • Loading branch information
zachleat committed Feb 2, 2023
1 parent dacea9d commit e67dea6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 86 deletions.
23 changes: 5 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,6 @@ module.exports = function(eleventyConfig) {

// Default bundle types
bundles: ["css", "js", "html"],

// Shortcode names
shortcodes: {
get: "getBundle",
toFile: "getBundleFileUrl",

// override bundle add names:
add: {
// use `addCss` instead of `css`
// css: "addCss"
}
}
});
};
```
Expand Down Expand Up @@ -254,31 +242,30 @@ _Coming soon_

### Add your own bundle type

If you’d like to add your own bundle types (in addition to `css`, `js`, and `html`), you can do so:
If you’d like to add your own bundle types (in addition to the guaranteed types: `css`, `js`, and `html`), you can do so:

```js
const bundlerPlugin = require("@11ty/eleventy-plugin-bundle");

module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(bundlerPlugin, {
bundles: ["css", "js", "html", "possum"]
bundles: ["possum"]
});
};
```

You _could_ remove existing bundle types too, the `bundles` array content is not deeply merged. The addition of `"possum"` in this array:
This does two things:

1. creates a new `possum` shortcode for adding arbitrary code to this bundle
2. adds `"possum"` as an eligible type argument to `getBundle` and `getBundleFileUrl`

<!--
Must haves:
* plugin duplicates: if I addPlugin directly from the webc plugin and someone else also adds it, what happens?
* Add postprocessing transforms for postcss modifications
* Add postprocessing hooks for postcss modifications
* guarantee that the transform runs first in order somehow (think about transform order)
* TODOs on readme: WebC example
* JavaScript API independent of eleventy
Version Two:
Expand Down
36 changes: 12 additions & 24 deletions eleventy.config.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
const pkg = require("./package.json");
const shortcodesPlugin = require("./eleventy.shortcodes.js");
const debug = require("debug")("Eleventy:Bundle");

function normalizeOptions(options) {
let shortcodes = Object.assign({
get: "getBundle",
toFile: "getBundleFileUrl",
add: {
// use `addCss` instead of `css`
// css: "addCss"
}
}, options.shortcodes);

function normalizeOptions(options = {}) {
options = Object.assign({
// Plugin defaults
bundles: ["css", "js", "html"],
bundles: [], // extra bundles: css, js, and html are guaranteed
toFileDirectory: "bundle",
}, options);

options.shortcodes = shortcodes;
options.bundles = Array.from(new Set(["css", "js", "html", ...options.bundles]));

return options;
}

let hasExecuted = false;

function eleventyBundlePlugin(eleventyConfig, options = {}) {
try {
eleventyConfig.versionCheck(pkg["11ty"].compatibility);
Expand All @@ -34,16 +22,16 @@ function eleventyBundlePlugin(eleventyConfig, options = {}) {

options = normalizeOptions(options);

eleventyConfig.on("eleventy.before", () => {
hasExecuted = false;
});
// TODO
// debug("Warning: Currently @11ty/eleventy-plugin-bundle only supports one addPlugin of this plugin per project. Subsequent adds are ignored.");

if(hasExecuted) {
debug("Warning: Currently @11ty/eleventy-plugin-bundle only supports one addPlugin of this plugin per project. Subsequent adds are ignored.");
} else {
hasExecuted = true;
shortcodesPlugin(eleventyConfig, options);
}
shortcodesPlugin(eleventyConfig, options);
};

// This plugin is used to find the package name for this plugin (used by eleventy-plugin-webc)
Object.defineProperty(eleventyBundlePlugin, "eleventyPackage", {
value: pkg.name
});

module.exports = eleventyBundlePlugin;
module.exports.normalizeOptions = normalizeOptions;
77 changes: 33 additions & 44 deletions eleventy.shortcodes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const path = require("path");
const CodeManager = require("./codeManager.js");
const OutOfOrderRender = require("./outOfOrderRender.js");
const debug = require("debug")("Eleventy:Bundle");
Expand All @@ -11,18 +10,12 @@ module.exports = function(eleventyConfig, options = {}) {
managers[name] = new CodeManager(name);

// e.g. `css` shortcode to add code to page bundle
let addShortcodeName = name;
if(options.shortcodes.add && options.shortcodes.add[name] !== undefined) {
addShortcodeName = options.shortcodes.add[name];
}

if(addShortcodeName) {
eleventyConfig.addPairedShortcode(addShortcodeName, function addContent(content, bucket, urlOverride) {
let url = urlOverride || this.page.url;
managers[name].addToPage(url, content, bucket);
return "";
});
}
// These shortcode names are not configurable on purpose (for wider plugin compatibility)
eleventyConfig.addPairedShortcode(name, function addContent(content, bucket, urlOverride) {
let url = urlOverride || this.page.url;
managers[name].addToPage(url, content, bucket);
return "";
});
});
}

Expand All @@ -39,42 +32,38 @@ module.exports = function(eleventyConfig, options = {}) {
});

// e.g. `getBundle` shortcode to get code in current page bundle
if(options.shortcodes.get) {
// bucket can be an array
eleventyConfig.addShortcode(options.shortcodes.get, function getContent(type, bucket) {
if(!type || !(type in managers)) {
throw new Error("Invalid bundle type: " + type);
}
// bucket can be an array
// This shortcode name is not configurable on purpose (for wider plugin compatibility)
eleventyConfig.addShortcode("getBundle", function getContent(type, bucket) {
if(!type || !(type in managers)) {
throw new Error("Invalid bundle type: " + type);
}

return OutOfOrderRender.getAssetKey("get", type, bucket);
});
}
return OutOfOrderRender.getAssetKey("get", type, bucket);
});

// write a bundle to the file system
if(options.shortcodes.toFile) {
eleventyConfig.addShortcode(options.shortcodes.toFile, function(type, bucket) {
if(!type || !(type in managers)) {
throw new Error("Invalid bundle type: " + type);
}
// This shortcode name is not configurable on purpose (for wider plugin compatibility)
eleventyConfig.addShortcode("getBundleFileUrl", function(type, bucket) {
if(!type || !(type in managers)) {
throw new Error("Invalid bundle type: " + type);
}

return OutOfOrderRender.getAssetKey("file", type, bucket);
});
}
return OutOfOrderRender.getAssetKey("file", type, bucket);
});

if(options.shortcodes.get || options.shortcodes.toFile) {
eleventyConfig.addTransform("@11ty/eleventy-bundle", function(content) {
if((this.page.outputPath || "").endsWith(".html")) {
let render = new OutOfOrderRender(content);
for(let key in managers) {
render.setAssetManager(key, managers[key]);
}
eleventyConfig.addTransform("@11ty/eleventy-bundle", function(content) {
if((this.page.outputPath || "").endsWith(".html")) {
let render = new OutOfOrderRender(content);
for(let key in managers) {
render.setAssetManager(key, managers[key]);
}

render.setOutputDirectory(eleventyConfig.dir.output);
render.setBundleDirectory(options.toFileDirectory);
render.setWriteToFileSystem(writeToFileSystem);
render.setOutputDirectory(eleventyConfig.dir.output);
render.setBundleDirectory(options.toFileDirectory);
render.setWriteToFileSystem(writeToFileSystem);

return render.replaceAll(this.page.url);
}
});
}
return render.replaceAll(this.page.url);
}
});
};

0 comments on commit e67dea6

Please sign in to comment.