Resolve relative imports in Protobufs compiled to JavaScript#298
Resolve relative imports in Protobufs compiled to JavaScript#298dmytro-grankin merged 98 commits intomasterfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## master #298 +/- ##
============================================
+ Coverage 72.68% 73.51% +0.82%
- Complexity 2139 2207 +68
============================================
Files 348 353 +5
Lines 9223 9425 +202
Branches 582 595 +13
============================================
+ Hits 6704 6929 +225
+ Misses 2367 2343 -24
- Partials 152 153 +1 |
|
@armiol PTAL. |
armiol
left a comment
There was a problem hiding this comment.
@dmytro-grankin please see my comments. The general issue is that you define basic terms poorly, and then all the code referencing the basic terms has unclear naming and, probably, mixed responsibility.
| import static com.google.common.base.Preconditions.checkState; | ||
|
|
||
| /** | ||
| * A path of an import in JavaScript. |
There was a problem hiding this comment.
Please clarify, whether it's a path to the imported file or an import statement at whole.
| * | ||
| * <p>The method skips a library name if it is present. | ||
| */ | ||
| public String filePath() { |
There was a problem hiding this comment.
With this name it looks Path, not String.
| } | ||
|
|
||
| /** | ||
| * Strings the current directory symbol as well as parent directory symbols in the path. |
There was a problem hiding this comment.
Strings is a typo.
Also, such a method in a public API looks weird. It looks a part of something more meaningful. Let's discuss.
| } | ||
|
|
||
| /** | ||
| * Obtains the path to the parent directory. |
There was a problem hiding this comment.
Again, bad naming and bad explanation. It's not a path.
Same below.
tools/proto-js-plugin/src/main/java/io/spine/js/generate/resolve/PackagePattern.java
Outdated
Show resolved
Hide resolved
| import io.spine.code.js.ImportPath; | ||
|
|
||
| /** | ||
| * An action performed to resolve particular types of imports. |
There was a problem hiding this comment.
Somewhere there should be a notion of what a resolution is.
| /** | ||
| * Tells whether the path should be resolved for the currently processed module. | ||
| */ | ||
| abstract boolean skipForModule(ImportPath importPath); |
There was a problem hiding this comment.
Is it an intention to skip or checking if should be skipped?
Also, how "skip for a module" plays with "import path"? I am confused.
| import java.util.List; | ||
|
|
||
| /** | ||
| * Resolves relative imports of generated Protobuf files among the {@link #modules}. |
There was a problem hiding this comment.
Let's not link anything which has no explanation anywhere.
| import static io.spine.code.js.ImportPath.parentDirectory; | ||
|
|
||
| /** | ||
| * Replaces library-like Spine imports by relative paths if the imported file |
There was a problem hiding this comment.
Please rephrase "library-like". Did you mean the imports of Spine library files? If so, just write a longer sentence, but make it more comprehensible.
| * | ||
| * <p>Information about modules is used to resolve imports in generated Protobuf files. | ||
| * | ||
| * <p>Predefined Spine Modules are {@linkplain #resolveSpineModules included} by default. |
There was a problem hiding this comment.
The link should be not here, but in a place, which describes how to override the default behavior.
|
@armiol PTAL. |
Currently, Protobuf compiler generates JavaScript files with relative imports, e.g:
It means that all the files used as dependencies should be generated for every module and cannot be reused. The PR solves the issue and allows to replace relative imports by global, library-like imports.
So, the import above can be changed to:
The adjustment of imports is done by Proto JS plugin by the
ResolveImportstask.Imports of standard Protobuf types
The task also adjusts imports of standard Protobuf types like
require('google-protobuf/google/protobuf/any_pb.js'). These imports are made non-relative by Protobuf compiler. But we want to use standard types, which are additionally processed by the Proto JS plugin. So, the imports are adjusted to referspine-webmodule insteadgoogle-protobuf.The set of imports that will be adjusted depends on the configuration of the Proto JS plugin. By default, the plugin will adjust only imports of files provided by Spine via NPM.
Custom configuration of imports resolving
You can also specify the custom modules and directories they provide. So, the plugin will replace relative imports by imports of module files.
An example:
For a more comprehensive description of the feature configuration, see the wiki page.
Other changes
The PR also solves #306 by introducing a new abstract base for plugins —
ProtoPlugin. This class providesFileSetincluding the types from the moduleknown_types.descand from the project runtime dependencies and ensures that these types are present inKnownTypes.Under the hood,
ProtoPluginusesFileDescriptorSuperset. So, this and related classes were moved frommodel-compilermodule toplugin-base.