node.js - 发布nodejs包时,有些文件上传到npm但没有上传到Github。为什么?

标签 node.js github npm

发布nodejs包时,有些文件上传到npm,但没有上传到Github。为什么?

例如,包是assets-webpack-plugin

在其 package.jsonfiles 部分中它包括dist/:

"files": [
  "index.js",
  "dist/"
],

但是在 .gitignore文件它仍然包含 dist:

node_modules
dist
tmp
npm-debug.log*
.package.json

# Lock files shouldn't be committed for libraries https://stackoverflow.com/a/40206145
yarn.lock
package-lock.json

问题是为什么 Github 上的项目会忽略这些文件,而 npm 必须包含它们?现在它们位于 package.jsonfiles 部分,这意味着这些文件对包很重要 - 为什么 .gitignore他们?

最佳答案

让我们首先了解有关此项目的“构建”“发布”步骤的更多信息...

如果您检查项目package.json文件中包含与您的问题相关的 scripts 部分中定义的以下两个脚本:

"scripts": {
    ...
    "build": "babel index.js --out-dir dist && babel lib --out-dir dist/lib",
    "prepublish": "npm run build"
},

assets-webpack-plugin的维护者将包发布到 npm 他们使用 npm publish命令。

运行npmpublish...后,会发生以下步骤:

  1. package.json 中定义的 prepublish 脚本(称为 pre hook )会自动被调用并运行 build 脚本。

  2. build 脚本本质上利用 babel-cli用于开始转译项目源代码的过程的工具(即源代码是您在项目 GitHub 存储库中看到的内容)。生成的转译代码将发布到 npm。

    注意:在 build 脚本中,输出文件保存到 distdist/lib 文件夹中。

上述步骤(1 和 2)发生在 npmpublish... 命令将包发布到 npm 之前。

<小时/>

The question is why does the project on Github ignore the files while npm has to contain them?

  1. 让我们解决问题中的“为什么 Github 上的项目会忽略这些文件”部分:

    • 保存到 dist 文件夹的文件是根据上述步骤 1 和 2 自动从源代码生成的。

    • 生成“构建”文件的过程是可重复的。此“构建”过程的结果文件输出是源代码文件的派生物。

    • 本质上,没有理由将“build”文件(即自动生成到 dist 文件夹的文件)推送到GitHub 存储库,因为项目维护者知道他们可以通过运行 npm build 脚本简单地从源代码重新生成衍生版本。

  2. 让我们尝试解决问题中的“...而 npm 必须包含它们”部分:

    • npm 必须包含 dist 文件夹中的文件,因为它们本质上是发布到 npm 注册表的文件。

关于node.js - 发布nodejs包时,有些文件上传到npm但没有上传到Github。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59387174/

相关文章:

git - 致命的 : The remote end hung up unexpectedly with a fetch upstream

node.js - 错误 : Could not locate the bindings file. 已尝试:

react-native - 在 native react 中获取 "Cannot read property ' pickAlgorithm' of null”错误

node.js - 使用 Jade 和 NodeJS 插入图像

javascript - 在 Google Cloud Functions 中使用 process.exit

javascript - 如何解析电子邮件文本的 "somebody wrote..."部分?

github - 从 Github 的 Wiki 页面下载 `.md` 文件的干净方法

node.js - TypeScript 中的 Http 请求

javascript - 无法在 Javascript 中使用第 n 个子级选择器内的变量值

ios - 如何在安装了 Cocoapods 的 React Native 上安装扩展?