0

I want to sort a JSON file as part of my build process. Basically I have react-intl which defines localised messages, then babel-plugin-react-intl extracts those messages to files - the issue is that the file in question is not sorted (which creates all sorts of annoying versioning problems).

I could simply hack the actual dist of the plugin (which works), but that is not very scalable. I looked at babel-plugin-macros but it seems most of these deal with traversing the actual AST of applications. Basically all I want is to say; "At the end of every build, run this function" which would contain:

//Open the file en.json
Object.keys(messages).reduce((accumulator, currentValue) => {
  accumulator[currentValue] = testObj[currentValue];
  return accumulator;
}, {})
// save it again

Isn't there any simple way to just call an extra function as part of your pipeline - without having too hook into the whole visitor/ast traversal API of babel?

Dennis
  • 909
  • 4
  • 13
  • 30

1 Answers1

0

Assuming you are calling the webpack build e.g. using webpack inside of a package.json script like this by using npm run build or yarn build

"scripts": { 
   "build": "webpack"
}

Then you can pretty easily create a javascript file (sortFile.js) with your sorting task and then call it in that same build script command...e.g.

"scripts": { 
   "build": "webpack && node ./sortFile.js"
}

The && shell command will only run that second step after the first one succeeds.

What is the purpose of "&&" in a shell command?

nicholascm
  • 621
  • 4
  • 7