Skip to content

libraryTarget UMD and code splitting #6818

@AndyOGo

Description

@AndyOGo

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

When splitting code in webpack, the chunks are loaded with temporary script tags:

/******/    var head = document.getElementsByTagName('head')[0];
/******/    var script = document.createElement('script');
/******/    script.type = 'text/javascript';
/******/    script.charset = 'utf-8';
/******/    script.async = true;
/******/    script.timeout = 120000;

/******/    script.src = __webpack_require__.p + "" + ({}[chunkId]||chunkId) + "." + "<some_key>" + ".js";

What is the expected behavior?

All chunks are wrapped within UMD blocks.

If this is a feature request, what is motivation or use case for changing the behavior?

When creating an umd library, the library might be loaded by another bundler again (webpack, jspm, etc.). In that case, the script tag loading isn't nice. It would be better to keep the AMD (or System.import for webpack 2) in the chunks which will be interpreted by the other bundler. I didn't find an option to enable this. Is it a missing feature? Or am I missing something and code splitting in umd libraries not possible?

Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.

Reopens #2471

Activity

AndyOGo

AndyOGo commented on Mar 27, 2018

@AndyOGo
Author

@sokra
Did you have any chance to look into this?
Your feedback would be very much appreciated, especially since the last issue just got lost and automatically closed...

sokra

sokra commented on Mar 29, 2018

@sokra
Member

This is not supported yet.

AndyOGo

AndyOGo commented on Mar 29, 2018

@AndyOGo
Author

@sokra
Thanks for your reply.
Yes I know that, hence the issue.

mspoerer

mspoerer commented on May 16, 2018

@mspoerer

+1

theiliad

theiliad commented on Sep 21, 2018

@theiliad

Any updates on this?

thebestnom

thebestnom commented on Oct 17, 2018

@thebestnom

I would really like it too, any updates on it?

bstst

bstst commented on Apr 26, 2019

@bstst

Was investigating this right now. And can't see this happening any time soon.

You library may be imported in to different projects with different asset delivery pipelines, and to deliver an async chunk your library needs to know where to look for it, a generic publicPath: '/assets/ won't simply work here. And for you library to know where to look for a chunk — there needs to be a generic way to pass this information to it. And there is no such generic mechanism.

egucciar

egucciar commented on May 6, 2019

@egucciar

Yes, this would be awesome to fix. For now I'll continue to use rollup as this blocks me from using webpack for my bundle-split library.

MrBr

MrBr commented on Oct 7, 2019

@MrBr

I've built a Vendor injection plugin that solves this issue. I am actually not very familiar with webpack and I've improvised a lot to get it working. It works with CRA (create React application) application webpack config, but I guess it shouldn't be too hard to adapt it to non-CRA projects.

The current implementation is used solely for development. Useful in a monorepo structure, should speed up build time and help reduce the source map size of internal packages (libraries). Build time should improve in a case when working on multiple packages at the same time.

The idea is to split a library bundle into 2, vendors (modules) and the source. Making changes in a package source should build only the source bundle (as long as dependencies aren't changed).

For bundle caching add HardSourceWebpackPlugin. Both plugins work with CSS as well.

VendorsInjectionPlugin gist

Anyhow, I would really appreciate feedback and improvements suggestion as I believe it's not written in best webpack fashion.

ghost

ghost commented on Feb 25, 2020

@ghost

Would love to see something like this. I have a library that would greatly benefit from code-splitting.

webpack-bot

webpack-bot commented on Jan 16, 2021

@webpack-bot
Contributor

This issue had no activity for at least three months.

It's subject to automatic issue closing if there is no activity in the next 15 days.

AndyOGo

AndyOGo commented on Jan 16, 2021

@AndyOGo
Author

still an issue @webpack-bot

bdsanfelippo

bdsanfelippo commented on Jun 3, 2021

@bdsanfelippo

Does anyone have any good workarounds for this issue? Had a library that was umd. Library became too large, so split it into chunks but would still like to load it in similar fashion as before.

thebestnom

thebestnom commented on Jun 4, 2021

@thebestnom

@bdsanfelippo in the end I just wrote and transpiled directly with babel...

sedghi

sedghi commented on Aug 15, 2022

@sedghi

Is there any update for this?

tsi

tsi commented on Dec 26, 2022

@tsi

@AndyOGo @sokra is this really specific to type: 'umd'? will this be any different with other types?

As the library author, is there any property I can specify in my package.json that will tell the consuming bundlers to copy the chunks and put them beside the compiled output?

karanrainafynd

karanrainafynd commented on Apr 12, 2023

@karanrainafynd

Any updates on this issue? Does anyone have a workaround ?

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @tsi@bstst@AndyOGo@sokra@egucciar

        Issue actions

          libraryTarget UMD and code splitting · Issue #6818 · webpack/webpack