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

fix(rollup): allow default require for non standard export names in .json files #218

Closed
wants to merge 3 commits into from

Conversation

alexzhang1030
Copy link

closes #217

Why?

The @rollup/plugin-json plugin transforms a json module into an ESM.

For example:

{ "foo": "bar" }

becomes:

export default { foo: "bar" }

However, CJS require statement can natively import a json file using the following syntax:

const json = require("./foo.json")
json.foo // directly access fields.

But when a json module is transformed to an ESM and exported using export default, we need to change the require statement to:

require("./foo.json")['default']

Resolution

For every require('*.json') statement, add the ['default'] suffix.

@pi0 pi0 added the pending label Mar 27, 2023
@pi0 pi0 changed the title fix: build result incorrect when require a json fix(rollup): allow default require for non standard export names in .json files Jul 18, 2023
@pi0
Copy link
Member

pi0 commented Jul 18, 2023

Hi! Thanks for your pull request and updated fixture. I think this is a valid fix as Node.js native behavior with .cjs files is similar to this. However running a transform on ALL entries like current changes to detect require() syntax seems a risky change and also potentially slowing down builds that's why still kept PR in pending state. If you have any alternative ideas please mention 👍🏼

@pi0
Copy link
Member

pi0 commented Oct 30, 2023

I'm an not sure if this behavior would be aligned with ESM standards. But still open to discussion if you think a core change worth to do it.

@pi0 pi0 closed this Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cannot use kebab-case keys from .json file default exports
2 participants