1
1
import { builtinModules } from "node:module" ;
2
2
import { dirname , relative , join , basename , extname , resolve } from "node:path" ;
3
3
import { consola } from "consola" ;
4
+ import { colors as c } from "consola/utils" ;
4
5
import { rolldown } from "rolldown" ;
5
6
import { dts } from "rolldown-plugin-dts" ;
6
7
import { fmtPath } from "../utils.ts" ;
7
8
import { resolveModulePath } from "exsolve" ;
8
9
9
- import type { Plugin } from "rolldown" ;
10
+ import type { OutputChunk , Plugin } from "rolldown" ;
10
11
import type { BuildContext ,
8000
BuildHooks , BundleEntry } from "../types.ts" ;
11
12
import type { InputOptions , OutputOptions } from "rolldown" ;
12
13
@@ -15,8 +16,6 @@ export async function rolldownBuild(
15
16
entry : BundleEntry ,
16
17
hooks : BuildHooks ,
17
18
) : Promise < void > {
18
- const start = Date . now ( ) ;
19
-
20
19
const inputs : Record < string , string > = { } ;
21
20
22
21
for ( let src of Array . isArray ( entry . input ) ? entry . input : [ entry . input ] ) {
@@ -70,8 +69,10 @@ export async function rolldownBuild(
70
69
71
70
const res = await rolldown ( rolldownConfig ) ;
72
71
72
+ const outDir = resolve ( ctx . pkgDir , entry . outDir || "dist" ) ;
73
+
73
74
const outConfig : OutputOptions = {
74
- dir : entry . outDir ,
75
+ dir : outDir ,
75
76
entryFileNames : "[name].mjs" ,
76
77
chunkFileNames : "_chunks/[name]-[hash].mjs" ,
77
78
minify : entry . minify ,
@@ -83,15 +84,67 @@ export async function rolldownBuild(
83
84
84
85
await res . close ( ) ;
85
86
87
+ const outputEntries : {
88
+ name : string ;
89
+ exports : string [ ] ;
90
+ deps : string [ ] ;
91
+ size : number ;
92
+ } [ ] = [ ] ;
93
+
94
+ const depsCache = new Map < OutputChunk , Set < string > > ( ) ;
95
+ const resolveDeps = ( chunk : OutputChunk ) => {
96
+ if ( ! depsCache . has ( chunk ) ) {
97
+ depsCache . set ( chunk , new Set < string > ( ) ) ;
98
+ }
99
+ const deps = depsCache . get ( chunk ) ! ;
100
+ for ( const id of chunk . imports ) {
101
+ if ( builtinModules . includes ( id ) || id . startsWith ( "node:" ) ) {
102
+ deps . add ( `[Node.js]` ) ;
103
+ continue ;
104
+ }
105
+ const depChunk = output . find (
106
+ ( o ) => o . type === "chunk" && o . fileName === id ,
107
+ ) as OutputChunk | undefined ;
108
+ if ( depChunk ) {
109
+ for ( const dep of resolveDeps ( depChunk ) ) {
110
+ deps . add ( dep ) ;
111
+ }
112
+ continue ;
113
+ }
114
+ deps . add ( id ) ;
115
+ }
116
+ return [ ...deps ] . sort ( ) ;
117
+ } ;
118
+
119
+ for ( const chunk of output ) {
120
+ if ( chunk . type !== "chunk" || ! chunk . isEntry ) continue ;
121
+ if ( chunk . fileName . endsWith ( "ts" ) ) continue ;
122
+ outputEntries . push ( {
123
+ name : chunk . fileName ,
124
+ exports : chunk . exports ,
125
+ deps : resolveDeps ( chunk ) ,
126
+ size : chunk . code . length ,
127
+ } ) ;
128
+ }
129
+
86
130
consola . log (
87
- `📦 Bundled in ${ Date . now ( ) - start } ms:\n${ output
88
- . filter (
89
- ( o ) => o . type === "chunk" && o . isEntry && o . fileName . endsWith ( "js" ) ,
90
- )
91
- . map (
92
- ( o ) =>
93
- ` - ${ fmtPath ( resolve ( ctx . pkgDir , entry . outDir || "dist" , o . fileName ) ) } ` ,
131
+ `\n${ outputEntries
132
+ . map ( ( o ) =>
133
+ [
134
+ c . magenta ( `[bundle] ` ) +
135
+ `${ c . underline ( fmtPath ( join ( outDir , o . name ) ) ) } ` ,
136
+ o . exports . length > 0
137
+ ? c . dim (
138
+ `${ c . bold ( "Exports:" ) } ${ o . exports . map ( ( e ) => e ) . join ( ", " ) } ` ,
139
+ )
140
+ : "" ,
141
+ o . deps . length > 0
142
+ ? c . dim ( `${ c . bold ( "Dependencies:" ) } ${ o . deps . join ( ", " ) } ` )
143
+ : "" ,
144
+ ]
145
+ . filter ( Boolean )
146
+ . join ( "\n" ) ,
94
147
)
95
- . join ( "\n" ) } `,
148
+ . join ( "\n\n " ) } `,
96
149
) ;
97
150
}
0 commit comments