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

loading yaml with numeric keys #30

Open
sodiumjoe opened this issue Aug 30, 2024 · 2 comments
Open

loading yaml with numeric keys #30

sodiumjoe opened this issue Aug 30, 2024 · 2 comments

Comments

@sodiumjoe
Copy link

Revisiting this issue, I don't think the raw config is quite the correct solution.

The original issue is that this plugin unquotes numeric keys:

❯ node
Welcome to Node.js v18.14.0.
Type ".help" for more information.
> yaml = require('@modyfi/vite-plugin-yaml')
{ default: [Function: src_default] }
> await yaml.default().transform(`
... '07': foo
... `, "foo.yaml")
{
  code: 'const data = { 07:"foo" };\nexport default data;',
  map: { mappings: '' }
}
>

Which, as the above issue notes, results in an error because of the leading 0.

The problem with raw mode is it fails on objects:

> await yaml.default({raw: true}).transform(`
... foo: bar
... `, "foo.yaml")
{
  code: 'const data = [object Object];\nexport default data;',
  map: { mappings: '' }
}

The above issue correctly identified tosource as the culprit here. Since js-yaml.load returns plain JSON, I'm not sure why tosource is necessary at all, as opposed to just JSON.stringify.

Happy to submit a PR if that sounds reasonable.

@sodiumjoe
Copy link
Author

Another option is to use JSON.stringify when raw is true.

@Pistonight
Copy link

Another option is to use JSON.stringify when raw is true.

I was looking for a similar config to https://vitejs.dev/config/shared-options.html#json-stringify for performance
A stringify option would be nice, instead of raw, which I feel have very limited use cases since they only work on scalar?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants