terser / html-minifier-terser

actively maintained fork of html-minifier - minify HTML, CSS and JS code using terser - supports ES6 code
https://terser.org/html-minifier-terser
MIT License
385 stars 32 forks source link

No support for ES2020? #139

Closed EmptyDreams closed 1 year ago

EmptyDreams commented 1 year ago

When I inline the HTML <script></script> with ?., html-minifier-terse will skip the compression of that element directly.

For example.

<html>
<script>
    (() => {
        const sw = navigator.serviceWorker
        const error = () => {}
        if (!sw?.register('/sw.js')?.then(() => {
            if (!sw.controller) location.reload()
        })?.catch(error)) error()
    })()
</script>
</html>

After compression, it will look like the following.

<html><script>(() => {
        const sw = navigator.serviceWorker
        const error = () => {}
        if (!sw?.register('/sw.js')?.then(() => {
            if (!sw.controller) location.reload()
        })?.catch(error)) error()
    })()</script></html>

If I put all the ?. are replaced by . will be compressed properly, like this below.

<html><script>(()=>{const r=navigator.serviceWorker,e=()=>{};r.register("/sw.js").then(()=>{r.controller||location.reload()}).catch(e)})()</script></html>
DanielRuf commented 1 year ago

Thanks for reporting this.

Since the js compression logic comes from terser, I suggest testing if a newer terser version fixes this.

This bug should be reported at https://github.com/terser/terser/issues so I'm going to close this one here.

Please link the terser issue here then so we can check what we will have to do then.

EmptyDreams commented 1 year ago

Thanks for reporting this.

Since the js compression logic comes from terser, I suggest testing if a newer terser version fixes this.

This bug should be reported at https://github.com/terser/terser/issues so I'm going to close this one here.

Please linke the terser issue here then so we can check what we will have to do then.

But another gulp plugin "gulp-terser" can compress this JS correctly, it uses terser@5.9.0.

DanielRuf commented 1 year ago

In html-minifier-terser a newer version is used: https://github.com/terser/html-minifier-terser/blob/master/package.json#L83

Also at https://terser.org/html-minifier-terser/ I can not reproduce your problem.

Please double-check the version of html-minifier-terser that you use, and its configuration.

EmptyDreams commented 1 year ago

Please double-check the version of html-minifier-terser that you use, and its configuration.

I'm sorry to trouble you. When I used ncu to check for updates, I did not check for the new version. I thought I was using the latest version, and the problem no longer existed after the update.

DanielRuf commented 1 year ago

the problem no longer existed after the update

Perfect, thanks for the update. If there are any other obstacles, please let us know in separate issues.

DanielRuf commented 1 year ago

I'm sorry to trouble you.

Everything is fine. I just had a rough week.

Besides that maybe it would be helpful to ask / clarify in an issue template what version of html-minifier-terser is used and mention that the js compression part is solely handled by terser.

So if new issues appear in the latest version of html-minifier-terser it might be hlpful to point users to the bugtracker of terser to clarify things there first.

If the issues can then be solved by either updating terser on our side or adjusting its configuration used by us, then we can do the relevant changes and prepare a new release.

What do you think @sibiraj-s?

sibiraj-s commented 1 year ago

Yes. That would be a great idea. It should deflect to the right place/solution to some of the issues which you have mentioned.

Let me add an issue template for this.

EmptyDreams commented 1 year ago

I apologize for not being able to respond to you in a timely manner, as I have been ill recently.

I'm glad I could provide some minor help in refining the question template.

I think it's a very good idea to add some templates that tell users what information they need to provide and include a reminder to double-check the version they're using at github.