Support code-splitting for UMD builds #3490
Comments
|
The reason Webpack can do it, is because they have their own runtime for loading stuff. Rollup supports import 'unfetch'
import s from "systemjs/dist/s"Build that as I am actually happy that Rollup doesn't have it's own runtime for that, because it has an overhead not to mention more than 88% of world wide browsers actually support native ES Modules, even more if you disregard dynamic import. Something else to keep in mind Webpack overhead increases per modules, it could have been changed by now but last I checked it was not negligible. You're better off with SystemJS. |
|
I think the runtime to support this could be rather small. code-splitting UMD would just require another UMD wrapper for the split file. I've written a small plugin for my use-case, that does exactly this. Sometimes you cannot use SystemJS, as you are locked in by the ecosystem to use something else. Also the "modern browser" argument doesn't hold for production, as in a business environment IE and the old Edge still (sadly) dominate. If rollup would support code-splitting in these scenarios, you won't really need webpack anymore... |
You need to load them somehow, which means its going to need polyfills for e.g
Hence the suggestion of
Right that's a good point, but the way I understand it, and I could be wrong, the only way UMD actually works out of the box is if it defaults to Another alternative is AMD but I suspect it has a larger footprint and requires a bigger loader. I tackled this issue earlier, so I thought I'd offer an alternative. |
|
Rollup could at least support code-splitting on a named module basis. I wrote a plugin that replaces a dynamic import call with an UMD-factory for named modules: /**
* Replaces named, dynamic imports with an UMD factory. Absolute and relative imports are ignored.
*
* Requires an environment that supports `Promise`.
*/
export default function pluginDynamicImports (options = {}) {
return {
name: 'dynamic-imports',
transform (code, filename) {
// TODO: warn/error if globalName is not specified in UMD environment.
// TODO: global is hardcoded to window, which is wrong.
const transformedCode = code.replace(/import\(['"`](?![./])(.*?)['"`]\)/gi, (match, request) => {
const globalName = options.globals[request]
return `new Promise(function (resolve, reject) {
(function (global) {
typeof exports === 'object' && typeof module !== 'undefined' ? resolve(require("${request}")) :
typeof define === 'function' && define.amd ? require(["${request}"], resolve, reject) :
(global = global || self, resolve(global["${globalName}"]));
}(window));
})`
})
return transformedCode
},
}
}It assumes that the imported module has an alias set in the This works for my use cases, but surely can be improved. It allows me to use code like this without any issues:
|
|
@johannes-z Out of curiosity, do you know if there are any benefits with The only thing I can tell is that
Were you under the impression that lazy loading wouldn't work unless you used UMD? |
|
I'm not 100% sure, but in production I only had problems using
Yes I'm aware of that: I'd actually only use So my point is: Support lazy loading where it is supported (AMD), but not where it doesn't have to be (CommonJS, IIFE). Rollup breaks the build though, which isn't a necessity as this can be circumvented with the plugin I just posted. Rollup could do the same, but warn the user instead of erroring. |
|
There is a similar issue #2072 which was closed a while back. I would also like this to be supported by Rollup out of the box, maybe this could be some config of some sort (like turn on rollup loader if needed). |
|
A workaround that may work for some babel+rollup users in the meantime, a babel plugin that can translate some dynamic imports to static imports to allow rollup to generate a UMD build. |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Feature Use Case
UMD builds should be able to code-split. Webpack can create code-splitting bundles even in UMD.
Webpack example:
Input
main.js
other.js
Compiled
main.js
other.js
Feature Proposal
rollup should support UMD and code-splitting. The only problem I see here is IIFE bundles. CommonJS and AMD are already supported.
I think webpack can code-split UMD bundles because of their runtime (please correct me if I'm wrong).
In order for rollup to support UMD code-splitting, the split IIFE files have to be loaded and executed somehow (maybe a runtime-helper?). Also I guess all chunks need a UMD factory also.
The text was updated successfully, but these errors were encountered: