Skip to content

Instantly share code, notes, and snippets.

@developit
Last active February 3, 2021 16:04
Show Gist options
  • Save developit/ba6bfe0ccdb65254c850b3d3f26e938d to your computer and use it in GitHub Desktop.
Save developit/ba6bfe0ccdb65254c850b3d3f26e938d to your computer and use it in GitHub Desktop.

Terser (blocking, on main thread)

total: 79.062Kb
index.a5e146fc.js 15.427Kb
chunks/index.92b04243.js 283b
chunks/compat.f7a82e1d.js 59.642Kb
chunks/_wmr.4e88e8af.js 125b
assets/hello.359cce80.js 22b
            Mean        Std.Dev.    Min         Median      Max
real        3.311       0.119       3.189       3.271       3.536       
user        4.509       0.055       4.433       4.502       4.600       
sys         0.332       0.027       0.311       0.319       0.382 

Terser (in a single Worker via worker_threads)

Wrote 79.062Kb to disk: (same as single-threaded)

            Mean        Std.Dev.    Min         Median      Max
real        2.875       0.039       2.825       2.902       2.911       
user        5.522       0.046       5.468       5.511       5.604       
sys         0.409       0.008       0.399       0.406       0.421 

Terser (unlimited concurrency via worker_threads)

Wrote 79.062Kb to disk: (same as single-threaded)

            Mean        Std.Dev.    Min         Median      Max
real        2.996       0.028       2.947       3.014       3.019       
user        6.444       0.069       6.343       6.456       6.552       
sys         0.507       0.006       0.499       0.508       0.515

ESBuild

total: 80.608Kb
index.a5e146fc.js 15.486Kb
chunks/index.92b04243.js 303b
chunks/compat.f7a82e1d.js 61.108Kb
chunks/_wmr.4e88e8af.js 126b
assets/hello.359cce80.js 22b
            Mean        Std.Dev.    Min         Median      Max
real        1.782       0.015       1.768       1.776       1.811       
user        2.028       0.019       2.003       2.028       2.061       
sys         0.294       0.004       0.289       0.294       0.301
import * as esbuild from 'esbuild';
/** @type {esbuild.Service} */
let inst,
svc,
timer,
usage = 0;
function free() {
if (--usage) return;
clearTimeout(timer);
timer = setTimeout(shutdown, 10);
}
function use() {
clearTimeout(timer);
usage++;
}
export function keepalive() {
use();
free();
}
export function shutdown() {
if (inst) inst.stop();
else if (svc) svc.then(inst => usage || inst.stop());
inst = svc = null;
}
process.on('beforeExit', shutdown);
/**
* This function is the same shape as Terser.minify, except that it is async.
* @param {string} code
* @param {esbuild.TransformOptions} opts
*/
export async function transform(code, opts) {
if (!inst) {
if (!svc) {
svc = ((esbuild && esbuild.default) || esbuild).startService();
}
inst = await svc;
}
use();
try {
const result = await inst.transform(code, opts);
return {
code: result.js,
map: result.jsSourceMap || null,
warnings: result.warnings.map(warning => warning.text)
};
} catch (err) {
return {
error: err
};
} finally {
free();
}
}
import { transform, keepalive } from './esbuild-service.js';
/** @returns {import('rollup').Plugin} */
export default function minifyPlugin({ sourcemap } = {}) {
return {
name: 'minify',
buildStart: keepalive,
async renderChunk(code, chunk) {
const out = await transform(code, {
minify: true,
sourcefile: chunk.fileName,
sourcemap: !!sourcemap,
strict: false,
target: 'es6' // 'es5' not fully supported (yet?)
});
if (out.warnings) for (const warn of out.warnings) this.warn(warn);
return {
code: result.js,
map: sourcemap ? JSON.parse(out.jsSourceMap) : null
};
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment