Skip to content

Commit

Permalink
fix(terser): remove terser and make minification optional
Browse files Browse the repository at this point in the history
closes #28 and closes #31
  • Loading branch information
ritz078 committed Oct 14, 2018
1 parent 6420e98 commit 32763af
Show file tree
Hide file tree
Showing 6 changed files with 604 additions and 80 deletions.
10 changes: 8 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ rollup({

## options

#### showMinifiedSize
type: `boolean`
default: true

Whether to show minified size or not

#### showGzippedSize
type: `boolean`
default: true
Expand All @@ -52,8 +58,8 @@ return the command that you want to log. Eg:

```js
filesize({
render : function (options, size){
return size;
render : function (options, bundle, { minSize, gzipSize, brotliSize, bundleSize }){
return minSize;
}
})
```
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
},
"homepage": "https://github.com/ritz078/rollup-plugin-filesize#readme",
"dependencies": {
"boxen": "^1.1.0",
"brotli-size": "0.0.2",
"colors": "^1.1.2",
"babel-minify": "^0.5.0",
"boxen": "^2.0.0",
"brotli-size": "0.0.3",
"colors": "^1.3.2",
"deep-assign": "^2.0.0",
"filesize": "^3.5.6",
"gzip-size": "^3.0.0",
"terser": "^3.8.0"
"filesize": "^3.6.1",
"gzip-size": "^5.0.0"
},
"devDependencies": {
"ava": "^0.19.1",
Expand Down
31 changes: 16 additions & 15 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import babel from 'rollup-plugin-babel';
import filesize from "./src/index"
import babel from "rollup-plugin-babel";
import filesize from "./src/index";
import pkg from "./package.json";

export default {
external: [ 'filesize', 'boxen', 'chalk', 'deep-assign', 'colors', 'gzip-size', 'brotli-size' ],
plugins: [
babel({
babelrc:false,
presets: [ 'es2015-rollup' ]
}),
filesize()
],
input: 'src/index.js',
output: {
file: 'dist/index.js',
format: 'cjs'
}
external: Object.keys(pkg.dependencies),
plugins: [
babel({
babelrc: false,
presets: ["es2015-rollup"]
}),
filesize()
],
input: "src/index.js",
output: {
file: "dist/index.js",
format: "cjs"
}
};
37 changes: 22 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import colors from "colors";
import deepAssign from "deep-assign";
import gzip from "gzip-size";
import brotli from "brotli-size";
import terser from "terser";
import minify from "babel-minify";

function render(opt, size, gzip, brotliSize, minifiedSize, bundle) {
function render(opt, bundle, sizes) {
const primaryColor = opt.theme === "dark" ? "green" : "black";
const secondaryColor = opt.theme === "dark" ? "yellow" : "blue";

Expand All @@ -15,11 +15,10 @@ function render(opt, size, gzip, brotliSize, minifiedSize, bundle) {

const values = [
...(bundle.file ? [`${title("Destination: ")}${value(bundle.file)}`] : []),
...[`${title("Bundle Size: ")} ${value(size)}`],
...[`${title("Minified and Gzipped Size: ")} ${value(minifiedSize)}`],
...(opt.showBrotliSize
? [`${title("Brothli size: ")}${value(brotliSize)}`]
: [])
...[`${title("Bundle Size: ")} ${value(sizes.bundleSize)}`],
...(sizes.minSize ? [`${title("Minified Size: ")} ${value(sizes.minSize)}`] : []),
...(sizes.gzipSize ? [`${title("Gzipped Size: ")} ${value(sizes.gzipSize)}`] : []),
...(sizes.brotliSize ? [`${title("Brothli size: ")}${value(sizes.brotliSize)}`] : [])
];

return boxen(values.join("\n"), { padding: 1 });
Expand All @@ -31,7 +30,8 @@ export default function filesize(options = {}, env) {
theme: "dark",
render: render,
showGzippedSize: true,
showBrotliSize: false
showBrotliSize: false,
showMinifiedSize: true
};

let opts = deepAssign({}, defaultOptions, options);
Expand All @@ -40,17 +40,24 @@ export default function filesize(options = {}, env) {
}

const getData = function(bundle, code) {
let size = fileSize(Buffer.byteLength(code), opts.format);
let gzipSize = opts.showGzippedSize
? fileSize(gzip.sync(code), opts.format)
: "";
let brotliSize = opts.showBrotliSize
const sizes = {};
sizes.bundleSize = fileSize(Buffer.byteLength(code), opts.format);

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

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

return opts.render(opts, size, gzipSize, brotliSize, minifiedSize, bundle);
return opts.render(opts, bundle, sizes);
};

if (env === "test") {
Expand Down
7 changes: 4 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ test('fileSize should return correct string', t => {

test('fileSize should apply correct template', t => {
const options = {
render: function (opts, size) {
return size;
render: function (opts, bundle, { gzipSize }) {
return gzipSize;
}
};

const z = filesize(options, "test");
const expected = '54 B';
const expected = '47 B';
console.log(z({dest: 'abc.js'}, code))
t.is(z({dest: 'abc.js'}, code), expected)
});
Loading

0 comments on commit 32763af

Please sign in to comment.