How to use the minify function from uglify-es

Find comprehensive JavaScript uglify-es.minify code examples handpicked from public code repositorys.

uglify-es.minify is a tool that minifies JavaScript code, reducing its size by removing unnecessary characters such as whitespace and comments.

8
9
10
11
12
13
14
15
16
17
18
const jszip = require('jszip');


let compressCode = function (jsFile, isMin) {
    if (fs.existsSync(jsFile)) {
        let data = fs.readFileSync(jsFile, 'utf-8');
        let result = uglifyES.minify(data, {
            compress: {
                dead_code: true,// 移除未使用的code
                drop_console: true,//丢弃console代码,默认false
                drop_debugger: true,//丢弃debugger代码,默认true
fork icon282
star icon612
watch icon43

2
3
4
5
6
7
8
9
10
11
12
13
const UglifyJS = require("uglify-es");
const {js_beautify} = require("js-beautify");
const {VM} = require('vm2');


function jsBeautify(code) {
    return UglifyJS.minify(code, {mangle: false, compress: false, output: {beautify: true, comments: true}}).code;
}


function splitJs(name, cb, mainDir) {
    let isSubPkg = mainDir && mainDir.length > 0;
fork icon15
star icon45
watch icon3

How does uglify-es.minify work?

uglify-es.minify is a function provided by the UglifyJS library that minifies JavaScript code. Minification involves analyzing the structure of the code and removing any unnecessary characters that do not affect its functionality. This can include removing comments, whitespace, and unnecessary semicolons, as well as renaming variables and functions to shorter names. The uglify-es.minify function takes an object as its argument, with the following properties: code: the JavaScript code to be minified. compress: an object that specifies the compression options to use when minifying the code. mangle: an object that specifies the mangling options to use when minifying the code. output: an object that specifies the output options to use when generating the minified code. The compress and mangle options control how the code is compressed and obfuscated, respectively. The output option controls the formatting of the minified code, such as whether to include semicolons or line breaks. When uglify-es.minify is called with the appropriate options, it returns an object with the following properties: code: the minified JavaScript code. map: a source map object that maps the minified code back to the original source code. Developers can use uglify-es.minify to reduce the size of their JavaScript code, which can improve the performance of their applications by reducing the amount of data that needs to be transferred over the network. Overall, uglify-es.minify is a powerful tool for optimizing JavaScript code, making it a valuable resource for web developers.

1585
1586
1587
1588
1589
1590
1591
1592
1593
1594

let deployBlock;
if(typeof code === 'string') {
    let wallet = await this.accountManager.getAccountAsync(accountName);

    code = uglifyJs.minify(code).code;
    deployBlock = new EcmaContractDeployBlock(code, {
        randomSeed: random.int(0, 10000),
        from: wallet.id,
        resourceRent: String(resourceRent),
fork icon2
star icon3
watch icon4

87
88
89
90
91
92
93
94
95
96
    return new CleanCSS({}).minify(code).styles;
});

// Minify JS
eleventyConfig.addFilter("jsmin", function(code) {
    let minified = UglifyJS.minify(code);
    if (minified.error) {
        console.log("UglifyJS error: ", minified.error);
        return code;
    }
fork icon2
star icon2
watch icon3

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const UglifyJS = require("uglify-es");

const code = `
function addNumbers(a, b) {
return a + b;
}

console.log(addNumbers(1, 2));
`;

const options = {};

const result = UglifyJS.minify(code, options);

console.log(result.code);

In this example, we first import the uglify-es library and define a block of JavaScript code that we want to minify. We then define an empty options object, which we will use to configure the minification process. We then call the UglifyJS.minify function with code and options as arguments. This generates a minified version of the code, which is returned in the code property of the result object. Finally, we log the resulting minified code to the console using console.log(result.code). When this code runs, it will print the following minified code to the console: javascript Copy code

132
133
134
135
136
137
138
139
140
141
142
143
    return `${generatedCode}\n${initaliser}`;
}


function compress(code) {
    if (process.env.NODE_ENV === 'production') {
        return UglifyJS.minify(iifeTemplate(code)).code;
    }


    return code;
}
fork icon2
star icon2
watch icon3

187
188
189
190
191
192
193
194
195
196
    if (ref && ref.filename && ref.source) {
        scripts[ref.filename] = ref.source;
    }
});

const minified = uglify.minify(scripts, {
    sourceMap: {
        filename: data.filename,
        url: `${String(data.filename).split(/[/\\]/).pop()}.map`,
        includeSources: true,
fork icon1
star icon0
watch icon5

+ 238 other calls in file

465
466
467
468
469
470
471
472
473
474
    // Override the options with user-specified ones, if specified
    if (bundlerOptions.uglifyOptionsOverride) {
        Object.assign(uglifyOptions, bundlerOptions.uglifyOptionsOverride);
    }

    let minification = uglify.minify(code, uglifyOptions);
    
    if (minification.error) throw new Error(minification.error);
    else code = minification.code;
}
fork icon0
star icon1
watch icon1

216
217
218
219
220
221
222
223
224
225
const value = configValues[field];

switch (field) {
	case 'script':
		if (value) {
			const minifiedScript = uglifyEs.minify(value);

			if (minifiedScript.error) {
				throw new Error(`Error while minifying script: ${minifiedScript.error.message}`);
			}
fork icon0
star icon0
watch icon1

+ 417 other calls in file

55
56
57
58
59
60
61
62
63
64
modules = modules.reduce((acc, val) => acc.concat(val), []);

let bundle = await nodeBundle(modules);
if (typeOfConfig === 'prebid') {
    let config = await getPrebidConfig(payload);
    let uglified = UglifyJS.minify(config);
    if (uglified['error'] === undefined) {
        bundle += uglified['code'];
    } else {
        console.error(uglified['error']);
fork icon0
star icon0
watch icon1

+ 2 other calls in file

453
454
455
456
457
458
459
460
461
462
    const {googleTag} = creativeResult;
    payload.googleTag = googleTag;
}
if (dev !== "true") {
    let uglyOptions = {keep_fnames: true};
    let uglifiedCreative = UglifyJS.minify(creative, uglyOptions);
    if (uglifiedCreative['error'] === undefined) {
        payload.creative = uglifiedCreative['code'];
    } else {
        console.error(uglifiedCreative['error']);
fork icon0
star icon0
watch icon1

226
227
228
229
230
231
232
233
234
235
	}

	scripts[ref.filename] = ref.source;
});

var minified = uglify.minify(scripts, {
	sourceMap: {
		filename: data.filename,
		url: String(data.filename).split(/[/\\]/).pop() + '.map',
		includeSources: true,
fork icon0
star icon0
watch icon1

43
44
45
46
47
48
49
50
51
52
function minify(
inputCode,
inputMap,
options)
{ 
  const result = uglify.minify(inputCode, {
    mangle: {
      toplevel: false,
      reserved: options.reserved,
      keep_classnames: true,
fork icon0
star icon0
watch icon5