Skip to content

5.2.0-dev.2 imports unused modules in JS output #3839

Closed

Description

@mlms13

Since trying out 5.2.0-dev.2, I've noticed quite a few unused imports in the compiled files. In particular, this seems to happen with modules that alias others as submodules. For front-end assets, this has the potential to add bloat to the bundled JS, though bundlers that support tree-shaking will probably eliminate the issue.

Here's a very simple example using Relude:

let () = Js.log (Relude.List.toArray [])

The Relude module looks roughly like:

module List = Relude_List
module Array = Relude_Array
(* ...lots of additional module aliases here *)

Currently, this compiles to the following JS, where the first import is unused:

import * as Relude from "relude/src/Relude.bs.js";
import * as Relude_List from "relude/src/Relude_List.bs.js";

console.log(Relude_List.toArray(/* [] */0));

Downgrading to bs-platform@5.2.0-dev.1, the output is:

import * as Relude_List from "relude/src/Relude_List.bs.js";

console.log(Relude_List.toArray(/* [] */0));

This doesn't only happen with third-party modules. In our larger (private, unfortunately) app, I was seeing the same thing with imports for our own internal modules.

Activity

  1. mlms13 commented on Sep 21, 2019

    @mlms13
    ContributorAuthor

    I pushed a small demo repository, in case that helps: https://github.com/mlms13/bsb-issue-3839

  2. bobzhang commented on Sep 22, 2019

    @bobzhang
    Member

    I am not sure what's the right solution.
    Recently we made a fix that will track effectful module alias, module A = B if B is not pure, it will still be linked in

  3. bobzhang commented on Sep 23, 2019

    @bobzhang
    Member

    I am going to close this issue, let me know if you have better ideas.

  4. mlms13 commented on Sep 23, 2019

    @mlms13
    ContributorAuthor

    I think that's fine. I'll keep an eye out for modules with side effects that don't seem like they should need to be side-effectful, and any amount of JS tree-shaking should take care of the rest.

  5. mlms13 commented on Sep 26, 2019

    @mlms13
    ContributorAuthor

    Recently we made a fix that will track effectful module alias, module A = B if B is not pure, it will still be linked in

    I looked through the changelog and I didn't see a mention of this. Do you have an issue or pull request I could look at for more detail? Is there any way this behavior could be made opt-in (or opt-out)?

    I understand that it may be correct to import unused modules that have effects, but it seems like the use-case for that would be quite rare in an fp language, and this is having a fairly substantial impact on our JS bundle size.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions