When using esm there seems to be no way to resolve paths to a packages 🤔
In a commonjs project, I currently have this "gem" 😅
/**
* Resolves a file/folder path that can contain bare modules.
*
* @example
* resolvedNodePackagePath('@foo/bar/some-folder');
* => /absolute/path/node_modules/@foo/bar/some-folder
*
* resolvedNodePackagePath('@foo/bar');
* => /absolute/path/node_modules/@foo/bar/
*
* @param {string } resolvePath Path to resolve
* @return {string } Resolved path
*/
function resolvedNodePackagePath ( resolvePath ) {
const hasNamespace = resolvePath . includes ( '@' ) ;
const parts = resolvePath . split ( path . sep ) ;
const pkgName = hasNamespace ? path . join ( parts [ 0 ] , parts [ 1 ] ) : parts [ 0 ] ;
parts . shift ( ) ;
if ( hasNamespace ) {
parts . shift ( ) ;
}
const purePath = path . join ( ...parts ) ;
const pkgJson = require . resolve ( path . join ( pkgName , 'package.json' ) ) ;
const pkgRoot = path . dirname ( pkgJson ) ;
return path . join ( pkgRoot , purePath ) ;
}
with --experimental-import-meta-resolve I can basically replace resolvedNodePackagePath with await import.meta.resolve(...).
However, as long as it's an experimental flag it is kind of a tough sell to users and additionally, it requires some trickery for bins 😅
So hence my question any chance this is going to be unflagged any time soon?
👍 React with 👍 8abdonrd, SalvatorePreviti, lightyen, thescientist13, borkdude and 3 more