-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 🎸 report size
- Loading branch information
Showing
18 changed files
with
485 additions
and
390 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,39 @@ | ||
|
||
import fs from 'fs-extra'; | ||
import { performance } from 'perf_hooks'; | ||
import { reportSize } from './helpers/reportSize.js'; | ||
import runTransform from './loaders/transform.js'; | ||
import runBundle from './loaders/bundle.js'; | ||
import { createLogger } from './helpers/logger.js'; | ||
import { timeFrom } from './utils.js'; | ||
|
||
import type { PkgContext, TaskConfig } from './types.js'; | ||
import type { PkgContext, TaskLoaderConfig, OutputFile } from './types.js'; | ||
|
||
export const buildAll = async (cfgArrs: TaskConfig[], ctx: PkgContext) => { | ||
export const buildAll = async (cfgArrs: TaskLoaderConfig[], ctx: PkgContext) => { | ||
for (let c = 0; c < cfgArrs.length; ++c) { | ||
const { type } = cfgArrs[c]; | ||
|
||
let outputFiles: OutputFile[] = []; | ||
if (type === 'bundle') { | ||
await runBundle(cfgArrs[c]); | ||
outputFiles = await runBundle(cfgArrs[c]); | ||
} | ||
|
||
if (type === 'transform') { | ||
await runTransform(cfgArrs[c], ctx); | ||
outputFiles = await runTransform(cfgArrs[c], ctx); | ||
} | ||
|
||
const reportSizeStart = performance.now(); | ||
if (ctx.command === 'build') { | ||
reportSize(outputFiles.reduce((pre, chunk) => { | ||
return { | ||
...pre, | ||
[chunk.filename]: chunk.code ? chunk.code : fs.readFileSync(chunk.dest), | ||
}; | ||
}, ({} as any))); | ||
} | ||
|
||
const logger = createLogger('report-size'); | ||
logger.debug(`ReportSize consume ${timeFrom(reportSizeStart)}`); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import consola from 'consola'; | ||
import picocolors from 'picocolors'; | ||
import { gzipSizeSync } from 'gzip-size'; | ||
|
||
const UNIT = ['B', 'KB', 'MB', 'GB', 'TB']; | ||
|
||
const prettifySize = (bytes: number): string => { | ||
if (bytes === 0) return '0 B'; | ||
|
||
const exp = Math.floor(Math.log2(bytes) / 10); | ||
return `${(bytes / Math.pow(1024, exp)).toFixed(2)} ${UNIT[exp]}`; | ||
}; | ||
|
||
const findMaxLength = (names: string[]) => { | ||
return names.map((name) => name.length).sort((a, b) => b - a)[0]; | ||
}; | ||
|
||
export const reportSize = ( | ||
files: { | ||
[name: string]: string; | ||
}, | ||
) => { | ||
const names = Object.keys(files); | ||
const maxLen = findMaxLength(names); | ||
const padLength = maxLen > 30 ? (maxLen + 2) : 30; | ||
|
||
names.forEach((name) => { | ||
const rawSize = prettifySize(files[name].length); | ||
const gzipSize = prettifySize(gzipSizeSync(files[name])); | ||
consola.info(`${name.padStart(padLength, ' ')}: ${picocolors.green('raw')} ${rawSize} ${picocolors.cyan('gzip')} ${gzipSize}`); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,38 @@ | ||
import * as rollup from 'rollup'; | ||
import { performance } from 'perf_hooks'; | ||
import { toArray } from '../utils.js'; | ||
import { toArray, timeFrom } from '../utils.js'; | ||
import { createLogger } from '../helpers/logger.js'; | ||
|
||
import type { TaskConfig } from '../types.js'; | ||
import type { TaskLoaderConfig, OutputFile } from '../types.js'; | ||
|
||
export default async (cfg: TaskConfig): Promise<boolean> => { | ||
const logger = createLogger('bundle'); | ||
export default async (cfg: TaskLoaderConfig): Promise<OutputFile[]> => { | ||
const logger = createLogger(cfg.name); | ||
|
||
const bundleStart = performance.now(); | ||
|
||
const rollupOption = cfg.rollupOptions; | ||
|
||
logger.info('Build start...'); | ||
logger.debug('Build start...'); | ||
const bundle = await rollup.rollup(rollupOption); | ||
|
||
const outputs = toArray(rollupOption.output); | ||
const outputChunks: OutputFile[] = []; | ||
|
||
for (let o = 0; o < outputs.length; ++o) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await bundle.write(outputs[o]); | ||
const writeResult = await bundle.write(outputs[o]); | ||
|
||
writeResult.output.forEach((chunk) => { | ||
outputChunks.push({ | ||
filename: chunk.fileName, | ||
code: chunk.type === 'chunk' ? chunk.code : chunk.source, | ||
}); | ||
}); | ||
} | ||
|
||
await bundle.close(); | ||
|
||
logger.info(`⚡️ Build success in ${(performance.now() - bundleStart).toFixed(2)}ms`); | ||
logger.info(`⚡️ Build success in ${timeFrom(bundleStart)}`); | ||
|
||
return true; | ||
return outputChunks; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.