简体   繁体   English

将 prettier 安装为 NPM package 和在 VS Code 中安装 prettier 扩展有什么区别

[英]what is the difference between installing prettier as a NPM package and installing prettier extension in VS Code

I'm new to web programming and I learned that we can type npm i -D prettier to install prettier as one of the devDependency and use it to format our code.我是 web 编程的新手,我了解到我们可以输入npm i -D prettier来安装 prettier 作为devDependency之一,并使用它来格式化我们的代码。 Then I found that there is a VS Code extension Pretty that does exactly the same thing.然后我发现有一个 VS Code 扩展 Pretty 可以做完全相同的事情。

I don't know what the differences are between the two.我不知道两者之间有什么区别。

If I only install the extension, can I format the codebase where the prettier npm package is not installed?如果我只安装扩展,我可以格式化没有安装更漂亮的 npm package 的代码库吗?

Also does the configuration process differ for these two?这两者的配置过程也不同吗? Which one is preferred?哪个是首选?

Functionally there is no difference, they will both work.在功能上没有区别,它们都可以工作。

The VS Code extension Prettier (not Pretty Formatter, that's different) includes a recent copy of the prettier npm package inside it, which it will use by default if you don't have the package installed via npm in your repo. The VS Code extension Prettier (not Pretty Formatter, that's different) includes a recent copy of the prettier npm package inside it, which it will use by default if you don't have the package installed via npm in your repo. See the extension page's section on Prettier Resolution .请参阅扩展页面的Prettier Resolution部分。

I want to use an example to illustrate why you might use npm over the extension.我想用一个例子来说明为什么你可以在扩展上使用 npm。

Lets say you worked on a team and you wanted everyone to have the same version of prettier.假设您在一个团队中工作,并且希望每个人都拥有相同版本的更漂亮。 It would be pretty hard to get everyone on the same version by VS Code extension.通过 VS Code 扩展让每个人都使用相同的版本是相当困难的。 Developers would have to manage the versioning on their own, and would be difficult to all be on the same version (if needed).开发人员必须自己管理版本控制,并且很难全部使用相同的版本(如果需要)。 But If you used npm and had the package.json/lock file in a repository you could all easily be on the same version.但是,如果您使用 npm 并在存储库中拥有 package.json/lock 文件,那么您都可以轻松地使用相同的版本。 If you needed to update to the same version you can just npm install and that will bring everyone on the same page.如果您需要更新到相同的版本,您只需npm install ,这将使每个人都在同一页面上。 It might not be so important for prettier, but think about packages with breaking changes to the latest version.对 prettier 来说可能不是那么重要,但请考虑对最新版本进行重大更改的软件包。

so to answer your questions:所以回答你的问题:

If I only install the extension, can I format the codebase where the prettier npm package is not installed?如果我只安装扩展,我可以格式化没有安装更漂亮的 npm package 的代码库吗?

When using the VS Code extension, you can use it to format any file.使用 VS Code 扩展时,您可以使用它来格式化任何文件。 If you don't have the npm package installed in a given project, the extension will use its built-in default package.如果您没有在给定项目中安装 npm package,则扩展将使用其内置的默认 package。

Also does the configuration process differ for these two?这两者的配置过程也不同吗? Which one is preferred?哪个是首选?

Both use .prettierrc for the (small) amount of things you can configure in Prettier.两者都使用.prettierrc来处理您可以在 Prettier 中配置的(少量)内容。 The VS Code extension comes with some additional configuration for how it should interact with your editor. VS Code 扩展附带了一些额外的配置,用于说明它应该如何与您的编辑器交互。

If you want your editor to do things like run prettier on save, or to be able to run it from the VS Code command palette, you should use the VS Code extension.如果你想让你的编辑器在保存时运行更漂亮,或者能够从 VS Code 命令面板运行它,你应该使用 VS Code 扩展。 Additionally, if you need a specific version of Prettier, or you need to run it from the command line, you can install it as an npm package in your repo.此外,如果您需要特定版本的 Prettier,或者您需要从命令行运行它,您可以将其安装为 npm package 在您的存储库中。 These two options are not mutually exclusive.这两个选项并不相互排斥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将eslint安装为扩展和安装为npm package有什么区别? - What is the difference between installing eslint as extension and installing as npm package? VS 代码中断语句中更漂亮的扩展 - Prettier extension in VS code breaks statement 漂亮的eslint,eslint的插件prettier和eslint的配置prettier有什么区别? - What's the difference between prettier-eslint, eslint-plugin-prettier and eslint-config-prettier? 更漂亮的扩展在 vs 代码中不起作用需要配置设置为 true - Prettier extension is not working in vs code Require config set to true VS Code 自动格式化更漂亮 - VS Code auto formatting with prettier 在 React 项目中使用 Prettier 安装 ESlint 的正确方法 - Proper way of Installing ESlint with Prettier in a React Project VS 代码中更漂亮的扩展不起作用,我尝试了包括 yt 教程在内的所有内容,但我所做的一切都没有帮助 - Prettier extension in VS code is not working, I tried everything including yt tutorials and what ever i did nothing helped 为什么 Prettier 不在 VS Code 中格式化代码? - Why does Prettier not format code in VS Code? 在 VS 代码中检测到无效的更漂亮的配置文件 - Invalid prettier configuration file detected in VS Code 如何在 VS Code 中启用 Prettier 突出显示? - How to enable Prettier highlighting in VS Code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM