如何将 .npmignore 目录中的单个文件列入白名单?

How can I whitelist a single file in a directory in .npmignore?

我试图让 npm 只下载包 npm install 目录中的一个文件。

目录如下:

+- dist/
   +- 1.0.0/
   +- 1.0.1/
   +- ...lots of other dirs...
   +- file.js

我希望 npm 忽略除 file.js 之外的所有内容,所以我尝试在我的 .npmignore 中包含以下内容:

dist/
!dist/file.js

然而,当我安装包时,npm 仍然会下载 dist 中的所有目录。我认为这应该像 .gitignore 一样工作,但显然我在这里遗漏了一些东西。

是的,它正在使用 glob 模式工作:https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package

但我会有不同的方法:

dist/*
dist/.*
!dist/file.js

为了不忽略整个文件夹但它的内容(可能不需要第二行)。

建议:白名单而不是黑名单

回答用例,我会建议明确列出您要使用 files entry in package.json, rather than blacklisting with .npmignore, as that makes it is much more likely that you will not accidentally publish the wrong files. See also: https://medium.com/@jdxcode/for-the-love-of-god-dont-use-npmignore-f93c08909d8d

发布的内容

这里的 a concrete example package 看起来有点像:

{
  "files": [
    "gitignore",
    "cirodown",
    "cirodown.js",
    "cirodown.embed.min.css",
    "cirodown.local.min.css",
    "cirodown.min.css",
    "index.js",
    "main.liquid.html",
    "main.scss",
    "nodejs.js"
  ],
}