<ul dir="auto">
<li>Store cache to file</li>
<li>Chunk merging</li>
<li>Loaded chunks detection</li>
<li>Duplicate code detection</li>
<li>Export packaged code to other module systems</li>
<li>Typescript loader</li>
<li>SourceMaps</li>
<li>Documentation</li>
<li>more?</li>
</ul>
<p dir="auto"><a href="https://github.com/webpack/webpack/wiki/Ideas">wiki Ideas</a></p>
<p dir="auto">This is a proposal to merge my existing work into the Webpack core. The base concept is federated application orchestration at runtime. What I hope will provide a new era for how we build apps at scale.</p>
<p dir="auto"><strong>Due to the demand for more information, we have created a site that consolidates all content</strong></p>
<p dir="auto"><a href="https://module-federation.github.io/" rel="nofollow">https://module-federation.github.io/</a></p>
<p dir="auto"><a href="https://github.com/module-federation/module-federation-examples">https://github.com/module-federation/module-federation-examples</a></p>
<p dir="auto"><a href="https://github.com/webpack/changelog-v5/blob/master/guides/module-federation.md">https://github.com/webpack/changelog-v5/blob/master/guides/module-federation.md</a></p>
<h2 dir="auto">Feature request</h2>
<p dir="auto"><a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/sokra/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/sokra">@sokra</a> as requested, I've opened a new issue for us to discuss planning and implementation.</p>
<p dir="auto">This is the evolution of my original issue: <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="392906686" data-permission-text="Title is private" data-url="https://github.com/webpack/webpack/issues/8524" data-hovercard-type="issue" data-hovercard-url="/webpack/webpack/issues/8524/hovercard" href="https://github.com/webpack/webpack/issues/8524">#8524</a><br>
edit: a better way to explain the request. I want the webpack equivalent of Apollos federation concept.<br>
I want to share resources between separate bundles at runtime. Similar to DLL plugin but without needing to provide Static build context , and I want to do this at runtime in the browser. Not at build time. Thus enabling tech like micro-frontends to be efficient, easy to manage, and most importantly - erase page reloads between separate apps and Enable independent deploys of standalone applications.</p>
<p dir="auto">For context, I've already implemented this feature request with great success. I feel this would have a major impact on frontend applications. I am proposing to refactor/rewrite my project and introduce it into the Webpack core. <a href="https://github.com/ScriptedAlchemy/webpack-external-import">https://github.com/ScriptedAlchemy/webpack-external-import</a></p>
<p dir="auto"><strong>What is the expected behavior?</strong><br>
<code class="notranslate">import('website-one/MegaNav')</code><br>
<code class="notranslate">require('website-one/MegaNav')</code></p>
<ul dir="auto">
<li>I expect to be able to require modules or chunks from other Webpack bundles hosted elsewhere.</li>
<li>I don't want to manage externals and worry about synchronizing them across systems</li>
<li>I don't want a single point of failure, like a commons chunk</li>
<li>I want to load code from another build, like dynamic imports and code-splitting</li>
<li>I want multiple builds to look and feel like a monolith in the client</li>
<li>I am able to deploy frontend apps independently and expect Webpack to orchestrate at runtime</li>
<li>I don't want to use less integrated/framework oriented solutions like SingleSPA, browser events, service workers - I want to use Webpack as the host container for foreign chunks and modules, not load other app entry points, but rather load other apps <em>chunks</em> and use those modules</li>
<li>If desired, an entire company should be able to federate code across every UI, from user-facing to backend. Effectively turning a multi build, multi team, multi mono-repo company into one SPA in the browser. Removing the need for page reloads or downloading additional code and bundles containing mostly the same node modules</li>
</ul>
<p dir="auto"><strong>What is motivation or use case for adding/changing the behavior?</strong><br>
This would offer a major shift in frontend architecture.<br>
Apps are getting larger and code-splitting on its own is not as effective with things such as micro-frontend technology.</p>
<p dir="auto">All current solutions on the market are substandard - working around the problem instead of addressing it. Application interleaving at runtime enables many new avenues for engineering.</p>
<p dir="auto">Heres some:</p>
<ul dir="auto">
<li>Applications can be self-healing, in the event there is a network failure - one could query other interleaved apps and load their copy of the missing dependency. This would solve deploy issues where pages break for a moment as the new code replaced old</li>
<li>Code can be distributed and managed by the team maintaining it, with options for evergreen code. Navigation, Pages, Routers - could be managed and deployed by the team who owns it - others would not have to redeploy their applications to get updates</li>
<li>Smaller bundles - interleaved apps wouldn't download chunks if Webpack already has the modules required by the interleaved app or fragment.</li>
<li>Easy and feasible micro-frontends - the tech right now involves some serious management and time to build supporting infrastructure</li>
<li>Shareable configs - interleave FE configurations without the need to rebundle or expose multiple configs.</li>
<li>Avoid long &amp; slow builds due to the size of the monorepo. This would enable smaller builds and faster deploys with less coordination across teams</li>
<li>Introduction of "micro-service functions" modeling something similar to what backend engineers get to enjoy.</li>
<li>Better AB testing and marketing tooling. Right now AB tests are managed via a tag manager, plastering vanilla JS onto the dom which is usually not optimized, pollifilled, or compatible with how much javascript apps are written today. Markering teams and AB teams would write native components without having to get in the way of team delivery.</li>
<li>Better analytics - Tag managers and analytics engineers could write system native modules that could be woven into codebases. Most depend on some plugin or just reading the DOM. Interleaving would open a new world of slick integration.</li>
<li>third party modules - vendors could take advantage of this tech and offer managed modules instead of inefficient scripts - they'd still be secure but would have a new way of implementation. Iframes could be avoided in some cases</li>
<li>no global pollution of the window, globals can be accessed via the Webpack runtime</li>
<li>as more apps are interleaved, they too can supply their modules to another interleaved app, resulting in less and less code needing to be downloaded as more and more modules are added to webpack</li>
</ul>
<p dir="auto">The use cases are limitless, I've only begun to imagine possibilities.</p>
<p dir="auto">Webpack is more than capable of doing this without needing any big changes to the core. Both 4 and 5 already have what I need.</p>
<p dir="auto"><strong>How should this be implemented in your opinion?</strong><br>
Seeing as I've already done it. I have a clear picture of how to implement it. However, it can be improved with some guidance. I hold little opinion over the solution, as long as it offers a similar outcome.</p>
<ol dir="auto">
<li>Add an additional require extension which can support interleaving. I'm currently using a slightly modified version of <code class="notranslate">requireEnsure</code> which already works very well with Webpacks chunk and module loading / caching. when requiring an interleaved module - users would specify a pattern like /&lt;module.id&gt; - Because my current solution is based on requireEnsure, it's pretty robust and not hacky. To handle CSS I've also taken the code template from mini-css to support side-effect loading.</li>
<li>Output an unhashed JS file that can be embedded onto other apps. This file would contain a small mapping of <code class="notranslate">chunk.id</code> to the cache busted file name.</li>
<li>Hashed module ids based on contents + package.json version of dependency + usedExports to support tree-shaken code. Hashing needs to be effective at avoiding collisions as well as to support tree-shaken code. Hashing enables effective code sharing and prevents overwriting existing modules. Ideally, if there's an option to update module id after optimization - it would likely improve the reliability of hashing. However, I have not encountered any problems. Open to alternative solutions. This hashing mechanism I am using is used by amazon who built a CLI to orchestrate code sharing across the company.</li>
<li>A slight addition to the chunk template to include some registration data or another function similar to webpackJsonp.push which registers chunks and provides some metadata to Webpack<br>
Heres what it would include:</li>
</ol>
<ul dir="auto">
<li>compilation hash: to detect if this chunk is being loaded by the build that created it, if so, no additional processing is needed</li>
<li>an array of additional <code class="notranslate">chunk.id</code>'s required by the current chunk</li>
<li>a list of module ids which allow Webpack to determine if it has all modules required by the chunk about to be interleaved. Webpack would use a lazy find to search modules for the first instance of a missing module.id. At which point Webpack would load the extra chunk needed to fulfill any missing modules it does not already have</li>
</ul>
<ol start="5" dir="auto">
<li>an interface similar to externals, where a developer can specify what files/modules they intend to make available for interleaving. During the build, Webpack would not hash these module.ids, but instead, make the id be whatever the key is. Allowing humans to call the module by a predictable name. All dependencies of a module and all modules, in general, would be hashed - ensuring that nested dependencies can be tree shaken and will only use existing modules in Webpack if they are an exact match.</li>
<li>Some automatic chunk splitting which would ensure interleaved files are placed into their own cache group. I also recommend chunking vendor bundles and any loader JS code out of the main chunk in order to avoid interleaving the entry point because of something like css-loader getting grouped into the main chunk. In order to prevent downloading a massive 200kb chunk, I am suggesting that there be some default limit for maxSize set on cache groups. Something along the lines of enabling aggressive code-splitting by default if interleaving is enabled. The goal is a happy medium between not downloading dozens of chunks and not downloading a massive chunk for one or two modules the host build cannot supply.</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  &quot;interleave&quot;: {
    &quot;TitleComponent&quot;: &quot;src/components/Title/index.js&quot;,
    &quot;SomeExternalModule&quot;: &quot;src/components/hello-world/index.js&quot;
  }"><pre class="notranslate"><code class="notranslate">  "interleave": {
    "TitleComponent": "src/components/Title/index.js",
    "SomeExternalModule": "src/components/hello-world/index.js"
  }
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  plugins: [
    new WebpackExternalImport({
      manifestName: &quot;website-one&quot;
    })
  ];"><pre class="notranslate"><code class="notranslate">  plugins: [
    new WebpackExternalImport({
      manifestName: "website-one"
    })
  ];
</code></pre></div>
<p dir="auto">Which would be used in consumer apps like this:<br>
<code class="notranslate">require.interleaved('website-one/TitleComponeent')</code></p>
<p dir="auto">For better understanding, I suggest reading the links below. Please note that I am aware of many of the less elegant parts of the codebase and eager to improve the implementation to meet internal standards of Webpack (perf, bundle size, implementation strategy, better hooks to use)</p>
<ul dir="auto">
<li><a href="https://github.com/ScriptedAlchemy/webpack-external-import">https://github.com/ScriptedAlchemy/webpack-external-import</a></li>
<li><a href="https://github.com/ScriptedAlchemy/webpack-external-import/blob/master/src/webpack/interleaveFn.js">https://github.com/ScriptedAlchemy/webpack-external-import/blob/master/src/webpack/interleaveFn.js</a></li>
<li><a href="https://github.com/ScriptedAlchemy/webpack-external-import/blob/master/src/webpack/chunkSplitting.js">https://github.com/ScriptedAlchemy/webpack-external-import/blob/master/src/webpack/chunkSplitting.js</a></li>
<li><a href="https://github.com/ScriptedAlchemy/webpack-external-import/blob/master/src/webpack/optimizeChunk.js">https://github.com/ScriptedAlchemy/webpack-external-import/blob/master/src/webpack/optimizeChunk.js</a></li>
</ul>
<p dir="auto"><strong>Are you willing to work on this yourself?</strong><br>
yes - I have been contributing to Webpack since v1 and have a deep understanding of its API.<br>
I'd like to be the one to bring this feature to the wider JS community as well as to Webpack. I've built multiple large scale MFEs, some consisting of over 100 MFE's at one company. I am very familiar with the challenges and alternative methods to chunk federation/interleaving - all of them are terrible.</p>
<p dir="auto">While this solution might not be for everyone, it will have major value to massive corporations consisting of hundreds of teams of FE stacks. I am also able to test implementation updates against my own userbase - providing additional assurance to Webpack. I used the same approach when merging <code class="notranslate">extract-css-chunks</code> with <code class="notranslate">mini-css</code> (bringing HMR to mini-css)</p>
<p dir="auto"><strong>Current results</strong><br>
I've been using this system for AB tests, Next.js apps, and config sharing at scale.<br>
In a simple comparison. Interleaving three separate React apps produced only 320kb of code transferred (1.2mb uncompressed) compared to 4mb using traditional solutions available on the market.</p>
<p dir="auto"><strong>Extra considerations</strong></p>
<ul dir="auto">
<li>Whitelisting instructions that would enable developers to tell Webpack to use the "host build's" version of a dependency. Cases like React where I can't have two on the page - I want to offer the option to have a chunk use the version already installed, even if its a patch version different.</li>
<li>Hashing bloats builds: I'm looking for an alternative solution to hashing to give the community options, however, the savings of code sharing drastically outweigh any overhead in my opinion. 4mb to 1.2mb speaks for itself.</li>
<li>Chunk manifests are inside the chunk - this could be in one single file to produce a single manifest, however, I like the idea of code splitting chunk needs into the chunk themselves. This would make the actual manifest file very small as all it looks up is chunk names to URL paths. Seeing as the manifest is either cache busted with Date.now or 304 cache control - I wanted to keep it lean in the event developers don't want to deal with cache headers and just cache bust the 2kb file on each page load. This would introduce one extra loading wave, as once the chunk is downloaded, it would specify what it needs, triggering Webpack to then download any additional files in one call. Download chunk-&gt;download any chunks containing modules Webpack host build cannot provide. Nested chunks would not create more loading waves as the chunk metadata contains a thorough map of what it needs in totality</li>
</ul>
<p dir="auto"><strong>SSR</strong><br>
Update: because this is <strong>not only a browser implementation</strong> - you are more than able to federate modules via common js and shared directory or volumes on s3 or some other store that system. Alternatively, one could npm install  a node version of the dependency at CI, treating the federated code (which is bundled so there’s no need for nested npm installs) to install the latest copy at deploy rendering most recent deploy of it - but at runtime the most updated Versions are available.</p>
<p dir="auto">Ultimately- if you use module federation for the node build and were to have a shared disk that a lambda could read from, you’d be able to require other code deployed, in the exact same manner as you do on the frontend.</p>