【问题标题】:How to ignore node_modules from prettier如何从 prettier 中忽略 node_modules
【发布时间】:2019-04-19 08:47:11
【问题描述】:

使用相当快速的美化目的

Prettier 配置和 Eslint 规则正在影响 node_modules。所以,想跳过它。

因此,尝试创建其中定义了 node_modules 的 .prettierignore 文件

有如下配置规则:

"lint": "pretty-quick & eslint "src/**/*.{js,jsx}" --quiet --fix"

给出错误为:

SyntaxError: Nested mappings are not allowed in compact mappings (8:9)
  6 | artifact: 'file://dcs.tar.gz'
  7 | deploymentStrategy: default
> 8 | config: artifact:
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 9 |
    | ^
    at e (/Users/mithha/Documents/dcs/ui/ui-plugins/dcs/node_modules/prettier/parser-yaml.js:1:323)
    at Object.parse (/Users/mithha/Documents/dcs/ui/ui-plugins/dcs/node_modules/prettier/parser-yaml.js:1:156977)
    at Object.parse$2 [as parse] (/Users/mithha/Documents/dcs/ui/ui-plugins/dcs/node_modules/prettier/index.js:7138:19)
    at coreFormat (/Users/mithha/Documents/dcs/ui/ui-plugins/dcs/node_modules/prettier/index.js:10398:23)
    at format (/Users/mithha/Documents/dcs/ui/ui-plugins/dcs/node_modules/prettier/index.js:10570:16)
    at formatWithCursor (/Users/mithha/Documents/dcs/ui/ui-plugins/dcs/node_modules/prettier/index.js:10582:12)
    at /Users/mithha/Documents/dcs/ui/ui-plugins/dcs/node_modules/prettier/index.js:34924:15
    at format (/Users/mithha/Documents/dcs/ui/ui-plugins/dcs/node_modules/prettier/index.js:34943:12)
    at exports.default (/Users/mithha/Documents/dcs/ui/ui-plugins/dcs/node_modules/pretty-quick/dist/formatFiles.js:18:41)
    at exports.default (/Users/mithha/Documents/dcs/ui/ui-plugins/dcs/node_modules/pretty-quick/dist/index.js:54:29)
✨  Done in 4.19s.

也试过这样配置:

"lint": "pretty-quick --ignore-path=.prettierignore & eslint \"src/**/*.{js,jsx}\" --quiet --fix"

但没有运气。

【问题讨论】:

    标签: javascript lint prettier


    【解决方案1】:

    根据 npm 包描述,prettier 会忽略你在.prettierrc.prettierignore.editorconfig 中写入路径的文件。

    如果您想了解更多信息,可以在下面查看。

    https://www.npmjs.com/package/pretty-quick#configuration-and-ignore-files

    【讨论】:

    • 它不起作用,这就是发布错误的原因
    • 您是否尝试插入所有模式的路径?
    【解决方案2】:

    .eslintignore 文件中,添加以下行:

    /node_modules/
    

    【讨论】:

    • 将这些选项对象的行添加到 webpack.base.conf.js 文件 module: { rules: [ options: { formatter: require('eslint-friendly-formatter'), emitWarning: true }, ...
    • 我们没有配置 webpack。有没有可能在.eslintrc 内部处理它?
    • 在控制台试试这行:npm i -g eslinteslint --ignore-path .gitignore node_modules/*
    • npm ERR! Could not install ".gitignore" as it is not a directory and is not a file with a name ending in .tgz, .tar.gz or .tar
    • eslint --ignore-path node_modules/*
    【解决方案3】:

    我们只能通过将--staged 传递给现有命令来对暂存文件进行更改,如下所示是我现在想出的唯一解决方案。

    "lint": "pretty-quick --staged & eslint "src/**/*.{js,jsx}" --quiet --fix"
    

    【讨论】:

      【解决方案4】:

      Prettier 的 CLI 默认忽略 node_modules。

      【讨论】:

      猜你喜欢
      • 2021-05-21
      • 2018-03-06
      • 2016-10-15
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 2018-06-22
      相关资源
      最近更新 更多