Skip to content

support output.libraryTarget: 'module' #2933

Closed

Description

@donaldpipowitch

I'm submitting a feature request

It would be nice, if output.libraryTarget could support the ES2015 module format.

What is the motivation / use case for changing the behavior?

Say I develop a fancy component called fancy-component with webpack. I use a very custom config with specific loader settings e.g. for inlining images and stuff like that. My module exports FancyComponent and BigFancyComponent.

A friend develops a single page application and uses rollup to do that. He knows nothing about webpack, but wants to use my FancyComponent, but not BigFancyComponent. He installs fancy-component and imports FancyComponent. BigFancyComponent should not be included in the build (because of tree shaking).

As far as I know this is currently not possible. My friend would need fancy-component in a format using ES2015 modules. But currently these include webpack specific loader logic. I really need to publish fancy-component without webpack specific logic, but with ES2015 modules.

Activity

  1. TheLarkInn commented on Aug 30, 2016

    @TheLarkInn
    Member

    I definitely see the merit in this. @sokra I don't think we have a way already to export vanilla es2015 / es modules. I think that maybe this can be part of the rollup story (module combining/(and this splitting exporting)).

  2. donaldpipowitch commented on Oct 13, 2016

    @donaldpipowitch
    Author

    Is there anything I can do to help with that? Can I tackle this problem myself as someone who is unfamiliar with webpacks code base? Or is this a rabbit hole into the guts of webpack? :)

  3. TheLarkInn commented on Oct 13, 2016

    @TheLarkInn
    Member

    For a feature like this we should write up the design for the feature. Questions I think should be answerered first:

    • if someone uses CJS/AMD/non-esm dependencies in their library, how does webpack convert this into esm modules?
    • Should externals be leveraged for lib dependencies?
  4. TheLarkInn commented on Oct 13, 2016

    @TheLarkInn
    Member

    Once we have the behavior designed then you can go straight into adding the test cases.

  5. donaldpipowitch commented on Oct 13, 2016

    @donaldpipowitch
    Author

    Seems to be like big step. Could we introduce some workaround in the meantime? Like emitting plain non-bundled commonjs modules without __webpack_require__, webpackBootstrap, etc.?

    Use case is similar. I want to build a project with webpacks powerful loaders and I want to output some sort of modules without webpack specific logic, so that they can be consumed more easily by third party build tools. (Or webpack itself, too! E.g. I can't consume CommonJS modules outputted from webpack right now and use process.env. A good example: https://github.com/facebook/react is created as CommonJS and they use ``process.env.NODE_ENV. Their "main"` file points to the CommonJS entry. They can only do that, because they don't use webpack in the first place. If they would want to include a loader somewhere, they currently couldn't emit a target like they have right now which doesn't force others to use webpack, too.)

  6. donaldpipowitch commented on Oct 14, 2016

    @donaldpipowitch
    Author

    Turns out... it looks like I already can output CommonJS modules and keep process.env. (From my knowledge using CommonJS - or ES2015 modules in the future - and allowing environment variables is the lowest common denominator between current popular bundlers. Other features like loaders are webpack-specific.)

    You need to set output.libraryTarget: 'commonjs2' (I guess most webpack users know this) and you need to set something like new DefinePlugin({ 'process.env.FOO': 'process.env.FOO' }) (that was new to me). So it should be possible to create a lib with webpack which can still be consumed by other bundlers without loosing the feature of setting environment variables.

    The only feature which isn't supported in that way is tree shaking, which brings me back to the original feature request.

  7. nadavsinai commented on Jan 19, 2017

    @nadavsinai

    @donaldpipowitch thanks for bringing this up, very useful indeed, I would love to contribute too, I am going to try to learn the current code base first...

  8. nathanosdev commented on May 10, 2017

    @nathanosdev

    Would love to this implemented!

  9. HipsterZipster commented on Sep 27, 2017

    @HipsterZipster

    Have there been any updates to this issue in the past year? Is it being tracked elsewhere?

  10. yansern commented on Sep 28, 2017

    @yansern

    Having to learn both webpack and rollup is no fun! Would love to have this implemented in webpack.

  11. 179 remaining items

  12. WalterWeidner commented on Feb 3, 2023

    @WalterWeidner

    @WalterWeidner Yeah, I see your problem, we generate __webpack_require__.d, and that is prevent us for tree shaking, we should improve it for libraries

    How likely is this to be implemented? We might need to adjust our build to use something else if this is unlikely to happen.

  13. 10Derozan commented on Feb 21, 2024

    @10Derozan

    Any updates? I want to use it for libraries, too

  14. tonysamperi commented on Mar 7, 2025

    @tonysamperi

    Seriously? It's been almost 10 years now?
    I used it for a library, but it turned out it wasn't tree-shakeable...
    I found a solution using tsc, which means I'll need to go back to rollup (that I used in older versions!!)..

    Very disappointed.

  15. Kocal commented on Mar 8, 2025

    @Kocal

    @tonysamperi we are waiting for your PR then

  16. tonysamperi commented on Mar 8, 2025

    @tonysamperi

    @tonysamperi we are waiting for your PR then

    @Kocal your reply is simply disappointing as well.
    If you really need a hand to understand how to tackle this I'm happy to help.
    Sarcasm won't solve this problem.
    If the main contributors can write up a document explaining what is blocking this (I assume there's something besides the non esm dependencies)...
    Maybe the readers of this issue can help find solutions...

  17. goloveychuk commented on Mar 8, 2025

    @goloveychuk

    Tldr use esbuild or rollup.
    Long version.
    To make tree shaking works, bundle should generate flat output. Which means that all imported functions or variables should be rendered in top level of output module.
    This is what esbuild and rolldown outputs (and probably rollup, I haven't check it personally).
    Webpack, instead, generates registry of modules. Every module (file) is registered in big hashmap, all imports are done via webpack runtime calls.
    Obviously, this could not be treeshaked.
    This is opinionated decision sokra made. It has pros and cons.
    E.g debugging is simpler because all module local members are scoped in function. Other one is that main chunk could provide common modules for async chunks via registry. Esbuild instead will generate additional chunk for commons.
    But the point is that it's a core principle of how major of webpack works. This affects chunks generation e.g.
    Thats why (as I see it) esm is still an issue. But I wouldn't expect it will be fixed in webpack anytime. Especially after sokra (afaik) switched developing turbopack.
    Generally I would look on it as it's out of webpack scope (because, it's main target is spa prod bundle, not libs).
    This is my imho.

  18. tonysamperi commented on Mar 9, 2025

    @tonysamperi

    hey @goloveychuk,

    first of all thank you for a such detailed explanation.
    I think I understand, but at this point I would close the issue, explaining this cannot be achieved, as it goes against the purposes of webpack itself.
    For what concerns my library, I'll explore esbuild (I'm intrigued by performances), or go back to rollup.

    Cheers

  19. alexander-akait commented on Mar 10, 2025

    @alexander-akait
    Member

    @goloveychuk

    Thats why (as I see it) esm is still an issue.

    For the most part we support it, but we don't generate "treeshaked output". This is not our primary goal at the moment, although it may well be realized in the future.

  20. goloveychuk commented on Apr 29, 2025

    @goloveychuk

    @alexander-akait btw I see https://github.com/webpack/webpack/releases/tag/v5.93.0
    modern-module option is added. Isn't it suppose to fix tree shaking issue?

  21. alexander-akait commented on Apr 29, 2025

    @alexander-akait
    Member

    @goloveychuk treeshaked output is working for modern (modern-module is an alias) too right now, but you should have ECMA modules source code

  22. sgarfinkel commented on Aug 15, 2025

    @sgarfinkel

    It doesn't fully work. asset/resource still persistently emits the __webpack_require__ iife which is not tree-shakeable.

  23. alexander-akait commented on Aug 17, 2025

    @alexander-akait
    Member

    @sgarfinkel Weird, can you provide reproducible test repo?

  24. alexander-akait commented on Sep 1, 2025

    @alexander-akait
    Member

    Let's close because we already support ES modules output, we have an issue to improve and make some things better - #17121

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

    help wantedMaintainers welcome a community PR for this

    Type

    No type

    Fields

    Priority

    None yet

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions