tdewolff-minify
Description
In search for the best (HTML, CSS, JavaScript, JSON, etc) minifier I happened to come across tdewolff's minify written in Go. Which seems to have awesome performance and capabilities. But it had no JavaScript API (npm library) available to make it easy to plug into a Node.js project; hence I made this one.
With this npm package you just need to npm install tdewolff-minify
and it will download the latest native precompiled binary for your platform (by fetching it straight from GitHub). And it comes with a nice JavaScript API for easy usage of the minifier.
Disclaimer
I'm in no way the author of minify or affiliated with his project. I only take credit for creating this JavaScript API which allows easy usage of it within Node.js.
Funding
If you find this useful then please consider helping me out (I'm jobless and sick). For more information visit my GitHub profile.
It would also be a good idea to fund the creator of minify, which you can do here.
How to use
Install using NPM
npm i tdewolff-minify
Usage demo
import {Minify, minifyStream, minifyPath} from 'tdewolff-minify'
import {Writable} from 'node:stream'
const moduleDirectory = import.meta.url.slice(7, import.meta.url.lastIndexOf('/')+1)
const log = console.log
process.chdir(moduleDirectory)
function stdoutWritable() {
return new Writable({
write(chunk, _encoding, callback) {
process.stdout.write(chunk, callback)
}
})
}
const minify = new Minify({maxConcurrency: 4})
try {
const result = await minify.file('fileToMinify.js')
log('success:', result, '\n')
} catch (error) {
log('error:', error, '\n')
}
minify.content('js', 'let a = 123;')
.then(
result => log('success:', result, '\n'),
error => log('error:', error, '\n')
)
const {createReadStream} = await import('node:fs')
await minify.pipe('js',
createReadStream('fileToMinify.js'),
stdoutWritable()
)
log('\n')
const {createGzip} = await import('node:zlib')
const {pipeline, Transform} = await import('node:stream')
pipeline(
createReadStream('fileToMinify.js'),
await minifyStream('js'),
createGzip(),
new Transform({
transform(chunk, _encoding, callback) {
callback(null, chunk.toString('hex'))
}
}),
stdoutWritable(),
error => {
if (error) log('\n', error, '\n')
else log('\n\nSuccess', '\n')
}
)
log('Minify path:', minifyPath, '\n')
try {
await minify.fileToFile('fileToMinify.js', 'fileToMinify.min.js')
} catch (error) {
log('error:', error, '\n')
}
End of readme
Even though it's encapsulated in multiple layers of crap;
know that inside of you there is pure love, because that's what you are!