Skip to content

Make exported consts better minifiable #7565

Closed as not planned
Closed as not planned

Description

TypeScript Version:

1.8.2 (Playground on 20160317)

Code

module Some {
    export const thing = "";
}

Expected behavior:

Should compile to:

var Some;
(function (Some) {
    var thing = "";
    Some.thing = thing;
})(Some || (Some = {}));

So internal references can use the local variable which will minify well. Exported functions and classes already compile minifier-friendly.

Actual behavior:

But compiles to:

var Some;
(function (Some) {
    Some.thing = "";
})(Some || (Some = {}));

So internal references use the nested reference which does not minify.

Activity

  1. mhegazy commented on Mar 17, 2016

    @mhegazy
    Contributor

    now every other reference to thing needs to be Some.thing. this not only impacts minification, it also impacts performance.

    I expect what you want is constant folding and propagation, see #3964

  2. chrmarti commented on Mar 17, 2016

    @chrmarti
    Author

    This is different from what I understand. I specifically want to avoid const values being inlined because that that makes the resulting code larger. The goal is to have code that can be shrunk better by a minifier for JavaScript.

    To compare a few examples:

    module Some {
        export const thing = "";
        export function getThing() {
            return thing;
        }
        getThing();
        export class Thing {}
        var t = new Thing();
    }

    This compiles to:

    var Some;
    (function (Some) {
        Some.thing = "";
        function getThing() {
            return Some.thing;
        }
        Some.getThing = getThing;
        getThing();
        var Thing = (function () {
            function Thing() {
            }
            return Thing;
        }());
        Some.Thing = Thing;
        var t = new Thing();
    })(Some || (Some = {}));

    A JavaScript minfier will rename function getThing and the two direct references to some short name. Likewise with the class Thing and its two direct references. With the exported const though, it cannot do that, because that is not assigned to a local variable.

    The request here is to have a local variable for the exported const to which the value is assigned and which is then referenced everywhere from within the local scope.

  3. RyanCavanaugh commented on Jun 9, 2016

    @RyanCavanaugh
    Member

    Approved but please wait for the new transforms-based emitter to merge before sending a PR

  4. added this to the milestone on Jun 9, 2016
  5. pjordaan commented on Jan 6, 2017

    @pjordaan

    most people use uglify.js for minification.
    If i read the documentations of uglify.js I can define a const with the const keyword in a javascript file and make uglify.js remove dead code/optimize the constant. However uglify.js will only output const keyword in ES6 code, which uglify.js can't parse. If I output it to ES5 code it becomes a regular var variable and uglify.js can't optimize it better. Would be nice to have an option to force the use of the const keyword in ES5 output, or prefix the var statement with the comment /** @const */

    Doing something like that would make the code better minifiable in all cases.

  6. RyanCavanaugh commented on Mar 4, 2024

    @RyanCavanaugh
    Member

    No one seemed interested in sending a PR for this and very few people are using value-based namespace anymore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions