'How to exclude files from pub publish for Dart packages?

Is there any way to exclude certain files or directories from pub publish for dart packages?

I am publishing a package that has so many screenshots. I want them to upload on GitHub but not on Pub.Dev. The intention behind this is to reduce the package size.



Solution 1:[1]

You can now create a .pubignore file to do this. See this PR for more details on how this works.

Solution 2:[2]

You can add them in .gitignore while publishing to pub.dev and later remove from .gitignore before pushing to GitHub.

Referenced from dart documentation.

What files are published? All files in your package are included in the published package, with the following exceptions: Any packages directories. Your package’s lockfile. If you aren’t using Git, all hidden files (that is, files whose names begin with .). If you’re using Git, any files ignored by your .gitignore file. Be sure to delete any files you don’t want to include (or add them to .gitignore). pub publish lists all files that it’s going to publish before uploading your package, so examine the list carefully before completing your upload.

Solution 3:[3]

You have three options to exclude files or directories from getting published to pub.dev.

  1. Add your files or directories to .pubignore or .gitignore file.

  2. Hide your file or directory by giving it a name which starts with a . (dot)

  3. Create a new directory with packages name and put your files there.

If a directory contains both a .pubignore file and a .gitignore file, dart pub publish will not read that directory's .gitignore file i.e. you can overrule .gitignore file by creating a .pubignore file.

Solution 4:[4]

To expand a little on some of the existing answers, the dart docs state:

All files under the package root directory are included in the published package, with the following exceptions:

  • Any hidden files or directories — that is, files with names that begin with dot (.)
  • Any directories with the name packages
  • Files and directories ignored by a .pubignore or .gitignore file

...

(If a directory contains both a .pubignore file and a .gitignore file, then dart pub publish doesn’t read that directory’s .gitignore file.) The format of .pubignore files is the same as the .gitignore file format

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prerak Mann
Solution 2 dev-aentgs
Solution 3 iDecode
Solution 4