@@ -25,10 +25,20 @@ function buildDependencyTree(prefix) {
2525
2626// walk tree and build remappings
2727function buildRemappings ( pkg ) {
28- const remappings = pkg . deps . map ( dep => {
29- return `${ pkg . path } /:${ dep . name } /=${ dep . path } /`
30- } )
31- return pkg . deps . map ( buildRemappings ) . concat ( remappings ) . flat ( )
28+ const paths = mapHashes ( pkg )
29+
30+ let implicits = [ ]
31+ if ( process . env . DAPP_ALLOW_IMPLICIT_IMPORTS == "yes" ) {
32+ const directs = pkg . deps . map ( p => p . name )
33+ // get the transitive deps with exactly one version that are not direct deps
34+ const uniques = Object . entries ( pkg . deps . reduce ( buildVersionMap , { } ) )
35+ . filter ( ( [ name , vs ] ) => vs . size == 1 && ! directs . includes ( name ) )
36+ // build remappings that allow importing these deps from `pkg`
37+ implicits = uniques . map ( ( [ name , v ] ) => `${ pkg . path } /:${ name } /=${ paths [ ( [ ...v ] [ 0 ] ) ] } /` )
38+ }
39+
40+ const remappings = pkg . deps . map ( dep => `${ pkg . path } /:${ dep . name } /=${ dep . path } /` )
41+ return pkg . deps . map ( buildRemappings ) . concat ( remappings ) . concat ( implicits ) . flat ( )
3242}
3343
3444// walk tree and rewrite paths so that all packages with the same hash have the same path
@@ -47,6 +57,21 @@ function mapHashes(pkg) {
4757 return pkg . deps . reduce ( go , { [ pkg . hash ] : pkg . path } )
4858}
4959
60+ // folds over a dependency tree with map as the accumlator and builds a mapping
61+ // from package name to a set of discovered package verions
62+ function buildVersionMap ( map , pkg ) {
63+ const update = ( map , dep ) => {
64+ map [ dep . name ] == undefined
65+ ? map [ dep . name ] = new Set ( [ dep . hash ] )
66+ : map [ dep . name ] . add ( dep . hash )
67+ return map
68+ }
69+
70+ return pkg . deps . reduce ( ( versions , dep ) => {
71+ return update ( versions , dep )
72+ } , update ( map , pkg ) )
73+ }
74+
5075// strip the leading `.` or `./` from a path
5176function normalize ( path ) {
5277 return path . replace ( / ^ \. \/ / , "" ) . replace ( / ^ \/ / , "" )
@@ -56,9 +81,9 @@ function normalize(path) {
5681// availalbe (because it's faster), or falls back to a sha256sum of the directory contents if needed
5782function hash ( dir ) {
5883 if ( ls ( dir ) . includes ( ".git" ) ) {
59- return run ( "git" , [ "-C" , dir , "rev-parse" , "HEAD" ] )
84+ return run ( "git" , [ "-C" , dir , "rev-parse" , "HEAD" ] ) . trim ( )
6085 } else {
61- return run ( "bash" , [ "-c" , `rg --files ${ dir } | sort | xargs sha256sum | sha256sum` ] )
86+ return run ( "bash" , [ "-c" , `rg --files ${ dir } | sort | xargs sha256sum | sha256sum | cut -d' ' -f1` ] ) . trim ( )
6287 }
6388}
6489
0 commit comments