We are in the process of updating our code base to replace RequireJS with ES6 modules. There are several instances where we have components that load their own CSS and use the require.toUrl to get an absolute path. The components only know their local directory where its CSS lives, and could be in different directory structures depending on how they are deployed.
For example:
if(!document.getElementById('my-component-css')) {
let cssRef = document.createElement('link');
cssRef.rel = 'stylesheet';
cssRef.type = 'text/css';
cssRef.id = 'my-component-css';
cssRef.href = require.toUrl('./css/my-component.min.css');
document.head.appendChild(cssRef);
}
or we also use local imagery with CesiumJS that requires something similar:
var natEarth = new Cesium.TileMapServiceImageryProvider({
url: require.toUrl('./assets/imagery/NaturalEarthII/'),
fileExtension: 'jpg',
credit: 'Imagery courtesy of NASA World Wind (Blue Marble)'
});
Looking at requireJS's code, they keep their own path structure internally and walk up the module tree to get a full absolute path for a component.
Short of passing in a path to these components (or using webpack since we cannot use it due to internal company policies), is there an equivalent in Vanilla JS to require.toUrl?