10

I know I can't use them both at once, but is there a way to make .npmignore file extending .gitignore? I have dozens of rules in .gitignore and I want to use them all + one additional for npm package. How can I do it without duplicating all the rules?

Daniel Kucal
  • 8,684
  • 6
  • 39
  • 64

2 Answers2

2

I don't believe there's any mechanism to do this, but it should be pretty simple to script! Here's how I would tackle this:

Set up a prepack npm script in your package.jsonthat:

  1. Copies your .gitignore file to a .npmignore
  2. Adds your extended rules to the .npmignore file after the copy finishes. I would suggest defining these extra rules in a file somewhere, we'll call it extra_rules_file for clarity in the below example.

Then, optionally a postpack script that deletes your .npmignore now that you don't need it (and maybe don't want to commit it, since it's a generated file)


For example:

package.json

{
  "scripts": {
    "prepack": "cp .gitignore .npmignore && cat extra_rules_file >> .npmignore",
    "postpack": "rm .npmignore"
  }
}

extra_rules_file

whatever/rules/you/want/**/*
Hawkins
  • 700
  • 6
  • 21
  • Yeah, that'd be some solution and I gave it a point. But at the same time, the drawback in limiting developers' environment to Unix is too big for such a small thing. – Daniel Kucal Oct 01 '19 at 01:51
  • 1
    Yeah, this answer would limit you to Unix, or something like git bash on windows. However, you could instead just reference node.js scripts here that use the standard library to do these same `cp`, `cat`, and `rm` equivalents in a platform-independent way. – Hawkins Oct 01 '19 at 19:48
0

common-ignore (npm package) looks promising.

ridvanaltun
  • 2,595
  • 2
  • 15
  • 28