From 18e618ebb402b69f25f0d99fb90069c97915cfff Mon Sep 17 00:00:00 2001
From: EGOIST <0x142857@gmail.com>
Date: Thu, 14 May 2020 02:58:30 +0800
Subject: [PATCH] feat: print sizes
---
package.json | 1 +
src/cli.ts | 5 +++--
src/index.ts | 27 +++++++++++++++++++++++++--
src/run.ts | 1 +
src/size-plugin.ts | 29 +++++++++++++++++++++++++++++
tsconfig.json | 2 +-
yarn.lock | 5 +++++
7 files changed, 65 insertions(+), 5 deletions(-)
create mode 100644 src/size-plugin.ts
diff --git a/package.json b/package.json
index ecf69a18d..92c28cad1 100644
--- a/package.json
+++ b/package.json
@@ -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",
diff --git a/src/cli.ts b/src/cli.ts
index 2c50ba8e4..8b17ac006 100644
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -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
', 'Output directory', { default: 'dist' })
.option('--format ', 'Bundle format, "cjs", "iife", "umd", "esm"', {
default: 'cjs',
})
@@ -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,
@@ -63,6 +63,7 @@ cli
await result.write(config.outputConfig)
})
)
+ printSizes()
const endTime = Date.now()
console.log(`Done in ${endTime - startTime}ms`)
} catch (error) {
diff --git a/src/index.ts b/src/index.ts
index 55ffef05d..d080f70be 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -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 = {
@@ -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 &&
@@ -63,6 +66,7 @@ export async function createRollupConfigs(files: string[], options: Options) {
jsxFragment: options.jsxFragment,
define: options.define,
}),
+ sizePlugin(),
].filter(Boolean),
},
outputConfig: {
@@ -79,3 +83,22 @@ export async function createRollupConfigs(files: string[], options: Options) {
return rollupConfigs
}
+
+export function printSizes() {
+ const result: Map = 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)
+ )}`
+ )
+ }
+}
diff --git a/src/run.ts b/src/run.ts
index 528fc146f..129b77323 100644
--- a/src/run.ts
+++ b/src/run.ts
@@ -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'
})
diff --git a/src/size-plugin.ts b/src/size-plugin.ts
new file mode 100644
index 000000000..b52da2c60
--- /dev/null
+++ b/src/size-plugin.ts
@@ -0,0 +1,29 @@
+import { Plugin } from 'rollup'
+
+type Cache = Map number>
+
+export const caches: Map = 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)
+ }
+ }
+ }
+ },
+ }
+}
diff --git a/tsconfig.json b/tsconfig.json
index 54d53fdf7..b5af5e755 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -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. */
diff --git a/yarn.lock b/yarn.lock
index 663c6428b..1ada2e885 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -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"