Skip to content

Commit

Permalink
Revert "chore(enhanced): adjust add federation init process (#2035)" (#…
Browse files Browse the repository at this point in the history
…2271)

Co-authored-by: ScriptedAlchemy <zackaryjackson@bytedance.com>
  • Loading branch information
ScriptedAlchemy and ScriptedAlchemy authored Apr 9, 2024
1 parent 32eba3c commit 6b3b210
Show file tree
Hide file tree
Showing 32 changed files with 423 additions and 638 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-timers-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/runtime': patch
---

Add Register plugins api
5 changes: 5 additions & 0 deletions .changeset/shaggy-turtles-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/enhanced': patch
---

revert chunk integration
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import png from '../../public/webpack.png';

export default function WebpackPng() {
return <img className="remote1-webpack-png" src={png} alt="webpack png" />;
return (
<div>
<img className="remote1-webpack-png" src={png} alt="webpack png" />
</div>
);
}
1 change: 1 addition & 0 deletions apps/modernjs/modern.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default defineConfig({
appendPlugins([
new AsyncBoundaryPlugin({
excludeChunk: chunk => chunk.name === 'app1',
eager: module => /\.federation/.test(module?.request || ''),
}),
new ModuleFederationPlugin({
name: 'app1',
Expand Down
8 changes: 4 additions & 4 deletions apps/modernjs/runtimePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
console.log(React);
console.log('runtime plugins active');
export default function () {
return {
name: 'eager-react-test-plugin',
init(args) {
async init(args) {
return args;
},
beforeRequest(args) {
async beforeRequest(args) {
// const React = await import('react');
// console.log(React);
return args;
},
resolveShare(args) {
Expand Down
24 changes: 0 additions & 24 deletions apps/react-ts-host/@mf-types/index.d.ts

This file was deleted.

This file was deleted.

8 changes: 0 additions & 8 deletions apps/react-ts-host/@mf-types/react_ts_nested_remote/apis.d.ts

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

24 changes: 0 additions & 24 deletions apps/react-ts-nested-remote/@mf-types/index.d.ts

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class ContainerEntryModule extends Module {
// @ts-ignore
new EntryDependency(this._injectRuntimeEntry),
);

callback();
}

Expand Down
67 changes: 16 additions & 51 deletions packages/enhanced/src/lib/container/ContainerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { containerPlugin } from '@module-federation/sdk';
import FederationRuntimePlugin from './runtime/FederationRuntimePlugin';
import checkOptions from '../../schemas/container/ContainerPlugin.check';
import schema from '../../schemas/container/ContainerPlugin';
import HoistContainerReferencesPlugin from './HoistContainerReferencesPlugin';

type ExcludeUndefined<T> = T extends undefined ? never : T;
type NonUndefined<T> = ExcludeUndefined<T>;
Expand Down Expand Up @@ -149,8 +150,6 @@ class ContainerPlugin {

if (!useModuleFederationPlugin) {
ContainerPlugin.patchChunkSplit(compiler, this._options.name);
ContainerPlugin.patchChunkSplit(compiler, 'federation-runtime');
ContainerPlugin.patchChunkSplit(compiler, 'mfp-runtime-plugins');
}
const federationRuntimePluginInstance = new FederationRuntimePlugin();
federationRuntimePluginInstance.apply(compiler);
Expand All @@ -166,16 +165,6 @@ class ContainerPlugin {
) {
compiler.options.output.enabledLibraryTypes.push(library.type);
}
const hasSingleRuntimeChunk = compiler.options?.optimization?.runtimeChunk;

new compiler.webpack.EntryPlugin(
compiler.options.context || '',
federationRuntimePluginInstance.entryFilePath,
{
name,
runtime: hasSingleRuntimeChunk ? false : runtime,
},
).apply(compiler);

compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation, callback) => {
const dep = new ContainerEntryDependency(
Expand Down Expand Up @@ -211,45 +200,21 @@ class ContainerPlugin {
);

// Function to add entry for undefined runtime
const addEntryToSingleRuntimeChunk = async () => {
const entries =
typeof compiler.options.entry === 'function'
? await compiler.options.entry()
: compiler.options.entry;
const runtimes: Set<undefined | string | false> = new Set();

Object.keys(entries).forEach((key) => {
if (entries[key].runtime) {
runtimes.add(entries[key].runtime);
} else if (entries[key].runtime === undefined) {
runtimes.add(undefined);
}
});

//Add container entry for each runtime that exists
for (const runtime of runtimes) {
const name = runtime
? 'federation-runtime-' + runtime
: 'federation-runtime';
await new Promise((resolve, reject) => {
compilation.addEntry(
compilation.options.context || '',
//@ts-ignore
dep,
{
name: name, // merge container into federation entrypoint added to compilation
runtime: runtime,
library,
},
(error: WebpackError | null | undefined) => {
if (error) return reject(error);
resolve(true);
},
);
}).catch(callback);
}

callback();
const addEntryToSingleRuntimeChunk = () => {
compilation.addEntry(
compilation.options.context || '',
//@ts-ignore
dep,
{
name: name ? name + '_partial' : undefined, // give unique name name
runtime: undefined,
library,
},
(error: WebpackError | null | undefined) => {
if (error) return callback(error);
callback();
},
);
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { containerReferencePlugin } from '@module-federation/sdk';
import FederationRuntimePlugin from './runtime/FederationRuntimePlugin';
import schema from '../../schemas/container/ContainerReferencePlugin';
import checkOptions from '../../schemas/container/ContainerReferencePlugin.check';
import HoistContainerReferencesPlugin from './HoistContainerReferencesPlugin';

const { ExternalsPlugin } = require(
normalizeWebpackPath('webpack'),
Expand Down Expand Up @@ -74,7 +75,6 @@ class ContainerReferencePlugin {
apply(compiler: Compiler): void {
const { _remotes: remotes, _remoteType: remoteType } = this;
new FederationRuntimePlugin().apply(compiler);

/** @type {Record<string, string>} */
const remoteExternals: Record<string, string> = {};

Expand Down
Loading

0 comments on commit 6b3b210

Please sign in to comment.