-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ Add `bundles: false` option to skip built-in bundles. + Throws an error on incompatible eleventy versions. + Adds `addBundle` and `getBundleManagers` methods to eleventyConfig. + Add option to set file extension separate from key #22 + Handle multiple addPlugin calls. + Skip transform if no bundles in play or if page being processed did not fetch bundles. + Changes tests to use ESM and 3.0+
- Loading branch information
Showing
9 changed files
with
185 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
const pkg = require("./package.json"); | ||
const CodeManager = require("./codeManager.js"); | ||
const debug = require("debug")("Eleventy:Bundle"); | ||
|
||
module.exports = function(eleventyConfig, pluginOptions = {}) { | ||
if("getBundleManagers" in eleventyConfig || "addBundle" in eleventyConfig) { | ||
throw new Error("Duplicate plugin calls for " + pkg.name); | ||
} | ||
|
||
let managers = {}; | ||
|
||
function addBundle(name, bundleOptions = {}) { | ||
if(name in managers) { | ||
debug("Bundle exists %o, skipping.", name); | ||
// note: shortcode must still be added | ||
} else { | ||
debug("Creating new bundle %o", name); | ||
managers[name] = new CodeManager(name); | ||
|
||
if(bundleOptions.hoist !== undefined) { | ||
managers[name].setHoisting(bundleOptions.hoist); | ||
} | ||
|
||
if(bundleOptions.outputFileExtension) { | ||
managers[name].setFileExtension(bundleOptions.outputFileExtension); | ||
} | ||
|
||
if(bundleOptions.transforms) { | ||
managers[name].setTransforms(bundleOptions.transforms); | ||
} | ||
} | ||
|
||
// if undefined, defaults to `name` | ||
if(bundleOptions.shortcodeName !== false) { | ||
let shortcodeName = bundleOptions.shortcodeName || name; | ||
|
||
// e.g. `css` shortcode to add code to page bundle | ||
// These shortcode names are not configurable on purpose (for wider plugin compatibility) | ||
eleventyConfig.addPairedShortcode(shortcodeName, function addContent(content, bucket, urlOverride) { | ||
let url = urlOverride || this.page.url; | ||
managers[name].addToPage(url, content, bucket); | ||
return ""; | ||
}); | ||
} | ||
}; | ||
|
||
eleventyConfig.addBundle = addBundle; | ||
|
||
eleventyConfig.getBundleManagers = function() { | ||
return managers; | ||
}; | ||
|
||
eleventyConfig.on("eleventy.before", async () => { | ||
for(let key in managers) { | ||
managers[key].reset(); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,29 @@ | ||
const bundlePlugin = require("../"); | ||
|
||
module.exports = function(eleventyConfig) { | ||
// This call is what Eleventy will do in the default config in 3.0.0-alpha.10 | ||
eleventyConfig.addPlugin(bundlePlugin, { | ||
bundles: false, | ||
immediate: true | ||
}); | ||
|
||
// adds html, css, js (maintain existing API) | ||
eleventyConfig.addPlugin(bundlePlugin); | ||
eleventyConfig.addPlugin(bundlePlugin); | ||
|
||
// ignored, already exists | ||
eleventyConfig.addBundle("css"); | ||
// ignored, already exists | ||
eleventyConfig.addBundle("css"); | ||
// ignored, already exists | ||
eleventyConfig.addBundle("css"); | ||
// ignored, already exists | ||
eleventyConfig.addBundle("html"); | ||
|
||
// new! | ||
eleventyConfig.addBundle("stylesheet", { | ||
outputFileExtension: "css", | ||
shortcodeName: "stylesheet", | ||
transforms: [], | ||
// hoist: true, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.