7

I am looking for a good way to ignore all .ts files in a project when I publish to NPM, I can do that by adding the following to my .npmignore file:

*.ts

but wait..actually I want to keep all the .d.ts files in my project, when I publish...

What can I add to my .npmignore file that will keep all .d.ts files but ignore all .ts files?

I assumed I have to use some form of regex to ignore all files that end with .ts that do not end with .d.ts

so the JS regex for this might look like:

*.\.[^d]\.ts

what the above regex should mean is match anything that ends with .ts that does not end with .d.ts

but of course we are stuck with what I believe to be less powerful regexes used by the OS etc.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

14

In the docs for .npmignore it states:

.npmignore files follow the same pattern rules as .gitignore files:

In which case you can negate the .d.ts files in .npmignore by using !. For example in your .npmignore you simply:

# ignore the .ts files
*.ts

# include the .d.ts files
!*.d.ts
RobC
  • 22,977
  • 20
  • 73
  • 80
  • so the include will override the ignore? – Alexander Mills Mar 06 '17 at 09:49
  • 1
    Yes the `.npmignore` config simply says ignore all `.ts` (i.e. `*.ts`) and by using the apostrophe `!` your saying but do track `.d.ts` (i.e. `!*.d.ts`), even though you're ignoring the `.ts` files with the previous statement above. – RobC Mar 06 '17 at 09:56
  • 1
    Yep, note that the negation using ! has to come last as far as I can tell. – Alexander Mills Oct 20 '17 at 06:56
  • 1
    this doesn't work for local file install `"my-package": "file:../shared"` but works for local tgz file install, `npm install ../../my-package/my-package-1.1.0.tgz` – JasonHsieh Aug 09 '22 at 07:49