1
0
mirror of https://github.com/S2-/minifyfromhtml.git synced 2025-08-04 04:40:05 +02:00

add some packages

This commit is contained in:
s2
2018-05-05 13:54:07 +02:00
parent 48c1138518
commit ff6e20677d
3738 changed files with 215920 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
"use strict";
// this fixes a bug where converting let to var
// doesn't change the binding's scope to function scope
// https://github.com/babel/babel/issues/4818
module.exports = function (mangler) {
mangler.program.traverse({
VariableDeclaration(path) {
if (path.node.kind !== "var") {
return;
}
const fnScope = path.scope.getFunctionParent() || path.scope.getProgramParent();
const bindingIds = path.getOuterBindingIdentifierPaths();
for (const name in bindingIds) {
const binding = path.scope.getBinding(name); // var isn't hoisted to fnScope
if (binding.scope !== fnScope) {
const existingBinding = fnScope.bindings[name]; // make sure we are clear that the fnScope doesn't already have
// an existing binding
if (!existingBinding) {
// move binding to the function scope
// update our scopeTracker first before
// we mutate the scope
mangler.scopeTracker.moveBinding(binding, fnScope);
fnScope.bindings[name] = binding;
binding.scope = fnScope;
delete binding.scope.bindings[name];
} else {
// we need a new binding that's valid in both the scopes
// binding.scope and fnScope
const newName = fnScope.generateUid(binding.scope.generateUid(name)); // rename binding in the original scope
mangler.rename(binding.scope, binding, name, newName); // move binding to fnScope as newName
// update our scopeTracker first before
// we mutate the scope
mangler.scopeTracker.moveBinding(binding, fnScope);
fnScope.bindings[newName] = binding;
binding.scope = fnScope;
delete binding.scope.bindings[newName];
}
}
}
}
});
};