Generalize implementation of ImportsResolver to allow imports from arbitrary npm packages #78
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why is it needed?
This came out of a discussion with @rbair23. We would like to use this plugin to compile contracts like this one:
This uses sol files from
@layerzerolabs/oft-evm
which in turn uses files from other npm packages (transitive dependencies/imports). This could be solved by somehow allowing to add more import providers as suggested in #77. But then you would need to configure all packages manually that are already declared in the SOL files.What does this PR do?
The solution proposed in this PR automatically reads all required npm packages, including transitive ones. This is done by changing the setup that is handled by the
npmInstall
andresolveSolidity
tasks as follows:extractSolidityImports
that extracts all packages required by imports from the SOL files in the project and generates thepackage.json
based on this information. Thepackage.json
is the output of this task and it only changes if modules required for the imports change. Which is not often the case. Thus, the next two tasks run seldomly and are UP-TO-DATE most of the time.npmInstall
downloads the packages and their transitive dependencies.resolveSolidity
searches through the SOL files downloaded by npm for additional packages that are used to get a complete list for the allowed paths and the path remappings incompileSolidity
.Where should the reviewer start?
This only a proposal at this point. Maybe there are reasons not to do this. I am happy to work on another solution that would allow us to compile contracts as the one shown above.
I haven't changed the documentation in the Readme on this topic yet, but if this is a solution that would be accepted, I will update it as well.
Checklist