question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lexical variables in the outermost lexical scope don’t get mangled

See original GitHub issue

Lexical variables in the outermost lexical scope don’t get mangled, i.e. they appear to be incorrectly treated as global variables.

To Reproduce

Minimal code to reproduce the bug:

// foo.mjs
const foo = 42;
const bar = 64;
$ minify foo.mjs

Actual Output

const foo=42,bar=64;

Expected Output

const a=42,b=64;

I’m using the babel-minify CLI, v0.4.3, with no additional configuration.

As a workaround, I have to wrap my source code in a block: { … }. That way, babel-plugin-minify-mangle-names seems to kick in correctly.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mathiasbynenscommented, Sep 24, 2018

To clarify what I mean:

  1. Enabling --mangle.topLevel for .mjs files (as @boopathi suggested) solves the problem for modules. It would be great to do this!
  2. Adding something like --mangle.topLevelLexical would enable this functionality for non-modules too.
0reactions
bakkotcommented, Nov 20, 2018

@mathiasbynens, for what it’s worth, I would be very surprised if those names were changed without --mangle.topLevel. I expect the default behavior to be that compiled programs have the same observable semantics as the input, modulo Function.prototype.toString and function names (and also stack traces and other non-spec things). And the names in the top-level lexical scope are very much part of the observable semantics of a program, just like names in the global scope.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is Mathematica's lexical scoping broken?
With is a scoping construct that implements read-only lexical variables. With replaces symbols in expr only when they do not occur as local ......
Read more >
Lexical Scope in JavaScript – What Exactly Is Scope in JS?
Scope refers to the area where an item (such as a function or variable) is visible and accessible to other code. Note: Scope...
Read more >
What is lexical scope? - javascript - Stack Overflow
Lexical scope means that in a nested group of functions, the inner functions have access to the variables and other resources of their...
Read more >
You-Don-t-Know-JS-Scope-Closures.pdf - Pepa
To define it somewhat circularly, lexical scope is scope that is defined at lexing time. In other words, lexical scope is based on...
Read more >
Don't use named lexical subroutines – The Effective Perler
Remember how lexical variables work. You can have package (global) variables and lexical variables with the same name and they do not affect ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found