ElectronJs:花括号'{}'在包json中是什么意思

ElectronJs: What does curly braces '{}' mean in package json

我正在查看一些电子 package.json 示例,其中我发现了一些 interpolations 如下所示:

"updater": {
    "urls": {
      "darwin": "{{& SQUIRREL_UPDATES_URL }}/update/%CHANNEL%/darwin?version=%CURRENT_VERSION%",
      "win32": "{{& SQUIRREL_UPDATES_URL }}/update/%CHANNEL%/win32",
      "linux": "{{& SQUIRREL_UPDATES_URL }}/update/%CHANNEL%/linux"
    }
  }

  "piwik": {
    "serverUrl": "{{& PIWIK_SERVER_URL }}"
  },
  "sentry": {
    "dsn": "{{& SENTRY_DSN_PRIVATE }}"
  }

我真的不知道以下内容:

  1. 这个{{}}在json
  2. 中是什么意思
  3. 这些变量存在于何处
  4. &{{}}"{{& SENTRY_DSN_PRIVATE }}"
  5. 中是什么意思

谁能解释一下就太好了。非常感谢。

我猜你说的是 Whatsie and it's package.json

如果您查看位于文件 tasks/compile.coffee 中的 Gulp 任务之一,您将能够看到以下行(在 CoffeeScript 中):

# Move package.json
gulp.task 'compile:' + dist + ':package', ['clean:build:' + dist], ->
  gulp.src './src/package.json'
    .pipe mustache process.env
    .pipe gulp.dest dir

这里实际的 package.json is being passed to a mustache template engine - it receives a template as a first argument (package.json 就像一个模板)和要插入模板的数据作为第二个参数 - process.env

As package.json acts like a template for mustache, you can use mustache 语法。 大括号 {{}} 是它的一部分,它们用作占位符,在编译模板时将被实际数据替换。在mustache docs中你还可以找到一行:

You can also use & to unescape a variable: {{& name}}

所以{{& name}}是为了防止值被转义。否则,如果你不使用 & 并且输出的值有一些危险字符,它们将被更安全的字符替换(最初是为了防止模板中的 XSS),因此它会转换初始值,即并不总是你想要的。在这种情况下,作者希望保留原始值。

回到 process.env - 它是一个对象,可以访问 environment variables in Node.JS. There is a file in repository .env-example with an example of env variables developer has to set in order to have the application work differently in different environments (for example on local machine or CI server). Names of some of the variables in this file are the ones that are used in a package.json 作为模板占位符 - 我猜应用程序的作者使用所有这些来简化不同环境的构建过程。