babel-dead-code-elimination
Advanced tools
Changelog
1.0.6
d4690c2: Do not eliminate empty object/array pattern function parameters
Function parameters are not dead code
Changelog
1.0.5
3cf19e5: Fix: do not check function expressions for referenced variables
Function expressions do not add their names to outer scopes
5149b08: Do not eliminate arrow expressions
Arrow expressions do not add names to the outer scope.
Arrow expressions bound to names via variable declarators are already handled by VariableDeclarator
visitor.
86af914: Do not eliminate unreferenced variables from array patterns and object patterns when they do not match the provided candidates
Previously, the candidates
were passed in to deadCodeElimination
were not consulted when removing unreferenced variables from within patterns.
This was a bug and has now been fixed.
Changelog
1.0.4
ade9eee: Fix: do not eliminate function expressions
Function expressions do not add their names to outer scope, so they should never be dead code eliminated
Changelog
1.0.3
Changelog
1.0.2
bd5e331: Fix elimination for object patterns and array patterns
Previously, running dead code elimination on object patterns and array patterns (aka destructuring) was brittle. For example:
const {
a: { b: c },
} = z
console.log(c)
Dead code elimination used to incorrectly remove the entire variable declaration even though c
is referenced:
-const {
- a: { b: c },
-} = z
console.log(c);
This was caused by erroneous detection of a
and b
as unreferenced variables.
But a
and b
are not variables, they are object property keys.
Only c
is a variable and it is referenced.
This is now corrected so that variables in object patterns and array patterns are detected only within values of object properties.
This also correctly accounts for cases where the key and value are the same for example { a }
.
Changelog
1.0.1
main
and module
fields in package.json
for older bundlersChangelog
1.0.0
8264d19: Initial release
Eliminates unused code from the Babel AST by repeatedly removing unreferenced identifiers.
deadCodeElimination(ast)
Find identifiers that are currently referenced in the Babel AST.
Useful for limiting deadCodeElimination
to only eliminate newly unreferenced identifiers,
as a best effort to preserve any intentional side-effects in the source.
let ast = parse(source, { sourceType: "module" })
let referenced = findReferencedIdentifiers(ast)
traverse(ast, {
/* ... your custom transform goes here ... */
})
deadCodeElimination(ast, referenced)