Skip to content

Instantly share code, notes, and snippets.

@mehulkar
Last active August 5, 2021 16:07
Show Gist options
  • Save mehulkar/a98a18529f82da8f17ca412353c577aa to your computer and use it in GitHub Desktop.
Save mehulkar/a98a18529f82da8f17ca412353c577aa to your computer and use it in GitHub Desktop.
esbuild vs terser
// Overwrote `node_modules/broccoli-terser-sourcemap/lib/process-file.js` with this to try esbuild vs terser
// Original source: https://github.com/ember-cli/broccoli-terser-sourcemap/blob/master/lib/process-file.js
'use strict';
const debugGn = require('debug');
const defaults = require('lodash.defaultsdeep');
const fs = require('fs');
const path = require('path');
const terser = require('terser');
const esbuild = require('esbuild')
const getSourceMapContent = require('./get-sourcemap-content');
const terserDebug = debugGn('broccoli-terser')
const esbuildDebug = debugGn('broccoli-esbuild')
async function processFileTerser(inFile, outFile, relativePath, outDir, silent, _options, src) {
let options = defaults({}, _options.terser);
let start = new Date();
try {
let result = await terser.minify(src, options);
let end = new Date();
let total = end - start;
terserDebug('[finished]: %s (%dKB) -> %dKB in %dms', relativePath, (src.length / 1000), (result.code.length / 1000), total);
fs.writeFileSync(outFile, result.code);
} catch (e) {
e.filename = relativePath;
throw e;
}
}
async function processFileEsBuild(inFile, outFile, relativePath, outDir, silent, _options, src) {
let outFile2 = `${path.basename(outFile)}-esbuild.${path.extname(outFile)}`
let start = new Date();
try {
await esbuild.build({
entryPoints: [inFile],
bundle: false,
minify: true,
outfile: outFile2,
})
let end = new Date();
let total = end - start;
esbuildDebug(`[debug] outFile2 ${outFile2}`);
let result = await fs.promises.stat(outFile2)
esbuildDebug('[finished]: %s (%dKB) -> %dKB in %dms', relativePath, (src.length / 1000), (result.size / 1000), total);
} catch (e) {
e.filename = relativePath;
throw e;
}
}
module.exports = async function processFile(inFile) {
let src = fs.readFileSync(inFile, 'utf-8');
await processFileTerser(...arguments, src);
// enable this line and re-run ember build
// await processFileEsBuild(...arguments, src);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment