-
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ResetNext identifier only when reuse is true Fix tests - add keepClassName Reuse vars as default I dont know why it works - #326 Extract tracker to a separate file, Add topLevel Option
- Loading branch information
Showing
8 changed files
with
1,791 additions
and
208 deletions.
There are no files selected for viewing
1,152 changes: 1,152 additions & 0 deletions
1,152
packages/babel-plugin-minify-mangle-names/__tests__/mangle-names-reuse-test.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"use strict"; | ||
|
||
const CHARSET = ("abcdefghijklmnopqrstuvwxyz" + | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ$_").split(""); | ||
|
||
module.exports = class Charset { | ||
constructor(shouldConsider) { | ||
this.shouldConsider = shouldConsider; | ||
this.chars = CHARSET.slice(); | ||
this.frequency = {}; | ||
this.chars.forEach((c) => { this.frequency[c] = 0; }); | ||
this.finalized = false; | ||
} | ||
|
||
consider(str) { | ||
if (!this.shouldConsider) { | ||
return; | ||
} | ||
|
||
str.split("").forEach((c) => { | ||
if (this.frequency[c] != null) { | ||
this.frequency[c]++; | ||
} | ||
}); | ||
} | ||
|
||
sort() { | ||
if (this.shouldConsider) { | ||
this.chars = this.chars.sort( | ||
(a, b) => this.frequency[b] - this.frequency[a] | ||
); | ||
} | ||
|
||
this.finalized = true; | ||
} | ||
|
||
getIdentifier(num) { | ||
if (!this.finalized) { | ||
throw new Error("Should sort first"); | ||
} | ||
|
||
let ret = ""; | ||
num++; | ||
do { | ||
num--; | ||
ret += this.chars[num % this.chars.length]; | ||
num = Math.floor(num / this.chars.length); | ||
} while (num > 0); | ||
return ret; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/babel-plugin-minify-mangle-names/src/counted-set.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Set that counts | ||
module.exports = class CountedSet { | ||
constructor() { | ||
// because you can't simply extend Builtins yet | ||
this.map = new Map; | ||
} | ||
keys() { | ||
return [...this.map.keys()]; | ||
} | ||
has(value) { | ||
return this.map.has(value); | ||
} | ||
add(value) { | ||
if (!this.has(value)) { | ||
this.map.set(value, 0); | ||
} | ||
this.map.set(value, this.map.get(value) + 1); | ||
} | ||
delete(value) { | ||
if (!this.has(value)) return; | ||
const count = this.map.get(value); | ||
if (count <= 1) { | ||
this.map.delete(value); | ||
} else { | ||
this.map.set(value, count - 1); | ||
} | ||
} | ||
}; |
71 changes: 71 additions & 0 deletions
71
packages/babel-plugin-minify-mangle-names/src/get-binding-identifiers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Original Source: https://github.com/babel/babel/blob/master/packages/babel-types/src/retrievers.js | ||
* | ||
* The original source is able to return only a Node. But we need Path to work around | ||
* this bug when used with BlockScoping Plugin or ES2015 preset | ||
* - The same Node {type: Identifier, name: "x"} object results in being used | ||
* in multiple places - binding, binding reference, | ||
* and another binding with same name in a different scope | ||
* | ||
* - This might be moved to babel in future | ||
* | ||
* Note: | ||
* This uses the KEYS from the original implementation from Babel | ||
* | ||
*/ | ||
module.exports = function (t) { | ||
const KEYS = t.getBindingIdentifiers.keys; | ||
|
||
return function getBindingIdentifiers(path, duplicates = false, outerOnly = false) { | ||
let search = [].concat(path); | ||
let ids = Object.create(null); | ||
|
||
while (search.length) { | ||
let id = search.shift(); | ||
if (!id) continue; | ||
if (!id.node) continue; | ||
|
||
let keys = KEYS[id.node.type]; | ||
|
||
if (id.isIdentifier()) { | ||
if (duplicates) { | ||
let _ids = ids[id.node.name] = ids[id.node.name] || []; | ||
_ids.push(id); | ||
} else { | ||
ids[id.node.name] = id; | ||
} | ||
continue; | ||
} | ||
|
||
if (id.isExportDeclaration()) { | ||
const declaration = id.get("declaration"); | ||
if (declaration.isDeclaration()) { | ||
search.push(declaration); | ||
} | ||
continue; | ||
} | ||
|
||
if (outerOnly) { | ||
if (id.isFunctionDeclaration()) { | ||
search.push(id.get("id")); | ||
continue; | ||
} | ||
if (id.isFunctionExpression()) { | ||
continue; | ||
} | ||
} | ||
|
||
if (keys) { | ||
for (let i = 0; i < keys.length; i++) { | ||
let key = keys[i]; | ||
const child = id.get(key); | ||
if (child.node) { | ||
search = search.concat(child); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return ids; | ||
}; | ||
}; |
Oops, something went wrong.