Prettier Code Formatter for Apex, Aura, and LWC
Prettier code formatter supports Aura and Lightning Web Components (LWC) as well as standard file formats such as JSON, Markdown, HTML, and JavaScript. Prettier can also support Apex if you install Prettier Apex plugin authored by Dang Mai.
This plugin requires NodeJS and npm in your environment. For setup details, refer to Downloading and installing Node.js and npm
.
To install the Prettier Apex plugin:
-
Navigate to the top-level of your project
-
Check if your project contains the
package.json
file. If not, run:npm init
and accept all the default options. -
Run:
npm install --save-dev --save-exact prettier prettier-plugin-apex
You must create a configuration file .prettierrc
in the root of your project. To learn more about formatting options of Prettier, read other config options.
The "trailingComma": "none"
setting is required for Aura.
After creating the local configuration file, install the Prettier extension for VS Code. If you want to ensure that all your files are formatted whenever you save them, enable the editor.formatOnSave
setting in your User and Workspace Settings. You can use Prettier with a pre-commit tool to reformat your files before you commit the files. For more information, see set up a Git hook.
Prettier Apex plugin runs slower than most other formatters. Because saving is a critical operation, you can decide if you want to wait until the plugin completes formatting before saving the file. Read how VS Code handles slow save operations.