What problem does this feature solve?
When using the built-in json feature, both const foo = require('./foo.json') and import foo from './foo.json' returns a value aligned with Node.js (foo has the same value containing the value of ./foo.json).
But when the json is handled by a plugin (like below),
export const plugin = {
name: 'my-json-plugin',
load(id) {
if (!id.endsWith('.json')) return
const content = fs.readFileSync(id, 'utf8')
return `export default ${content}`
}
}
import foo from './foo.json' works as expected, but const foo = require('./foo.json') does not (foo has { default: contentOfJson } instead).
This is a problem for rolldown-vite, as Vite has a built-in JSON plugin that has more advanced options that the builtin json feature in rolldown. (reproduction that shows that it works in current Vite, reproduction that shows that it does not work in Rolldown)
The reason why it works in rollup seems to be because commonjs plugin has some json specific code.
https://github.com/rollup/plugins/blob/94951774773a8797ff7e1a93d0a7ebf14f009327/packages/commonjs/src/dynamic-modules.js#L91
What does the proposed API look like?
Option 1
Add support for module.exports export, which is supported by Node.js (nodejs/node#54563).
With this the JSON plugin in Vite can add module.exports export to control what value require('./foo.json') should return.
export default jsonContent
export { jsonContent as "module.exports" }
The downside of this is that we have to think about what should happen if the arbitrary module namespace specifier (tc39/ecma262#2154) is not supported.
Option 2
Add some special handling for json files like the commonjs plugin does.
Additional Information
I found this while testing rolldown-vite on ecosystem-ci.
The actual dependency I encountered this is https://github.com/jshttp/statuses.
https://github.com/jshttp/statuses/blob/b7b21ff6ea81746d4eba9b8b9d9abee6565da5be/index.js#L28
What problem does this feature solve?
When using the built-in json feature, both
const foo = require('./foo.json')andimport foo from './foo.json'returns a value aligned with Node.js (foohas the same value containing the value of./foo.json).But when the json is handled by a plugin (like below),
import foo from './foo.json'works as expected, butconst foo = require('./foo.json')does not (foohas{ default: contentOfJson }instead).This is a problem for rolldown-vite, as Vite has a built-in JSON plugin that has more advanced options that the builtin json feature in rolldown. (reproduction that shows that it works in current Vite, reproduction that shows that it does not work in Rolldown)
The reason why it works in rollup seems to be because commonjs plugin has some json specific code.
https://github.com/rollup/plugins/blob/94951774773a8797ff7e1a93d0a7ebf14f009327/packages/commonjs/src/dynamic-modules.js#L91
What does the proposed API look like?
Option 1
Add support for
module.exportsexport, which is supported by Node.js (nodejs/node#54563).With this the JSON plugin in Vite can add
module.exportsexport to control what valuerequire('./foo.json')should return.The downside of this is that we have to think about what should happen if the arbitrary module namespace specifier (tc39/ecma262#2154) is not supported.
Option 2
Add some special handling for json files like the commonjs plugin does.
Additional Information
I found this while testing rolldown-vite on ecosystem-ci.
The actual dependency I encountered this is https://github.com/jshttp/statuses.
https://github.com/jshttp/statuses/blob/b7b21ff6ea81746d4eba9b8b9d9abee6565da5be/index.js#L28