How to resolve relative path to absolute url in ES6 module (in browser)?
For example :
base-url.com/
|-resolve
| |-resolveUrl.js
|-dir
|-dir1
|-module1.js
|-dir2
|-module2.js
module1.js
import resolveUrl from "../../../resolve/resolveUrl.js"
resolveUrl("../dir2/module2.js") // return 'base-url.com/dir/dir2/module2.js'
What would be the resolveUrl implementation?
base-url.com/ may not be the web-site’s domain. It can be a repo where only sources are stored.
Advertisement
Answer
🎉 You can now use to-absolute-url.
It uses stacktrace.js under the hoods to get the caller function’s absolute path file:
Thanks to @PatrickRoberts for his idea to use stacktrace, and @jfriend00 for his tip to use URL object.