npm 发布 .gitignored 文件

npm publish .gitignored file

情况如下

我有一个 .gitignore 文件:

node_modules
npm-debug.log
/index.js # this is a build output, I don't want it in the repo

和一个 package.json 文件(只有相关部分):

{
  "main": "index.js",
  "files": [
    "index.js", // however I want this in the npm package
    "readme.md",
    "package.json"
  ],
  "scripts": {
    "build": "rollup -c rollup.config.js", // this builds index.js ...
    "lint": "eslint **/*.js --config .eslintrc",
    "test": "jest --no-cache",
    "prepublish": "npm run lint && npm test && npm run build" // ...before publishing
  }
}

我发布第一版的时候,省略了index.js,只发布了readme.mdpackage.json。我唯一可以假设的是 .gitignore 导致了这个,因为根据文档 .gitignore 替代 .npmignore 如果后者不存在(或者可能 2 一起使用,不知道) .

那么有没有办法不在 git 存储库中包含该文件,而是在 npm 包中包含该文件?

设法通过添加 .npmignore 文件解决此问题:

# all the crap that's not neccessary for the npm module to work
node_modules
src
test
.babelrc
.eslintignore
.eslintrc
.gitignore
.npmignore
.travis.yml
rollup.config.js

并从 package.json 中完全删除 files 属性。