Help Quickly Get Started and Use Prettier
Help quickly understand Prettier and how to use it.
Prettier (hereinafter referred to as Prt) is a code formatting tool that supports the following languages:
It removes all original styling and ensures that all output code conforms to a consistent style rule.
Essentially, Prt reads the user's code and rewrites it according to certain style rules, helping users quickly format code style through this styling.
shell
Now we can format code using the commands provided by Prt. Prt will detect and process files in the current folder, but first let's create a Prt configuration file to make better use of Prt.
The official website example creates a configuration file via a Node command:
shell
This script essentially creates a .prettierrc configuration file via Node; we can also directly create an empty .prettierrc file in the current folder. Prt will automatically load the configuration file and apply its settings.
At the same time, we also need to create a .prettierignore file, which is used to configure files that Prt should ignore during formatting (ignoring files that do not need formatting). For example:
shell
So even if you don't configure an ignore file, your final .prettierignore will not be empty, but will look like this:
shell
Prettier uses cosmiconfig for configuration file support. This means you can configure Prettier via (in order of precedence):
Prt uses cosmiconfig to provide configuration file support, which means you can control the priority of configuration files:
The configuration file will be resolved starting from the location of the file being formatted, and searching up the file tree until a config file is (or isn’t) found.
The configuration file will start resolving from the location of the file being formatted and search up the file tree until a config file is (or isn’t) found.
shell
We can also specify the directory of files to format:
shell
Or specify a certain file:
shell
Maybe you just want to check whether the code has been formatted by Prt, you can use the --check flag:
shell
Formatting code via command line can be tedious; you might think of using Node listeners or other methods to detect file changes and automatically execute the Prt formatting script. But we have a more convenient way — many editors now support Prt, allowing us to quickly format files while developing code.
The Prt official website shows ways to integrate Prt with some commonly used editors. Here we only briefly introduce how to use it in VSCode.
Search for Prettier - Code formatter in the VSCode extensions and install the plugin.
After installing the plugin, it is automatically enabled. Now let's modify the VSCode settings so that when we finish editing a file and save it (Ctrl + S), Prt will trigger formatting of the file.
In the top menu bar of VSCode, click File → Preferences → Settings → User → Text Editor → Formatting. Check Format on Save.
In theory, saving a code file supported by Prt will successfully trigger formatting, and VSCode will automatically load the Prt configuration file in the current directory. For users who use TypeScript or have installed the ESLint plugin, these tools themselves provide their own formatting rules. If your Prt configuration does not take effect, you can right-click the file, choose ... Format Document → Configure Default Formatter... → Prettier - Code formatter to switch the formatter used by VSCode.
The reason Prt was created is to get rid of the cumbersome configuration options in other formatting tools, which cause inconsistent formatting across different terminals in the same project. Therefore, Prt has very few style configuration options — since you are using Prt, you must first accept Prt's own set of rules.
You can view Prt's configuration items by clicking here to go to the official website.
When running, Prt automatically selects the corresponding parser based on the file extension. You can configure Prt to handle files it cannot understand:
js