Skip to content

Commit

Permalink
feat: print sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed May 13, 2020
1 parent cb445fd commit 18e618e
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"fs-extra": "^9.0.0",
"jest": "^26.0.1",
"prettier": "^2.0.5",
"pretty-bytes": "^5.3.0",
"resolve": "^1.17.0",
"rollup-plugin-dts": "^1.4.2",
"rollup-plugin-hashbang": "^2.2.2",
Expand Down
5 changes: 3 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const cli = cac('tsup')

cli
.command('<...files>', 'Bundle files')
.option('-d, --out-dir', 'Output directory', { default: 'dist' })
.option('-d, --out-dir <dir>', 'Output directory', { default: 'dist' })
.option('--format <format>', 'Bundle format, "cjs", "iife", "umd", "esm"', {
default: 'cjs',
})
Expand All @@ -29,7 +29,7 @@ cli
})
.action(async (files: string[], options) => {
const { rollup, watch } = await import('rollup')
const { createRollupConfigs } = await import('./')
const { createRollupConfigs, printSizes } = await import('./')
const rollupConfigs = await createRollupConfigs(files, {
watch: options.watch,
minify: options.minify,
Expand Down Expand Up @@ -63,6 +63,7 @@ cli
await result.write(config.outputConfig)
})
)
printSizes()
const endTime = Date.now()
console.log(`Done in ${endTime - startTime}ms`)
} catch (error) {
Expand Down
27 changes: 25 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { ModuleFormat, InputOptions, OutputOptions } from 'rollup'
import { Target as EsbuildTarget } from 'esbuild'
import prettyBytes from 'pretty-bytes'
import colors from 'colorette'
import hashbangPlugin from 'rollup-plugin-hashbang'
import esbuildPlugin from 'rollup-plugin-esbuild'
import commonjsPlugin from '@rollup/plugin-commonjs'
import jsonPlugin from '@rollup/plugin-json'
import dtsPlugin from 'rollup-plugin-dts'
import { sizePlugin, caches } from './size-plugin'
import { resolvePlugin } from './resolve-plugin'

type Options = {
Expand Down Expand Up @@ -50,8 +53,8 @@ export async function createRollupConfigs(files: string[], options: Options) {
commonjsPlugin({
namedExports: {
// commonjs plugin failed to detect named exports for `resolve`, TODO: report this bug
resolve: Object.keys(require('resolve'))
}
resolve: Object.keys(require('resolve')),
},
}),
dts && dtsPlugin(),
!dts &&
Expand All @@ -63,6 +66,7 @@ export async function createRollupConfigs(files: string[], options: Options) {
jsxFragment: options.jsxFragment,
define: options.define,
}),
sizePlugin(),
].filter(Boolean),
},
outputConfig: {
Expand All @@ -79,3 +83,22 @@ export async function createRollupConfigs(files: string[], options: Options) {

return rollupConfigs
}

export function printSizes() {
const result: Map<string, number> = new Map()
for (const cache of caches.values()) {
for (const [filename, getSize] of cache.entries()) {
result.set(filename, getSize())
}
}
const maxNameLength = [...result.keys()].sort((a, b) =>
a.length > b.length ? -1 : 1
)[0].length
for (const [filename, size] of result.entries()) {
console.log(
`${colors.bold(filename.padEnd(maxNameLength))} - ${colors.green(
prettyBytes(size)
)}`
)
}
}
1 change: 1 addition & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {spawn} from 'child_process'
export function runCode(filename: string, {
args
}: {args: string[]}) {
console.log(filename, args)
const cmd = spawn('node', [filename, ...args], {
stdio: 'inherit'
})
Expand Down
29 changes: 29 additions & 0 deletions src/size-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Plugin } from 'rollup'

type Cache = Map<string, () => number>

export const caches: Map<number, Cache> = new Map()

export const sizePlugin = (): Plugin => {
const key = Math.random()
let cache: Cache
return {
name: 'size',

buildStart() {
cache = new Map()
caches.set(key, cache)
},

generateBundle(options, bundle, isWrite) {
if (isWrite) {
for (const key of Object.keys(bundle)) {
const file = bundle[key]
if (file.type === 'chunk') {
cache.set(file.fileName, () => file.code.length)
}
}
}
},
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"target": "es2018", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2889,6 +2889,11 @@ prettier@^2.0.5:
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.0.5.tgz#d6d56282455243f2f92cc1716692c08aa31522d4"
integrity sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==

pretty-bytes@^5.3.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-5.3.0.tgz#f2849e27db79fb4d6cfe24764fc4134f165989f2"
integrity sha512-hjGrh+P926p4R4WbaB6OckyRtO0F0/lQBiT+0gnxjV+5kjPBrfVBFCsCLbMqVQeydvIoouYTCmmEURiH3R1Bdg==

pretty-format@^25.2.1, pretty-format@^25.5.0:
version "25.5.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-25.5.0.tgz#7873c1d774f682c34b8d48b6743a2bf2ac55791a"
Expand Down

0 comments on commit 18e618e

Please sign in to comment.