Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mangle properties prefix feature proposal #1001

Closed
bo4arovpavel1989 opened this issue May 27, 2021 · 6 comments
Closed

Mangle properties prefix feature proposal #1001

bo4arovpavel1989 opened this issue May 27, 2021 · 6 comments

Comments

@bo4arovpavel1989
Copy link

Bug report or Feature request?

Feature request

Version (complete output of terser -V or specific git commit)

using terser via terser-webpack-plugin v5.0.3

Complete CLI command or minify() options used

new TerserPlugin({ 
    parallel: false, 
    terserOptions: {
        nameCache: {},                            
        mangle: {
            properties: { regex: /^_private_/ }                            
        }                        
    }
})

Problem description
We use TerserPlugin with webpack, bundling several async chunks. Terser is configured to minify private properties of class methods by regexp, also parallel minifying disabled and nameCache provided to avoid unwanted method and property names overriding in inherited classes, when parent and child classes are located in different async chunks. But it still remains issue, that private property mangling can override parent's public/protected method or property. Issue could be solved by using reserved option, but having complex project we can not provide such a full reserved list.
Proposed solution
We'd like to have an option to mangle properties by some defined prefix, whitch is never used in props and methods, that are not meant to be mangled.
Propose config example:

new TerserPlugin({ 
    parallel: false, 
    terserOptions: {
        nameCache: {},                            
        mangle: {
            properties: { regex: /^_private_/, outputPrefix: '$' }                            
        }                        
    }
})

Terser input:

class A {
   _private_foo = 'foo';
   _private_bar() {
        console.log('bar');
    }
}

Desired terser mangling properties output:

class A {
   $i = 'foo';
   $j() {
        console.log('bar');
    }
}
@bo4arovpavel1989
Copy link
Author

up! Hello, project maintainers! Any thoughts about the issue? If any, i would be glad to contribute

@jridgewell
Copy link
Collaborator

Seems #1038 would solve this usecase?

@fabiosantoscode
Copy link
Collaborator

Closed in #1038 -- thanks @jridgewell

@fvittorello
Copy link

Hi guys, I'm trying to set a prefix to all of the variables and properties of a project but couldn't understand how to solve it with the post mentioned, do you have an example of what the config should be like?

@fabiosantoscode
Copy link
Collaborator

Both mangle.nth_identifier and mangle.properties.nth_identifier options can be a variable name generator. In this generator, a prefix can be added.

The default generator is here.

terser/lib/scope.js

Lines 1015 to 1061 in d32e177

const base54 = (() => {
const leading = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_".split("");
const digits = "0123456789".split("");
let chars;
let frequency;
function reset() {
frequency = new Map();
leading.forEach(function(ch) {
frequency.set(ch, 0);
});
digits.forEach(function(ch) {
frequency.set(ch, 0);
});
}
function consider(str, delta) {
for (var i = str.length; --i >= 0;) {
frequency.set(str[i], frequency.get(str[i]) + delta);
}
}
function compare(a, b) {
return frequency.get(b) - frequency.get(a);
}
function sort() {
chars = mergeSort(leading, compare).concat(mergeSort(digits, compare));
}
// Ensure this is in a usable initial state.
reset();
sort();
function base54(num) {
var ret = "", base = 54;
num++;
do {
num--;
ret += chars[num % base];
num = Math.floor(num / base);
base = 64;
} while (num > 0);
return ret;
}
return {
get: base54,
consider,
reset,
sort
};
})();
It not only creates variable names, but it can also take in information about what characters are most common, to make more gzip-friendly names.

@fvittorello
Copy link

Thanks @fabiosantoscode, I will check them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants