Skip to content

Commit

Permalink
Allow toFileDirectory option on individual addBundle options.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Apr 30, 2024
1 parent b4ae1f2 commit 41c49f2
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion BundleFileOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const debug = require("debug")("Eleventy:Bundle");
class BundleFileOutput {
constructor(outputDirectory, bundleDirectory) {
this.outputDirectory = outputDirectory;
this.bundleDirectory = bundleDirectory;
this.bundleDirectory = bundleDirectory || "";
this.hashLength = 10;
this.fileExtension = undefined;
}
Expand Down
9 changes: 7 additions & 2 deletions codeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class CodeManager {
this.transforms = [];
this.isHoisting = true;
this.fileExtension = undefined;
this.toFileDirectory = undefined;
}

setFileExtension(ext) {
Expand All @@ -27,6 +28,10 @@ class CodeManager {
this.isHoisting = !!enabled;
}

setBundleDirectory(dir) {
this.toFileDirectory = dir;
}

reset() {
this.pages = {};
}
Expand Down Expand Up @@ -145,13 +150,13 @@ class CodeManager {
return "";
}

let { output, bundle, write } = options;
let { output, write } = options;

buckets = CodeManager.normalizeBuckets(buckets);

// TODO the bundle output URL might be useful in the transforms for sourcemaps
let content = await this.getForPage(pageData, buckets);
let writer = new BundleFileOutput(output, bundle);
let writer = new BundleFileOutput(output, this.toFileDirectory);
writer.setFileExtension(this.fileExtension);
return writer.writeBundle(content, this.name, write);
}
Expand Down
1 change: 1 addition & 0 deletions eleventy.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function eleventyBundlePlugin(eleventyConfig, pluginOptions = {}) {
outputFileExtension: name, // default as `name`
shortcodeName: name, // `false` will skip shortcode
transforms: pluginOptions.transforms,
toFileDirectory: pluginOptions.toFileDirectory,
});
});
}
Expand Down
4 changes: 4 additions & 0 deletions eleventy.bundleManagers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ module.exports = function(eleventyConfig, pluginOptions = {}) {
managers[name].setFileExtension(bundleOptions.outputFileExtension);
}

if(bundleOptions.toFileDirectory) {
managers[name].setBundleDirectory(bundleOptions.toFileDirectory);
}

if(bundleOptions.transforms) {
managers[name].setTransforms(bundleOptions.transforms);
}
Expand Down
3 changes: 1 addition & 2 deletions eleventy.shortcodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ module.exports = function(eleventyConfig, pluginOptions = {}) {
render.setAssetManager(key, managers[key]);
}

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

return render.replaceAll(this.page);
Expand Down
5 changes: 0 additions & 5 deletions outOfOrderRender.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ class OutOfOrderRender {
this.outputDirectory = dir;
}

setBundleDirectory(dir) {
this.bundleDirectory = dir;
}

normalizeMatch(match) {
if(match.startsWith("/*__EleventyBundle:")) {
let [prefix, type, name, bucket, suffix] = match.split(OutOfOrderRender.SEPARATOR);
Expand Down Expand Up @@ -130,7 +126,6 @@ class OutOfOrderRender {
// returns promise
return manager.writeBundle(pageData, bucket, {
output: this.outputDirectory,
bundle: this.bundleDirectory,
write: this.writeToFileSystem,
});
}
Expand Down
22 changes: 13 additions & 9 deletions sample/sample-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ module.exports = function(eleventyConfig) {
});

// adds html, css, js (maintain existing API)
eleventyConfig.addPlugin(bundlePlugin);
eleventyConfig.addPlugin(bundlePlugin, {
toFileDirectory: "bundle1"
});

// ignored, already exists
eleventyConfig.addBundle("css");
// ignored, already exists
eleventyConfig.addBundle("css");
// ignored, already exists
eleventyConfig.addBundle("css");
// ignored, already exists
eleventyConfig.addBundle("html");
eleventyConfig.addPlugin(eleventyConfig => {
// 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", {
Expand Down
3 changes: 2 additions & 1 deletion sample/test.njk
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ And now you can use `icon-close` in as many SVG instances as you’d like (witho
<svg><use xlink:href="#icon-close"></use></svg>
<svg><use xlink:href="#icon-close"></use></svg>
<svg><use xlink:href="#icon-close"></use></svg>
<style>{% getBundle "stylesheet" %}</style>
<style>{% getBundle "stylesheet" %}</style>
{# <link rel="stylesheet" href="{% getBundleFileUrl "css" %}"> #}

0 comments on commit 41c49f2

Please sign in to comment.