Skip to content

Commit

Permalink
fix(mkdist) group output chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 10, 2022
1 parent 90cd3a6 commit 24b0072
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 17 deletions.
13 changes: 6 additions & 7 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,25 +195,24 @@ export async function build (rootDir: string, stub: boolean, inputConfig: BuildC
}
}

const entries = [
...ctx.buildEntries.filter(e => !e.chunk),
...ctx.buildEntries.filter(e => e.chunk)
]
let distSize = 0
const rPath = (p: string) => relative(process.cwd(), resolve(options.outDir, p))
for (const entry of entries) {
for (const entry of ctx.buildEntries.filter(e => !e.chunk)) {
distSize += entry.bytes || 0
let totalBytes = entry.bytes || 0
for (const chunk of entry.chunks || []) {
totalBytes += entries.find(e => e.path === chunk)?.bytes || 0
totalBytes += ctx.buildEntries.find(e => e.path === chunk)?.bytes || 0
}
let line = ` ${chalk.bold(rPath(entry.path))} (` + [
entry.bytes && `size: ${chalk.cyan(prettyBytes(entry.bytes))}`,
totalBytes !== entry.bytes && `total size: ${chalk.cyan(prettyBytes(totalBytes))}`,
entry.exports?.length && `exports: ${chalk.gray(entry.exports.join(', '))}`
].filter(Boolean).join(', ') + ')'
if (entry.chunks?.length) {
line += '\n' + entry.chunks.map(p => chalk.gray(' └─ ' + rPath(p))).join('\n')
line += '\n' + entry.chunks.map((p) => {
const chunk = ctx.buildEntries.find(e => e.path === p) || {} as any
return chalk.gray(' └─ ' + rPath(p) + (chunk.bytes ? ` (${prettyBytes(chunk?.bytes)})` : ''))
}).join('\n')
}
consola.log(entry.chunk ? chalk.gray(line) : line)
}
Expand Down
3 changes: 2 additions & 1 deletion src/builder/mkdist.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { relative } from 'pathe'
import { mkdist, MkdistOptions } from 'mkdist'
import { symlink, rmdir } from '../utils'
import type { MkdistBuildEntry, BuildContext } from '../types'
Expand Down Expand Up @@ -25,7 +26,7 @@ export async function mkdistBuild (ctx: BuildContext) {
const output = await mkdist(mkdistOptions)
ctx.buildEntries.push({
path: distDir,
chunks: [`${output.writtenFiles.length} files`]
chunks: output.writtenFiles.map(p => relative(ctx.options.outDir, p))
})
await ctx.hooks.callHook('mkdist:entry:build', ctx, entry, output)
}
Expand Down
15 changes: 8 additions & 7 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@ export async function rollupBuild (ctx: BuildContext) {
for (const id of entry.imports) {
ctx.usedImports.add(id)
}
ctx.buildEntries.push({
chunk: !entry.isEntry,
chunks: entry.imports.filter(i => outputChunks.find(c => c.fileName === i)),
path: entry.fileName,
bytes: Buffer.byteLength(entry.code, 'utf8'),
exports: entry.isEntry ? entry.exports : []
})
if (entry.isEntry) {
ctx.buildEntries.push({
chunks: entry.imports.filter(i => outputChunks.find(c => c.fileName === i)),
path: entry.fileName,
bytes: Buffer.byteLength(entry.code, 'utf8'),
exports: entry.exports
})
}
}
for (const chunkFileName of chunkFileNames) {
ctx.usedImports.delete(chunkFileName)
Expand Down
1 change: 1 addition & 0 deletions test/fixture/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default defineBuildConfig({
},
entries: [
'src/index',
{ input: 'src/runtime/', outDir: 'dist/runtime' },
{ input: 'src/schema', builder: 'untyped' }
]
})
2 changes: 1 addition & 1 deletion test/fixture/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ console.log(require.resolve('rollup'))
import('os').then(os => console.log(os.arch()))

// @ts-ignore
import('./test.html').then(console.log)
import('./runtime/foo.ts').then(console.log)

export const foo = 'bar'
export const baz = '123'
Expand Down
1 change: 1 addition & 0 deletions test/fixture/src/runtime/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default '123'
1 change: 0 additions & 1 deletion test/fixture/src/test.html

This file was deleted.

0 comments on commit 24b0072

Please sign in to comment.