prettify

Snippet

Usage

import prettify from './prettify';

const code = `
  () => {
    const [count, setCount] = useState(0);

    return <Button onClick={() => setCount((count) => count + 1)}>Count: {count}</Button>
  };
`;

const prettyCode = prettify(code);

// Result:

/*
() => {
  const [count, setCount] = useState(0);

  return (
    <Button onClick={() => setCount((count) => count + 1)}>
      Count: {count}
    </Button>
  );
};
*/

Implementation

import babelPlugin from 'prettier/plugins/babel';
import estreePlugin from 'prettier/plugins/estree';
import prettier from 'prettier/standalone';

/**
 * Formats code using Prettier
 * @param {string} code - Code to be formatted
 * @param {Object} config - Optional Prettier config
 * @return {string}
 */
export default async function prettify(code, config = {}) {
  try {
    return await prettier.format(code, {
      parser: 'babel',
      plugins: [babelPlugin, estreePlugin],
      printWidth: 80,
      ...config,
    });
  } catch (error) {
    console.error(error);
    return code;
  }
}
00000
© 2026Mykhaylo Ryechkin.All rights reserved.