oxc-minify: clarify "Core Assumptions" like "No Monkey-Patching Built-ins" #14941
-
|
I'm sorry, but I don't understand what "Core Assumptions" means here. Does it mean that if I violate the assumption, incorrect minified code will be generated or what? When I tried to minify this code I didn't see any exception: // The minifier assumes this never happens:
Array.prototype.push = function() {
console.log('hijacked!');
};
Object.defineProperty(Number.prototype, 'toString', { value: () => 'hijacked!' });
let array = [];
array.push("a");
console.log(array);
let n = 123;
console.log(n.toString());Array.prototype.push=function(){console.log(`hijacked!`)},Object.defineProperty(Number.prototype,`toString`,{value:()=>`hijacked!`});let array=[];array.push(`a`),console.log(array);let n=123;console.log(n.toString());Please help me better understand these "Core Assumptions", preferably with some unexpected examples, thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
|
If you patch the This below is very unlikely a real example, but it's simple so it communicates the problem well. Array.prototype.push = function() {
console.log('hijacked!');
};
const array = [];
array.push(1);
array.push(2);Could be minified to something like the below. Array.prototype.push = function() {
console.log('hijacked!');
};
const array = [1, 2];If you run the original code and the possible minified output, you'll see that the behavior is different. The first creates an empty array and logs two statements. The second provides you an array with 2 elements and does not log anything. |
Beta Was this translation helpful? Give feedback.
I don't think it'll produce unexpected results because you'd not hook/override the functions for the code you own.