Skip to content

Commit

Permalink
INCOMPLETE:
Browse files Browse the repository at this point in the history
1. Fix for __dirname? rollup/rollup#3445
2. Resume using `filesize` again in Rollup config; tests/add coverage?

feat: replace `render` with `reporter` array

BREAKING CHANGE:

Removes `render`.

Also adds sourcemaps and bumps some devDeps and drops unneeded ones
  • Loading branch information
brettz9 committed Apr 15, 2020
1 parent d79539c commit c5220eb
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 165 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -4,6 +4,7 @@ module.exports = {
es6: true,
node: true,
},
parser: "babel-eslint",
extends: ["eslint:recommended", "plugin:prettier/recommended"],
parserOptions: {
ecmaVersion: 2017,
Expand Down
19 changes: 3 additions & 16 deletions Readme.md
Expand Up @@ -60,21 +60,8 @@ default : {}

See the options [here](https://github.com/avoidwork/filesize.js)

#### render
type : `function`

return the command that you want to log. Eg:

```js
filesize({
render : function (options, bundle, { minSize, gzipSize, brotliSize, bundleSize }){
return minSize;
}
})
```

#### reporter
type : `function`
type : Array of `function`'s

After rendering occurs, you may wish to pass on the collected file data,
e.g., to build a badge for filesizes (as does [filesize-badger](https://github.com/brettz9/filesize-badger)).
Expand All @@ -83,10 +70,10 @@ You can use `reporter` to do so:

```js
filesize({
reporter : function (options, bundle, { minSize, gzipSize, brotliSize, bundleSize }){
reporter : [function (options, bundle, { minSize, gzipSize, brotliSize, bundleSize }){
// If a promise is returned, it will be awaited before rendering.
return promise;
}
}]
})
```

Expand Down
9 changes: 4 additions & 5 deletions package.json
Expand Up @@ -34,27 +34,26 @@
},
"homepage": "https://github.com/ritz078/rollup-plugin-filesize#readme",
"dependencies": {
"babel-eslint": "^10.1.0",
"boxen": "^4.2.0",
"brotli-size": "4.0.0",
"colors": "^1.4.0",
"filesize": "^6.1.0",
"gzip-size": "^5.1.1",
"lodash.merge": "^4.6.2",
"terser": "^4.6.11"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.5",
"@babel/register": "^7.9.0",
"ava": "^3.6.0",
"babel-plugin-transform-async-to-promises": "^0.8.15",
"ava": "^3.7.0",
"babel-register": "^6.26.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.1",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-prettier": "^3.1.3",
"prettier": "^2.0.4",
"regenerator-runtime": "^0.13.5",
"rollup": "^2.6.0",
"rollup": "^2.6.1",
"rollup-plugin-babel": "^4.4.0"
},
"ava": {
Expand Down
54 changes: 38 additions & 16 deletions rollup.config.js
@@ -1,20 +1,42 @@
import babel from "rollup-plugin-babel";
import filesize from "./src/index";
// import filesize from "./src/index.js";
import pkg from "./package.json";

export default {
external: ["fs", "util", ...Object.keys(pkg.dependencies)],
plugins: [
babel({
babelrc: false,
plugins: ["transform-async-to-promises"],
presets: ["@babel/preset-env"],
}),
filesize(),
],
input: "src/index.js",
output: {
file: "dist/index.js",
format: "cjs",
const reporters = ["boxen"];

export default [
{
external: ["fs", "util", ...Object.keys(pkg.dependencies)],
plugins: [
babel({
babelrc: false,
presets: [["@babel/preset-env", { targets: { node: 8 } }]],
}),
// filesize(),
],
input: "src/index.js",
output: {
sourcemap: true,
file: `dist/index.js`,
format: "cjs",
},
},
};
...reporters.map((reporter) => {
return {
plugins: [
babel({
babelrc: false,
presets: [["@babel/preset-env", { targets: { node: 8 } }]],
}),
// filesize(),
],
input: `src/reporters/${reporter}`,
output: {
exports: "named",
sourcemap: true,
file: `dist/reporters/${reporter}.js`,
format: "cjs",
},
};
}),
];
159 changes: 57 additions & 102 deletions src/index.js
Expand Up @@ -2,90 +2,28 @@ import { readFile as origReadFile } from "fs";
import { promisify } from "util";

import fileSize from "filesize";
import boxen from "boxen";
import colors from "colors";
import merge from "lodash.merge";
import gzip from "gzip-size";
import terser from "terser";
import brotli from "brotli-size";

const readFile = promisify(origReadFile);

async function render(opt, outputOptions, info) {
const primaryColor = opt.theme === "dark" ? "green" : "black";
const secondaryColor = opt.theme === "dark" ? "yellow" : "blue";

const title = colors[primaryColor].bold;
const value = colors[secondaryColor];

const values = [
...(outputOptions.file
? [`${title("Destination: ")}${value(outputOptions.file)}`]
: info.fileName
? [`${title("Bundle Name: ")} ${value(info.fileName)}`]
: []),
...(info.bundleSizeBefore
? [
`${title("Bundle Size: ")} ${value(info.bundleSize)} (was ${value(
info.bundleSizeBefore
)})`,
]
: [`${title("Bundle Size: ")} ${value(info.bundleSize)}`]),
...(info.minSize
? info.minSizeBefore
? [
`${title("Minified Size: ")} ${value(info.minSize)} (was ${value(
info.minSizeBefore
)})`,
]
: [`${title("Minified Size: ")} ${value(info.minSize)}`]
: []),
...(info.gzipSize
? info.gzipSizeBefore
? [
`${title("Gzipped Size: ")} ${value(info.gzipSize)} (was ${value(
info.gzipSizeBefore
)})`,
]
: [`${title("Gzipped Size: ")} ${value(info.gzipSize)}`]
: []),
...(info.brotliSize
? info.brotliSizeBefore
? [
`${title("Brotli size: ")}${value(info.brotliSize)} (was ${value(
info.brotliSizeBefore
)})`,
]
: [`${title("Brotli size: ")}${value(info.brotliSize)}`]
: []),
];

return boxen(values.join("\n"), { padding: 1 });
}

export default function filesize(options = {}, env) {
let defaultOptions = {
format: {},
theme: "dark",
render: render,
reporter: null,
showBeforeSizes: false,
showGzippedSize: true,
showBrotliSize: false,
showMinifiedSize: true,
};

let opts = merge({}, defaultOptions, options);
if (options.render) {
opts.render = options.render;
}

const getData = async function (outputOptions, bundle) {
let {
format = {},
theme = "dark",
showBeforeSizes = false,
showGzippedSize = true,
showBrotliSize = false,
showMinifiedSize = true,
} = options;

const getLoggingData = async function (outputOptions, bundle) {
const { code, fileName } = bundle;
const info = {};

let codeBefore;
if (opts.showBeforeSizes) {
if (showBeforeSizes) {
try {
codeBefore = await readFile(
outputOptions.file || outputOptions.dest,
Expand All @@ -98,52 +36,67 @@ export default function filesize(options = {}, env) {

info.fileName = fileName;

info.bundleSize = fileSize(Buffer.byteLength(code), opts.format);
info.bundleSize = fileSize(Buffer.byteLength(code), format);

info.brotliSize = opts.showBrotliSize
? fileSize(brotli.sync(code), opts.format)
: "";
info.brotliSize = showBrotliSize ? fileSize(brotli.sync(code), format) : "";

if (opts.showMinifiedSize || opts.showGzippedSize) {
if (showMinifiedSize || showGzippedSize) {
const minifiedCode = terser.minify(code).code;
info.minSize = opts.showMinifiedSize
? fileSize(minifiedCode.length, opts.format)
info.minSize = showMinifiedSize
? fileSize(minifiedCode.length, format)
: "";
info.gzipSize = opts.showGzippedSize
? fileSize(gzip.sync(minifiedCode), opts.format)
info.gzipSize = showGzippedSize
? fileSize(gzip.sync(minifiedCode), format)
: "";
}

if (codeBefore) {
info.bundleSizeBefore = fileSize(
Buffer.byteLength(codeBefore),
opts.format
);
info.brotliSizeBefore = opts.showBrotliSize
? fileSize(brotli.sync(codeBefore), opts.format)
info.bundleSizeBefore = fileSize(Buffer.byteLength(codeBefore), format);
info.brotliSizeBefore = showBrotliSize
? fileSize(brotli.sync(codeBefore), format)
: "";
if (opts.showMinifiedSize || opts.showGzippedSize) {
if (showMinifiedSize || showGzippedSize) {
const minifiedCode = terser.minify(codeBefore).code;
info.minSizeBefore = opts.showMinifiedSize
? fileSize(minifiedCode.length, opts.format)
info.minSizeBefore = showMinifiedSize
? fileSize(minifiedCode.length, format)
: "";
info.gzipSizeBefore = opts.showGzippedSize
? fileSize(gzip.sync(minifiedCode), opts.format)
info.gzipSizeBefore = showGzippedSize
? fileSize(gzip.sync(minifiedCode), format)
: "";
}
}

const rendered = opts.render(opts, outputOptions, info);
const reporters = options.reporter || ["boxen"];

if (opts.reporter) {
await opts.reporter(opts, outputOptions, info);
}

return rendered;
return (
await Promise.all(
reporters.map(async (reporter) => {
if (typeof reporter === "string") {
if (reporter === "boxen") {
reporter = "/reporters/boxen.js";
}
reporter = (await import(__dirname + reporter)).default;
}
return reporter(
{
format,
theme,
reporter,
showBeforeSizes,
showGzippedSize,
showBrotliSize,
showMinifiedSize,
},
outputOptions,
info
);
})
)
).join("");
};

if (env === "test") {
return getData;
return getLoggingData;
}

return {
Expand All @@ -159,11 +112,13 @@ export default function filesize(options = {}, env) {
return !currentBundle.isAsset;
})
.map((currentBundle) => {
return getData(outputOptions, currentBundle);
return getLoggingData(outputOptions, currentBundle);
})
);
dataStrs.forEach((str) => {
console.log(str);
if (str) {
console.log(str);
}
});
},
};
Expand Down

0 comments on commit c5220eb

Please sign in to comment.