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

Why Terser removed your code #1

Open
Schnark opened this issue Oct 5, 2022 · 2 comments
Open

Why Terser removed your code #1

Schnark opened this issue Oct 5, 2022 · 2 comments

Comments

@Schnark
Copy link

Schnark commented Oct 5, 2022

In https://xem.github.io/articles/js13k22 you write: "That's a weird choice from Terser. Especially when we see that it works normally if we write "module: false" in the top-left options... "
That's not a weird choice, and indeed you should have set "module: false".

When you set "module: true" you tell Terser that you will load the code as a module, i.e. via <script type="module">. Inside a module the global variable R is just global to the module, so window.R is something different.

You can test with

<!DOCTYPE html>
<script type="module" src="module.js"></script>
var foo = 0;
window.foo = 1;
console.log('foo = ', foo);
console.log('window.foo = ', window.foo);

So given your code and configuration, Terser correctly assumed that R is never modified, thus always falsy. The removal of the code is correct in this case.

TL;DR: When you don't use your code as module, then don't tell Terser so.

@xem
Copy link
Owner

xem commented Oct 5, 2022

Hi Michael, thanks a lot for your explanations! I will add them in the article!
I understand that you've read my making-of that is still in draft :D
I'm not (yet) very familiar with modules, so I had the feeling that something weird was going on.
And indeed, try.terser.org sets the "module" option to true by default and I'm surprised it hadn't bothered me before, as I've happily used it for years now!
Sorry if my blog post seemed aggressive about Terser. In fact it remains my favourite JS minifier <3

@Schnark
Copy link
Author

Schnark commented Oct 6, 2022

Sorry if my blog post seemed aggressive about Terser.

No need to apologise, it didn't sound like that, and even if it did I wouldn't have cared. My favourite JS minifier is still the Google Closure Compiler.

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

No branches or pull requests

2 participants