Proposal: a new scheme for module and library names #619
Description
After talking to @vsmenon and @jmesserly about the constraints underlying module and library names, I want to put forth the following proposal. I believe this should still work for build systems like Bazel while being substantially more usable and comprehensible by end users. It would also fix #612.
First, I propose we do away with the --module-root
and --library-root
flags. They're currently used to determine the names of modules and libraries within those modules, so we'll need an alternate way to determine those.
For module names, I propose that by default the dev compiler uses the basename of the target file, post-processed if necessary to make it a valid Dart identifier. So if the user runs dartdevc -o module.js file1.dart file2.dart file3.dart
, the module's name would be "module". There would also be a --module-name
flag that would explicitly declare the name of the module. This would allow users with large build systems to provide their own module naming scheme that would allow them to avoid conflicts.
This also means that the compiler will no longer be able to determine the name of a dependency module from the path to its summary combined with the module root. To fix this, I propose that the module name be included in the summary, either by passing information to the analyzer or by adding some sort of dev compiler-specific header.
For library names, we can take advantage of the fact that they only need to be unique per-module and that the dev compiler knows the URIs of all files in the module at compile time. Each library name is computed by taking the shortest unique final sub-path of the library's path, and converting that to an identifier. For example, if a module contained only the library lib/src/foo.dart
, its name would just be foo
. However, if it also contained the library lib/foo.dart
, their names would be src_foo
and foo
, respectively.
Library names would also be explicitly definable by the user. When passing a library to the dev compiler on the command line, the user would be able to add a :
followed by an identifier to declare the identifier for that library. For example, dartdevc -o module.js file1.dart:one file2.dart
would use an explicit name (one
) for the library defined by file1.dart
, but the default (file2
) for file2.dart
.
Like module names, library names would be included in the summaries emitted by the dev compiler. The default library names could potentially be recomputed from scratch based on the library URIs, but that wouldn't work for explicitly-named libraries.