简体   繁体   English

dart中的lib / src和/ bin有什么区别?

[英]What is the difference between lib/src and /bin in dart?

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. 我知道lib /是存放所有库文件的位置,/ bin是存放命令行应用程序入口点的位置。 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 我知道它们都是公共lib /和bin,但我无法理解使用lib / src的约定,根据官方文档,该约定应包含: 实现代码

lib/ is the directory that contains shareable code. lib/是包含可共享代码的目录。 It can be shared 可以共享

  • to other top-level directories like bin/ , web/ , example/ , test/ , tool/ , ... in the same package 到同一包中的其他顶级目录,如bin/web/example/test/tool/ ,...
  • to other packages that have this package as a dependency. 到其他依赖于此软件包的软件包。

lib/src by convention contains the private implementation of the public API exposed by lib/ or lib/xxx where xxx is not src . 按照惯例, lib/src包含由lib/lib/xxx公开的公共API的私有实现,其中xxx不是src

bin is reserved for command line apps and contains the Dart entry point scripts to execute them (the files that contain main() {...} ). bin是为命令行应用程序保留的,包含执行它们的Dart入口点脚本(包含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 ). pubspec.yaml您可以定义可执行文件https://www.dartlang.org/tools/pub/pubspec#executables ,该文件使您可以通过执行foo来执行dart somePath/bin/foo.dart ,从而从bin/运行脚本(使用pub global activate my_package_with_foo )。

See Pub Package Layout Conventions - Implementation files 请参见发布包布局约定-实施文件

The libraries inside lib are publicly visible: other packages are free to import them. lib内部的lib是公开可见的:其他软件包可以自由导入。 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 of lib called src . 它们位于lib的子目录src 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 in lib , scripts in bin , and tests) but you should never import from another package's lib/src directory. 您可以从同一个程序包中的其他Dart代码中导入位于lib/src中的库(就像lib其他库, bin和测试中的脚本一样),但是绝对不要从另一个包的lib/src目录中导入。 Those files are not part of the package's public API, and they might change in ways that could break your code. 这些文件不是软件包公共API的一部分,它们可能会以破坏代码的方式进行更改。

When you use libraries from within your own package, even code in src , you can (and should) still use package: to import them. 当您从自己的程序包中使用库时,即使是src代码,也可以(并且应该)仍然使用package:来导入它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM