First, a static solution:
You can include a .npmignore
file per directory that you want to exclude.
As the doc states:
Like git, npm looks for .npmignore and .gitignore files in all subdirectories of your package, not only the root directory.
I created an example of this in https://github.com/dral/npmignore-example (and the corresponding package npmignore-example
).
file structure
index.js
.npmignore
bar/index.js
baz/index.js
foo/index.js
/.npmignore
the root .npmignore removes only the baz directory. the nested foo/.npmignore removes all content (from this point on).
The installed package includes only
index.js
bar/index.js
In order to do so dynamically you can use a script that adds and then removes a simple .npmignore
file in the selected folder(s).
echo '*' > .r2g/.npmignore && npm publish && rm .r2g/.npmignore
Then if you need to automate this, consider using prepublish
and postpublish
scripts that take into account an env variable so you can use something like NPM_IGNORE=.r2g npm publish
.
Hope this helps.