zce / velite

Turns Markdown / MDX, YAML, JSON, or others into app's data layer with Zod schema.
http://velite.js.org
MIT License
495 stars 25 forks source link

JSON import issue in a Node.js application #221

Closed muco-rolle closed 1 month ago

muco-rolle commented 2 months ago

JSON import issue in a Node.js application

I tried Velite in AdonisJS and encountered an issue with importing JSON files.

The type inference seems to be broken as well.

TypeError: Module "file:///.../.velite/posts.json" needs an import attribute of "type: json"

   ⁃ async addShortCircuitFlag
     node_modules/ts-node-maintained/src/esm.ts:409

However, if I change the import in the generated file as shown below, it fixes the issue.

const { default: posts } = await import('./posts.json', { with: { type: 'json' } })
export { posts }

Here's a reproduction repo: https://github.com/muco-rolle/velite-adonisjs

zce commented 2 months ago

This is indeed a known issue. Velite was originally designed for use in a bundler. I need to consider this.

muco-rolle commented 2 months ago

I’d make a PR with some guidelines. Does it need an option to output that format in an environment without a bundler?

zce commented 2 months ago

yes, I think it should be an options in config.output.format

muco-rolle commented 1 month ago

Cool, I'll look into it and submit a PR