I am creating a custom preprocess for prettier plugin, which rearrange the import order, but it is not working properly

19 Views Asked by At

The below was the preprocess I have created, I can see the reordering occurs and the conversation of the ast to code was also happening, but while saving the file the reordering was not happening. I am using prettier in vscode.

The below code is for plugin.js file

const { parsers } = require("prettier/parser-babel");
const { default: generate } = require("@babel/generator");
const fs = require("fs");


const parseIt = (text, options) => {
  console.log(text);
  const ast = parsers.babel.parse(text);
  let temp = ast.program.body[0];
  ast.program.body[0] = ast.program.body[1];
  ast.program.body[1] = temp;
  console.log(generate(ast, options).code);
  return generate(ast, options).code;
};



module.exports = {
  parsers: {
    babel: {
      ...parsers.babel,
      preprocess: parseIt,
    },
  },
};

The below code is inside the prettier.config.js


module.exports = {
  plugin: ["./plugin.js"]
};

Is there was anything wrong in my code, can any one help me ?

0

There are 0 best solutions below