Skip to content

feat(cjs-to-esm): update package.jsons#400

Open
AugustinMauroy wants to merge 4 commits intowip/cjs-to-esmfrom
augustin/issue#342
Open

feat(cjs-to-esm): update package.jsons#400
AugustinMauroy wants to merge 4 commits intowip/cjs-to-esmfrom
augustin/issue#342

Conversation

@AugustinMauroy
Copy link
Member

close #342

@AugustinMauroy AugustinMauroy added the awaiting reviewer Author has responded and needs action from the reviewer label Mar 21, 2026
Copy link
Member

@avivkeller avivkeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just modify JSON, rather than use Codemod for simple JSON transforms?

@JakobJingleheimer
Copy link
Member

Yeah, I would expect:

  1. Read pjson (as json)
  2. Check if there's a problem
  3. Write updated json to pjson

@AugustinMauroy
Copy link
Member Author

you mean something like that ?

const source = root.text();
const pjsonData = JSON.parse(source);

@JakobJingleheimer
Copy link
Member

Like

const rawSource = readFile()
const pjson = JSON.parse(rawSource)

@AugustinMauroy
Copy link
Member Author

if we do that we need to enable fs on jssg context which trow a warn so user can be afraid.

Why I use full ast ?

  • because we use normal codemod workflow
  • we didn't mix ast with jssg object
  • we have much more security/stabilty/predictability

@brunocroh
Copy link
Member

if we do that we need to enable fs on jssg context which trow a warn so user can be afraid.

+1 - I don’t think it’s worth using fs here either. root.source() already returns the file content.

But yeah, if I’m not wrong, you can just return a string and it will replace the entire file. So something like the example below could be an easier way to achieve the same result:

const pjson = JSON.parse(root.source())
// manipulate pjson as a JS object
return JSON.stringify(pjson)

The only drawback I can think of is that formatting might be lost, but even the key order is preserved in most cases.

@AugustinMauroy
Copy link
Member Author

I'm going to take a look at sting -> json parse -> js object manipulation -> json string -> string back to modemode

@alexbit-codemod & @mohebifar what is the recommandation from Codemod team when a codemod should update json file in a JSSG step ?

@JakobJingleheimer JakobJingleheimer changed the title feat(cjs-to-esm/package-json): introduce feat(cjs-to-esm): update package.jsons Mar 22, 2026
@JakobJingleheimer JakobJingleheimer changed the title feat(cjs-to-esm): update package.jsons feat(cjs-to-esm): update package.jsons Mar 22, 2026
@JakobJingleheimer JakobJingleheimer added awaiting author Reviewer has requested something from the author and removed awaiting reviewer Author has responded and needs action from the reviewer labels Mar 22, 2026
@alexbit-codemod
Copy link
Contributor

We have cross file transformations feature (a new one). Also, we do have json support. So you should be good. Let me know if you have other questions.

https://docs.codemod.com/jssg/reference#multi-file-transforms-with-jssgtransform

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting author Reviewer has requested something from the author