Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to add custom data extension for JSON files #2300

Closed
pdehaan opened this issue Apr 1, 2022 · 4 comments
Closed

Allow users to add custom data extension for JSON files #2300

pdehaan opened this issue Apr 1, 2022 · 4 comments
Labels
enhancement needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved.

Comments

@pdehaan
Copy link
Contributor

pdehaan commented Apr 1, 2022

Is your feature request related to a problem? Please describe.
I can add a custom data extension for .json5 files which allows me to load JSON files w/ comments (huzzah!)
But I wanted to shim that into .json files as well and was hoping I could just add a custom data extension for .json files as well, but no luck. Which is semi-understandable because Node, but unfortunate.

Describe the solution you'd like

const {inspect} = require("node:util");
const json5 = require("json5");

module.exports = eleventyConfig => {
  eleventyConfig.addDataExtension("json", contents => json5.parse(contents));
  eleventyConfig.addDataExtension("json5", contents => json5.parse(contents));

  eleventyConfig.addFilter("inspect", value => inspect(value));

  return {
    dir: {
      input: "src",
      output: "www",
    }
  };
};
> 11ty-json5-test@1.0.0 build
> eleventy

[11ty] > /private/tmp/11ty-json5-test/src/_data/def.json: Unexpected token n in JSON at position 4

`SyntaxError` was thrown:
[11ty]     SyntaxError: /private/tmp/11ty-json5-test/src/_data/def.json: Unexpected token n in JSON at position 4
        at parse (<anonymous>)
        at Object.Module._extensions..json (node:internal/modules/cjs/loader:1163:22)
        at Module.load (node:internal/modules/cjs/loader:975:32)
        at Function.Module._load (node:internal/modules/cjs/loader:822:12)
        at Module.require (node:internal/modules/cjs/loader:999:19)
        at require (node:internal/modules/cjs/helpers:102:18)
        at TemplateData.getDataValue (/private/tmp/11ty-json5-test/node_modules/@11ty/eleventy/src/TemplateData.js:477:25)
        at TemplateData.getAllGlobalData (/private/tmp/11ty-json5-test/node_modules/@11ty/eleventy/src/TemplateData.js:271:29)
        at async TemplateData.getData (/private/tmp/11ty-json5-test/node_modules/@11ty/eleventy/src/TemplateData.js:326:24)
The terminal process "/bin/zsh '-c', 'npm run build'" terminated with exit code: 1.

Describe alternatives you've considered
Not sure. Ideally I can override the JSON loader. I was hoping to come up w/ a workaround for #2299 where I could try building a safer JSON parser which would return empty objects on a parsing error to fail more silently.

Additional context
❤️

@pdehaan
Copy link
Contributor Author

pdehaan commented Apr 1, 2022

Where my src/_data/def.json looks like this:

{
  name: "Zaaaach"
  // A wild comment appears!
}

And the data loads a-ok if I rename it to def.json5 (see https://www.npmjs.com/package/json5)

@pdehaan
Copy link
Contributor Author

pdehaan commented Apr 1, 2022

Possible workaround via require-json5, which I believe shims the require.extensions mapping.

require("require-json5").replace();

module.exports = eleventyConfig => {
  return {
    dir: {
      input: "src",
      output: "www",
    }
  };
};
{
  // src/_data/sample.json
  name: "pdehaan"
}
---
# src/index.liquid
title: OK Computer!
---

<h1>{{ title }}</h1>
<pre>{{ sample.name }}</pre>

OUTPUT

<h1>OK Computer!</h1>
<pre>pdehaan</pre>

Of course, adding this to core is probably a bit too wild. But wasn't sure if there was a possibility of checking user defined data extensions before checking the default handlers for .js, .cjs, or .json (which I think is happening in src/TemplateData.js:459 in getDataValue() maybe).

@zachleat
Copy link
Member

Yeah I don’t think we’ll want to use require.extensions for this but it does seem inevitable that we’ll refactor some of this require-based JSON loading out as we move to ECMAScript Modules.

#836 (required for first-class Deno support too)

@zachleat zachleat added the needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved. label Apr 11, 2022
@zachleat
Copy link
Member

This repository is now using lodash style issue management for enhancements. This means enhancement issues will now be closed instead of leaving them open.

View the enhancement backlog here. Don’t forget to upvote the top comment with 👍!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved.
Projects
None yet
Development

No branches or pull requests

2 participants