Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid output when using global assignments #5927

Closed
AshleyScirra opened this issue Aug 22, 2024 · 4 comments · Fixed by #5928
Closed

Invalid output when using global assignments #5927

AshleyScirra opened this issue Aug 22, 2024 · 4 comments · Fixed by #5928
Labels

Comments

@AshleyScirra
Copy link
Contributor

Uglify version (uglifyjs -V) 3.19.2

JavaScript input

globalThis.Unused = {};

globalThis.Namespace = {};
globalThis.Namespace.Foo = {
	bar: false,
};

The uglifyjs CLI command executed or minify() options used.

uglifyjs input.js --compress --beautify --output output.js

JavaScript output or error produced.

globalThis.Unused = {}, globalThis.Namespace.Foo = {
    bar: !(globalThis.Namespace = {})
};

Note in the output code there is an error as self.Namespace is not declared. For some reason the creation of Namespace has been moved to after it is used, so UglifyJS has transformed this from working code in to broken code.

If you comment out the line globalThis.Unused = {}; then it works correctly - so it looks like UglifyJS encountering the first global ends up in a state which then causes it to process the second global incorrectly.

@AshleyScirra AshleyScirra changed the title Invalid output when using scopes and globals Invalid output when using global assignments Aug 22, 2024
@AshleyScirra
Copy link
Contributor Author

The problem appears to be with the collapse_vars option. It can be worked around by passing false for that option, e.g.:

uglifyjs input.js --compress collapse_vars=false --beautify --output output.js

produces the following working output:

globalThis.Unused = {}, globalThis.Namespace = {}, globalThis.Namespace.Foo = {
    bar: !1
};

@alexlamsl alexlamsl added the bug label Aug 25, 2024
@alexlamsl
Copy link
Collaborator

Thanks for the detailed report − investigating.

alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Aug 25, 2024
@alexlamsl
Copy link
Collaborator

Patch released in uglify-js@3.19.3

@AshleyScirra
Copy link
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants