@@ -25,10 +25,20 @@ function buildDependencyTree(prefix) {
25
25
26
26
// walk tree and build remappings
27
27
function 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 ( )
32
42
}
33
43
34
44
// walk tree and rewrite paths so that all packages with the same hash have the same path
@@ -47,6 +57,21 @@ function mapHashes(pkg) {
47
57
return pkg . deps . reduce ( go , { [ pkg . hash ] : pkg . path } )
48
58
}
49
59
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
+
50
75
// strip the leading `.` or `./` from a path
51
76
function normalize ( path ) {
52
77
return path . replace ( / ^ \. \/ / , "" ) . replace ( / ^ \/ / , "" )
@@ -56,9 +81,9 @@ function normalize(path) {
56
81
// availalbe (because it's faster), or falls back to a sha256sum of the directory contents if needed
57
82
function hash ( dir ) {
58
83
if ( ls ( dir ) . includes ( ".git" ) ) {
59
- return run ( "git" , [ "-C" , dir , "rev-parse" , "HEAD" ] )
84
+ return run ( "git" , [ "-C" , dir , "rev-parse" , "HEAD" ] ) . trim ( )
60
85
} 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 ( )
62
87
}
63
88
}
64
89
0 commit comments