Skip to content

Commit

Permalink
Adds getOutput method to serverless to return all of the raw entries …
Browse files Browse the repository at this point in the history
…that match the path (for advanced serverless usage).

Adds `dataFilterSelectors` to configuration file to allow some data to pass through to the JSON object returned by Eleventy.prototype.toJSON. These are lodash selectors.
  • Loading branch information
zachleat committed Nov 4, 2021
1 parent ccf9c89 commit a9c29f4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/Serverless.js
Expand Up @@ -155,7 +155,7 @@ class Serverless {
return {};
}

async render() {
async getOutput() {
if (this.dir.startsWith("/var/task/")) {
process.chdir(this.dir);
}
Expand Down Expand Up @@ -220,27 +220,25 @@ class Serverless {
// TODO (@zachleat) https://github.com/11ty/eleventy/issues/1957
this.deleteEnvironmentVariables();

return json.filter((entry) => {
return entry.inputPath === inputPath;
});
}

async render() {
let json = await this.getOutput();

if (!json.length) {
let err = new Error(
`Couldn’t find any generated output from Eleventy (URL path parameters: ${JSON.stringify(
`Couldn’t find any generated output from Eleventy (Input path: ${inputPath}, URL path parameters: ${JSON.stringify(
pathParams
)}).`
);
err.httpStatusCode = 404;
throw err;
}

for (let entry of json) {
if (entry.inputPath === inputPath) {
return entry.content;
}
}

// Log to Serverless Function output
console.log(json);
throw new Error(
`Couldn’t find any matching output from Eleventy for ${inputPath} (${json.length} pages rendered).`
);
return json[0].content;
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/Template.js
Expand Up @@ -3,6 +3,8 @@ const os = require("os");
const path = require("path");
const normalize = require("normalize-path");
const isPlainObject = require("lodash/isPlainObject");
const lodashGet = require("lodash/get");
const lodashSet = require("lodash/set");
const { DateTime } = require("luxon");

const TemplateData = require("./TemplateData");
Expand Down Expand Up @@ -805,6 +807,15 @@ class Template extends TemplateContent {
return content;
}

retrieveDataForJsonOutput(data, selectors) {
let filtered = {};
for (let selector of selectors) {
let value = lodashGet(data, selector);
lodashSet(filtered, selector, value);
}
return filtered;
}

async generateMapEntry(mapEntry, to) {
return Promise.all(
mapEntry._pages.map(async (page) => {
Expand All @@ -824,6 +835,16 @@ class Template extends TemplateContent {
content: content,
};

if (
this.config.dataFilterSelectors &&
this.config.dataFilterSelectors.size > 0
) {
obj.data = this.retrieveDataForJsonOutput(
page.data,
this.config.dataFilterSelectors
);
}

if (to === "ndjson") {
let jsonString = JSON.stringify(obj);
this.logger.toStream(jsonString + os.EOL);
Expand Down
2 changes: 2 additions & 0 deletions src/UserConfig.js
Expand Up @@ -77,6 +77,7 @@ class UserConfig {
this._pluginExecution = false;

this.useTemplateCache = true;
this.dataFilterSelectors = new Set();
}

versionCheck(expected) {
Expand Down Expand Up @@ -770,6 +771,7 @@ class UserConfig {
plugins: this.plugins,
useTemplateCache: this.useTemplateCache,
precompiledCollections: this.precompiledCollections,
dataFilterSelectors: this.dataFilterSelectors,
};
}
}
Expand Down

0 comments on commit a9c29f4

Please sign in to comment.