简体   繁体   English

package.json是否支持复合变量?

[英]Does package.json support compound variables?

A project that respects the semver directory structure for build artefacts is beginning soon, and package.json or.nmprc would seem to be the right place to define this metadata.一个尊重构建工件的 semver 目录结构的项目即将开始,package.json 或 .nmprc 似乎是定义此元数据的正确位置。 This is some preliminary code that demonstrates how the goal is intended to be achieved:这是一些初步代码,演示了如何实现目标:

{
  "name": "com.vendor.product"
, "version": "0.0.0"
, "directories": {
    "build": "./out"
  }
, "main": "./${npm_directories_build}/${npm_package_name}/${npm_package_version}/${npm_package_name}.js"
, "exports": "${npm_package_main}"
, "scripts": {
    "echo": "echo\"${npm_package_exports}\""
  }
}

I expected我期望

npm run echo

to print the compound variable result to standard output,将复合变量结果打印到标准 output,

./out/com.vendor.product/0.0.0/com.vendor.product.js

but instead, it prints the literal text但相反,它打印文字

${npm_package_export}

I attempted to use array variables in.npmrc我试图在 .npmrc 中使用数组变量

outpath[]=./out
outpath[]=/${npm_package_name}
outpath[]=/${npm_package_version}

But

...
{
  "echo": "echo \"${npm_config_outpath}\""
}

Simply prints an empty newline简单地打印一个空的换行符


It was expected that package.json supports compound variables, but this assumption is now in question.原本预计package.json支持复合变量,但现在这个假设有问题。 I have checked documentation, but either I am missing something or such is not defined.我已经检查了文档,但要么我遗漏了某些东西,要么没有定义。 Long hand repetition of the same data is to be avoided (eg multiple references to package variables in order to make a single path).避免长时间重复相同的数据(例如多次引用 package 变量以形成单一路径)。 It is intended for package name and version to always dictate the location of the build files in a reliable and predictable manner.它旨在 package 名称和版本始终以可靠且可预测的方式指示构建文件的位置。 If compound variables are not supported, could you clarify how.npmrc array variables actually work?如果不支持复合变量,您能否阐明 .npmrc 数组变量的实际工作原理? Failing that, could you recommend an alternative method to achieve the same ends?如果做不到这一点,您能否推荐一种替代方法来达到相同的目的? Many thanks!非常感谢!

Searched documentation:搜索文档:

Short Answer:简短回答:

"Does package.json support compound variables?" “package.json支持复合变量吗?”

Unfortunately no, not for the way you are wanting to use them.不幸的是不,不是因为你想要使用它们的方式。 It only has package json vars which can be used in npm scripts only.它只有package json 变量,只能在npm 脚本中使用。 For example on *Nix defining the echo script as:例如在 *Nix 上将echo显脚本定义为:

"echo": "echo $npm_package_version"

or on Windows defining it as:或在 Windows 上将其定义为:

"echo": "echo %npm_package_version%"

will print the version eg 0.0.0 .将打印版本,例如0.0.0

Note: cross-env provides a solution for a single syntax that works cross-platform.注意: cross-env为跨平台工作的单一语法提供了解决方案。

You certainly cannot include parameter substitution ${...} elsewhere in package.json fields except for the scripts section.除了scripts部分,您当然不能在package.json字段的其他地方包含 参数替换${...}


Additional info:附加信息:

Regarding your subsequent comment:关于您随后的评论:

How array values defined in .npmrc can be used in package.json .npmrc 中定义的数组值如何在.npmrc中使用

AFAIK I don't think you can. AFAIK 我不认为你可以。 For example let's say we;例如,假设我们;

  1. Save this contrived .npmrc in the root of the project directory.将这个人为的.npmrc保存在项目目录的根目录中。

    .npmrc .npmrc

     quux[]="one" quux[]="two" quux[]="three" foo=foobar
  2. Then cd to the project directory and run the following command to print all environment variables:然后cd到项目目录,运行如下命令打印所有环境变量:

     npm run env

    As you can see, the npm_config_foo=foobar environment variable has been added by npm. However for the quux array there is no npm_config_quux=[... ] environment variable added.如您所见,npm 添加了npm_config_foo=foobar环境变量。但是对于quux数组,没有npm_config_quux=[... ]环境变量。

    So, in npm scripts using package.json vars the following does work:因此,在使用package.json 变量的 npm 脚本中,以下内容有效:

     "echo": "echo $npm_config_foo"

    However the following, (for referencing the array), does not - simply because it does not exist;然而,下面的(用于引用数组)并不——仅仅因为它不存在;

     "echo": "echo $npm_config_quux"

The ini node.js package: ini node.js package:

Maybe consider investigating the ini node.js package that npm utilizes for parsing .npmrc files.也许考虑调查 npm 用于解析.npmrc文件的ini node.js package。 For example:例如:

  1. If you run the following command to install the package in your project:如果运行以下命令在项目中安装 package:

     npm i -D ini
  2. Then define the npm echo script as per this:然后按照以下定义 npm echo显脚本:

     "scripts": { "echo": "node -e \"var fs = require('fs'), ini = require('ini'); var config = ini.parse(fs.readFileSync('./.npmrc', 'utf-8')); console.log(config.quux)\"" }

    Note it uses the nodejs command line option -e to evaluate the JavaScript code.请注意,它使用 nodejs 命令行选项-e来评估 JavaScript 代码。 It essentially executes the following:它主要执行以下操作:

     var fs = require('fs'), ini = require('ini'); var config = ini.parse(fs.readFileSync('./.npmrc', 'utf-8')); console.log(config.quux);
  3. Then given the contrived .npmrc file that I mentioned previously when running:然后给出我之前在运行时提到的人为设计的.npmrc文件:

     npm run echo

    it will print:它会打印:

    [ 'one', 'two', 'three' ]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM