There may be some history I'm missing here, but I think the current approach to set codebase path based on location of scripts.js stems from before lib-franklin/aem.js was separated from scripts.js.
import.meta.url is well supported (pretty much anywhere dynamic import() is supported), and think this may be slightly simpler:
[window.hlx.codeBasePath] = new URL(import.meta.url).pathname.split('/scripts/');