Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A terser script to minify all javascript files in a directory
const fs = require('fs')
const {sync: globSync} = require('glob')
const filesize = require('filesize')
const Terser = require('terser')
const options = require(process.env.TERSER_CONFIG || './terserrc.json')
const getSize = (file) => {
const {size} = fs.statSync(file)
return filesize(size)
}
const files = globSync(`${process.env.TERSER_DIST_PATH || './dist'}/*.js`)
files.map(file => {
console.log(`Minifying ${file} (${getSize(file)})`)
const terserResult = Terser.minify(fs.readFileSync(file, 'utf8'), options)
if (terserResult.error) {
console.log(`Minifying ${file} error.`, terserResult.error)
}
else {
fs.writeFileSync(file, terserResult.code, 'utf8');
console.log(`Minifying ${file} (${getSize(file)}) success.`)
}
})
{
"parse": {
"ecma": 8
},
"compress": {
"ecma": 5,
"warnings": false,
"arrows": false,
"collapse_vars": false,
"comparisons": false,
"computed_props": false,
"hoist_funs": false,
"hoist_props": false,
"hoist_vars": false,
"inline": false,
"loops": false,
"negate_iife": false,
"properties": false,
"reduce_funcs": false,
"reduce_vars": false,
"switches": false,
"toplevel": false,
"typeofs": false,
"booleans": true,
"if_return": true,
"sequences": true,
"unused": true,
"conditionals": true,
"dead_code": true,
"evaluate": true
},
"mangle": {
"safari10": true
},
"output": {
"ecma": 5,
"comments": false,
"ascii_only": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.