Prettier

作者: _于曼丽_ | 来源:发表于2022-05-05 10:25 被阅读0次

    格式化代码风格的前端工具

    在 CLI 中使用

    本地安装 Prettier

    npm i -D prettier
    

    在项目根目录中创建配置文件

    • .prettierrc.jsprettier.config.js
    • .prettierrc.json
    • package.json 中的 prettier 字段
    // prettier.config.js or .prettierrc.js
    module.exports = {
      // 是否使用尾逗号
      trailingComma: "none",
      // 按 Tab 键的时候的缩进方式,true 使用 tab 缩进,false 将 tab 转换为空格缩进
      useTabs: false,
      // useTabs: false 的时候,使用空格缩进缩进几个空格
      tabWidth: 4,
      // 语句结尾是否加分号
      semi: true,
      // 字符串是否使用单引号
      singleQuote: true
    };
    

    在项目根目录中创建忽略文件 .prettierignore,文件格式使用 gitignore syntax

    # Ignore artifacts:
    build
    

    默认会忽略 .git 目录和 node_modules 目录

    # Ignore artifacts by default 
    **/.git
    **/node_modules
    

    推荐在项目根目录中创建 .prettierignore,这样就可以在项目根目录下执行 prettier --write . 来格式化所有文件。

    执行 CLI 命令

    # 格式化当前目录中的所有文件(除了 .prettierignore 里忽略的文件)
    npx prettier --write .
    # 格式化指定目录中的所有文件
    npx prettier --write src/
    # 格式化某个具体文件
    npx prettier --write src/index.js
    # 格式化与 glob 模式匹配的所有文件
    npx prettier --write src/**/*.test.js
    

    配置文件

    // prettier.config.js or .prettierrc.js
    module.exports = {
        // 一行最多 100 字符
        printWidth: 100,
        // 使用 4 个空格缩进
        tabWidth: 4,
        // 不使用缩进符,而使用空格
        useTabs: false,
        // 尾逗号规则
        trailingComma: "es5",
        // 行尾需要有分号
        semi: true,
        // 使用单引号
        singleQuote: true,
        // 对象的 key 仅在必要时用引号
        quoteProps: 'as-needed',
        // jsx 不使用单引号,而使用双引号
        jsxSingleQuote: false,
        // 末尾不需要逗号
        trailingComma: 'none',
        // 大括号内的首尾需要空格
        bracketSpacing: true,
        // jsx 标签的反尖括号需要换行
        jsxBracketSameLine: false,
        // 箭头函数,只有一个参数的时候,也需要括号
        arrowParens: 'always',
        // 每个文件格式化的范围是文件的全部内容
        rangeStart: 0,
        rangeEnd: Infinity,
        // 不需要写文件开头的 @prettier
        requirePragma: false,
        // 不需要自动在文件开头插入 @prettier
        insertPragma: false,
        // 使用默认的折行标准
        proseWrap: 'preserve',
        // 根据显示样式决定 html 要不要折行
        htmlWhitespaceSensitivity: 'css',
        // 换行符使用 lf
        endOfLine: 'lf'
    };
    

    在 VSCode 中使用

    1. 本地安装 Prettier npm i -D prettier
    2. 项目根目录中创建配置文件 .prettierrc.js
    3. 项目根目录中创建忽略文件 .prettierignore
    4. 在 VSCode 中安装 Prettier 插件:shift command x 搜索并安装 Prettier 插件
    5. 通过以下任意一种方式设置保存时自动使用 Prettier 格式化:
      • 控制面板设置:command + , 搜索 format on save,打钩
      • 修改 vscode 配置文件:"editor.formatOnSave": true

    新版本的 vscode 里面自带一个 javascript 和 typescript 的 format 工具,默认使用该工具进行格式化。因此在保存文件的时候,会使用自带的 format 工具格式化,而不会使用 Prettier 格式化。如果我们希望保存的时候使用 Prettier 来格式化,要么关闭 vscode 自带的 format 工具,要么设置优先使用 Prettier 来 格式化。

    关闭 vscode 自带的 format 工具

    command + , 打开控制面板,搜索 TypeScript Format Enable 和 Javascript Format Enable,选项不要打钩。

    设置完之后,就会自动在工作区或者用户区的 vscode 配置文件里添加如下配置:

    {
        "javascript.format.enable": false,
        "typescript.format.enable": false
    }
    

    你也可以直接在对应的配置文件里手动添加这两行来关闭 vscode 自带的 format 工具。

    设置优先使用 Prettier 来格式化

    这种方式不需要关闭 vscode 自带的 format 工具,你打开一个 .js 文件,按 shift command P 打开命令行面板,搜索 Format Document,点击它,会提示“文件有多个格式化程序,选择默认格式化程序以继续”。点击配置,选择 Prettier 即可。

    设置完之后,就会自动在用户区的 vscode 配置文件里面添加如下配置:

    "[javascript]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    }
    

    你也可以直接在用户区或者工作区的配置文件里面手动添加上面的配置。

    如果你希望在 .ts 类型的文件里面也能够优先使用 Prettier 格式化,你需要打开一个 .ts 类型的文件,然后重复以上的步骤。就会自动在用户区的 vscode 配置文件里面添加如下配置:

    "[typescript]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    }
    

    你也可以直接在用户区或者工作区的配置文件里面手动添加上面的配置。

    其他类型的文件也如此,例如 .vue

    .editorconfig 文件一起使用

    如果存在 .editorconfig 文件,Prettier 会根据 .editorconfig 文件中的以下选项自动转换为对应的 Prettier 配置信息:

    • max_line_length => printWidth
    • tab_width => tabWidth
    • indent_style => useTabs
    • end_of_line => endOfLine

    自定义的 Prettier 配置文件中的对应项可以覆盖从 .editorconfig 文件自动转换过来的对应项。

    其实有了 Prettier 就不需要 .editorconfig 文件了

    相关文章

      网友评论

          本文标题:Prettier

          本文链接:https://www.haomeiwen.com/subject/dysnyrtx.html