因为大多数开发人员都熟悉基于标记/注释的文档,所以我开始使用的约定是类似的。来尝尝:
{
"@comment dependencies": [
"These are the comments for the `dependencies` section.",
"The name of the section being commented is included in the key after the `@comment` 'annotation'/'tag' to ensure the keys are unique.",
"That is, using just \"@comment\" would not be sufficient to keep keys unique if you need to add another comment at the same level.",
"Because JSON doesn't allow a multi-line string or understand a line continuation operator/character, just use an array for each line of the comment.",
"Since this is embedded in JSON, the keys should be unique.",
"Otherwise JSON validators, such as ones built into IDEs, will complain.",
"Or some tools, such as running `npm install something --save`, will rewrite the `package.json` file but with duplicate keys removed.",
"",
"@package react - Using an `@package` 'annotation` could be how you add comments specific to particular packages."
],
"dependencies": {
...
},
"@comment scripts": {
"build": "This comment is about the build script.",
"start": [
"This comment is about the `start` script.",
"It is wrapped in an array to allow line formatting.",
"When using npm, as opposed to yarn, to run the script, be sure to add ` -- ` before adding the options.",
"",
"@option {number} --port - The port the server should listen on."
],
"test": "This comment is about the test script.",
},
"scripts": {
"build": "...",
"start": "...",
"test": "..."
}
}
注意:对于dependencies、devDependencies等部分,注释注释不能直接添加到配置对象中各个包依赖项的上面,因为npm期望键是npm包的名称。这就是@comment依赖的原因。
我喜欢在JSON中添加注释的注释/标记样式,因为@符号与普通声明不同。
年长的建议
以下是我之前的建议。它内联注释的脚本,但我已经意识到,这些注释显示为“命令”在一些工具(在VS Code > Explorer > NPM脚本部分)。最新的建议没有受到这个问题的影响,但是脚本注释不再位于同一位置。
{
"@comment dependencies": [
...
],
"dependencies": {
...
},
"scripts": {
"@comment build": "This comment is about the build script.",
"build": "...",
"@comment start": [
"This comment is about the `start` script.",
"It is wrapped in an array to allow line formatting.",
"When using npm, as opposed to yarn, to run the script, be sure to add ` -- ` before adding the options.",
"",
"@option {number} --port - The port the server should listen on."
],
"start": "...",
"@comment test": "This comment is about the test script.",
"test": "..."
}
}
注意:在某些情况下,例如在“scripts”对象中,一些编辑器/ ide可能会抱怨数组。在脚本上下文中,VS Code期望值为字符串——而不是数组。