Reputation: 7973
I know that lib/ is where we put all our library files and /bin is where we put our entrypoint for our command-line app. I know both of them are public lib/ and bin but i'm unable to understand the convention of using lib/src which according to the official docs should contain: implementation code
Upvotes: 13
Views: 4760
Reputation: 657929
lib/
is the directory that contains shareable code.
It can be shared
bin/
, web/
, example/
, test/
, tool/
, ... in the same packagelib/src
by convention contains the private implementation of the public API exposed by lib/
or lib/xxx
where xxx
is not src
.
bin
is reserved for command line apps and contains the Dart entry point scripts to execute them (the files that contain main() {...}
).
In pubspec.yaml
you can define executables https://www.dartlang.org/tools/pub/pubspec#executables that allows you to run scripts from bin/
by just executing foo
to have dart somePath/bin/foo.dart
executed (using pub global activate my_package_with_foo
).
Upvotes: 26
Reputation: 76323
See Pub Package Layout Conventions - Implementation files
The libraries inside
lib
are publicly visible: other packages are free to import them. But much of a package’s code is internal implementation libraries that should only be imported and used by the package itself. Those go inside a subdirectory oflib
calledsrc
. You can create subdirectories in there if it helps you organize things.You are free to import libraries that live in
lib/src
from within other Dart code in the same package (like other libraries inlib
, scripts inbin
, and tests) but you should never import from another package’slib/src
directory. Those files are not part of the package’s public API, and they might change in ways that could break your code.When you use libraries from within your own package, even code in
src
, you can (and should) still usepackage:
to import them.
Upvotes: 9