Components used in Themes and Plugins: How to get file URL?

Is there a way to determine if my current PHP file is within the plugins’ folder or within the themes’ folder

Broadly you always have path to current file (__FILE__) so you can always examine it for where you are and compare with context. This might be good enough in custom site, but likely too brittle for public code. There are plenty of edge cases with paths since they can be highly customized.

What you have is not just asset problem, but dependency problem — how do you reuse multiple components, in different ways, and without conflicts.

Personally I’d strongly consider looking into Composer, since it’s de–facto way to manage dependencies in modern PHP. It’s… not perfect for quirks of publicly distributed WP extensions, but your case sounds more like custom–work focused anyway.