Skip to content

Pure annotation in downlevel emits #5632

@sonicoder86

Description

@sonicoder86

Feature request

Minifiers like Uglify try to remove dead code from minified builds.
To completely remove a declaration, they have to prove that it is side-effect free.
Unfortunately in JS this is often not possible as pretty much anything can have arbitrary effects.

An important issue is that ES5 emit for class can't easily be determined to be side-effect free.
As a result, tree-shaking is ineffective. In simple words: no class is removed from the output, even if it is never used.

A very simple solution could be to have a hint for the minifier, inside a special comment.
If the emit added a /*#__PURE__*/ comment at the beginning of ES5 classes it could easily be detected and removed by Uglify.

var V6Engine = /*#__PURE__*/(function () {
    function V6Engine() {
    }
    V6Engine.prototype.toString = function () {
        return 'V6';
    };
    return V6Engine;
}());

This is an important enhancement that can dramatically reduce the size of unused code, especially in large libraries.
Uglify|JS already supports this annotation: mishoo/UglifyJS#1448
Original UglifyJS discussion: mishoo/UglifyJS#1261
For reference, implementation details are here: mishoo/UglifyJS@1e51586

The issue was originally raised at Webpack: webpack/webpack#2867

Metadata

Metadata

Assignees

No one assigned

    Labels

    PR: New Feature 🚀A type of pull request used for our changelog categoriesoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions