@@ -28,17 +28,31 @@ const terserOptions = {
28
28
format : { comments : false } ,
29
29
} ;
30
30
31
+ async function resolvePeer ( module ) {
32
+ if ( ! module ) return ;
33
+
34
+ try {
35
+ const pkg = await fs . readJson (
36
+ fileURLToPath ( new URL ( import . meta. resolve ( `${ module } /package.json` ) ) ) ,
37
+ 'utf8' ,
38
+ ) ;
39
+ return pkg . peerDependencies ;
40
+ } catch {
41
+ const arr = module . split ( '/' ) ;
42
+ arr . pop ( ) ;
43
+ return resolvePeer ( arr . join ( '/' ) ) ;
44
+ }
45
+ }
46
+
31
47
const bundledModules = new Set ( ) ;
32
48
async function bundleModule ( module ) {
33
49
if ( bundledModules . has ( module ) ) return ;
34
50
bundledModules . add ( module ) ;
35
51
36
- const { peerDependencies } = await fs . readJson ( fileURLToPath (
37
- new URL ( import . meta. resolve ( `${ module } /package.json` ) ) ,
38
- ) , 'utf8' ) ;
52
+ const peer = await resolvePeer ( module ) ;
39
53
const bundle = await rollup ( {
40
54
input : module ,
41
- external : peerDependencies ? Object . keys ( peerDependencies ) : undefined ,
55
+ external : peer ? Object . keys ( peer ) : undefined ,
42
56
plugins : [
43
57
commonjs ( ) ,
44
58
replace ( {
@@ -58,14 +72,14 @@ async function bundleModule(module) {
58
72
} ) ;
59
73
}
60
74
61
- function traverseAST ( ast , onlyBabel = false ) {
75
+ function traverseAST ( ast , babelOnly = false ) {
62
76
traverse . default ( ast , {
63
77
CallExpression ( { node } ) {
64
78
if (
65
79
node . callee . name !== 'require' ||
66
80
! t . isStringLiteral ( node . arguments [ 0 ] ) ||
67
81
node . arguments [ 0 ] . value . startsWith ( '.' ) ||
68
- ( onlyBabel && ! node . arguments [ 0 ] . value . startsWith ( '@babel/runtime' ) )
82
+ ( babelOnly && ! node . arguments [ 0 ] . value . startsWith ( '@babel/runtime' ) )
69
83
) {
70
84
return ;
71
85
}
0 commit comments