Skip to content

Commit c37371b

Browse files
authored
feat: change interactions between files array and ignore files to be more consistent (#88)
BREAKING CHANGE: this module now follows a strict order of operations when applying ignore rules. if a `files` array is present in the package.json, then rules in `.gitignore` and `.npmignore` files from the root will be ignored.
1 parent a2c96ef commit c37371b

24 files changed

+710
-627
lines changed

README.md

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# npm-packlist
22

3-
[![Build Status](https://travis-ci.com/npm/npm-packlist.svg?token=hHeDp9pQmz9kvsgRNVHy&branch=master)](https://travis-ci.com/npm/npm-packlist)
4-
53
Get a list of the files to add from a folder into an npm package.
64

75
These can be handed to [tar](http://npm.im/tar) like so to make an npm
@@ -29,8 +27,8 @@ This uses the following rules:
2927

3028
1. If a `package.json` file is found, and it has a `files` list,
3129
then ignore everything that isn't in `files`. Always include the
32-
readme, license, notice, changes, changelog, and history files, if
33-
they exist, and the package.json file itself.
30+
readme, license, licence and copying files, if they exist, as well
31+
as the package.json file itself.
3432
2. If there's no `package.json` file (or it has no `files` list), and
3533
there is a `.npmignore` file, then ignore all the files in the
3634
`.npmignore` file.
@@ -64,17 +62,14 @@ semantics are applied.
6462

6563
### Interaction between `package.json` and `.npmignore` rules
6664

67-
For simplicity, it is best to use _either_ a `files` list in `package.json`
68-
_or_ a `.npmignore` file, and not both. If you only use one of these
69-
methods, you can skip this documentation section.
65+
In previous versions of this library, the `files` list in `package.json`
66+
was used as an initial filter to drive further tree walking. That is no
67+
longer the case as of version 6.0.0.
7068

71-
The `files` list in `package.json` is used to direct the exploration of the
72-
tree. In other words, that's all the walker will ever look at when
73-
exploring that level.
69+
If you have a `package.json` file with a `files` array within, any top
70+
level `.npmignore` and `.gitignore` files *will be ignored*.
7471

75-
In some cases this can lead to a `.npmignore` file being ignored. If a
76-
_directory_ is listed in `files`, then any rules in a root or nested
77-
`.npmignore` files will be honored.
72+
If a _directory_ is listed in `files`, then any rules in nested `.npmignore` files within that directory will be honored.
7873

7974
For example, with this package.json:
8075

@@ -88,59 +83,21 @@ a `.npmignore` file at `dir/.npmignore` (and any subsequent
8883
sub-directories) will be honored. However, a `.npmignore` at the root
8984
level will be skipped.
9085

91-
Conversely, with this package.json:
86+
Additionally, with this package.json:
9287

9388
```
9489
{
9590
"files": ["dir/subdir"]
9691
}
9792
```
9893

99-
a `.npmignore` file at `dir/.npmignore` will not be honored.
100-
101-
Any specific file matched by a glob or filename in the package.json `files`
102-
list will be included, and cannot be excluded by any `.npmignore` files in
103-
nested directories, or by a `.npmignore` file in the root package
104-
directory, unless that root `.npmignore` file is also in the `files` list.
105-
106-
The previous (v1) implementation used in npm 6 and below treated
107-
`package.json` as a special sort of "reverse ignore" file. That is, it was
108-
parsed and handled as if it was a `.npmignore` file with `!` prepended to
109-
all of the globs in the `files` list. In order to include children of a
110-
directory listed in `files`, they would _also_ have `/**` appended to them.
111-
112-
This is tricky to explain, but is a significant improvement over the
113-
previous (v1) implementation used in npm 6 and below, with the following
114-
beneficial properties:
115-
116-
- If you have `{"files":["lib"]}` in `package.json`, then the walker will
117-
still ignore files such as `lib/.DS_Store` and `lib/.foo.swp`. The
118-
previous implementation would include these files, as they'd be matched
119-
by the computed `!lib/**` ignore rule.
120-
- If you have `{"files":["lib/a.js","lib/b.js"]}` in `package.json`, and a
121-
`lib/.npmignore` containing `a.js`, then the walker will still include
122-
the two files indicated in `package.json`, and ignore the
123-
`lib/.npmignore` file. The previous implementation would mark these
124-
files for inclusion, but then _exclude_ them when it came to the nested
125-
`.npmignore` file. (Ignore file semantics dictate that a "closer" ignore
126-
file always takes precedence.)
127-
- A file in `lib/pkg-template/package.json` will be included, and its
128-
`files` list will not have any bearing on other files being included or
129-
skipped. When treating `package.json` as just Yet Another ignore file,
130-
this was not the case, leading to difficulty for modules that aim to
131-
initialize a project.
132-
133-
In general, this walk should work as a reasonable developer would expect.
134-
Matching human expectation is tricky business, and if you find cases where
135-
it violates those expectations, [please let us
136-
know](https://github.com/npm/npm-packlist/issues).
94+
a `.npmignore` file at `dir/.npmignore` will be honored, as well as `dir/subdir/.npmignore`.
95+
96+
Any specific file matched by an exact filename in the package.json `files` list will be included, and cannot be excluded, by any `.npmignore` files.
13797

13898
## API
13999

140100
Same API as [ignore-walk](http://npm.im/ignore-walk), just hard-coded
141101
file list and rule sets.
142102

143-
The `Walker` and `WalkerSync` classes take a `bundled` argument, which
144-
is a list of package names to include from node_modules. When calling
145-
the top-level `packlist()` and `packlist.sync()` functions, this
146-
module calls into `npm-bundled` directly.
103+
The `Walker` class will load an [arborist](https://github.com/npm/cli/tree/latest/workspaces/arborist) tree, and if any bundled dependencies are found will include them as well as their own dependencies in the resulting file set.

0 commit comments

Comments
 (0)