Skip to content

Make exported consts better minifiable #7565

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

Closed
chrmarti opened this issue Mar 17, 2016 · 5 comments
Closed

Make exported consts better minifiable #7565

chrmarti opened this issue Mar 17, 2016 · 5 comments
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@chrmarti
Copy link

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.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 17, 2016

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

@mhegazy mhegazy closed this as completed Mar 17, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label Mar 17, 2016
@chrmarti
Copy link
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.

@RyanCavanaugh RyanCavanaugh removed the Duplicate An existing issue was already created label Mar 17, 2016
@RyanCavanaugh RyanCavanaugh reopened this Mar 17, 2016
@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Jun 2, 2016
@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Jun 9, 2016
@RyanCavanaugh
Copy link
Member

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

@mhegazy mhegazy added this to the Community milestone Jun 9, 2016
@pjordaan
Copy link

pjordaan commented Jan 6, 2017

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.

@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
@RyanCavanaugh
Copy link
Member

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

@RyanCavanaugh RyanCavanaugh closed this as not planned Won't fix, can't repro, duplicate, stale Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants