Skip to content

Commit

Permalink
fix(enhanced): container addInclude wrap in make hook (#3002)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScriptedAlchemy committed Sep 27, 2024
1 parent 39bfbb4 commit 1b6bf0e
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 103 deletions.
5 changes: 5 additions & 0 deletions .changeset/ai-hungry-tiger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@module-federation/enhanced": patch
---

ContainerPlugin to use makeHook to addInclude of federation runtime dependency
5 changes: 3 additions & 2 deletions ai-lint-fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ async function lintFileContent(fileContent) {
RULES:
-Should preserve uses of normalizeWebpackPath
-Should preserve uses of ts-ignore
-Removed commented out code
-Should improve the source code while ensuing its logic is preserved and functionality is not altered
-Update existing comments for accuracy
-Return only the updated file content with no other response text:
${fileContent}`;

const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: prompt }],
max_tokens: 4096,
max_completion_tokens: 4096,
});

let res = response.choices[0].message.content.trim().split('\n');
Expand Down
4 changes: 2 additions & 2 deletions packages/enhanced/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"parallel": false,
"commands": [
{
"command": "node packages/enhanced/test/script.js",
"command": "node --expose-gc --max-old-space-size=4096 --experimental-vm-modules --trace-deprecation ./node_modules/jest-cli/bin/jest --logHeapUsage --config packages/enhanced/jest.config.ts --silent",
"forwardAllArgs": false
}
]
Expand All @@ -53,7 +53,7 @@
"parallel": false,
"commands": [
{
"command": "node packages/enhanced/test/script-experiments.js",
"command": "node --expose-gc --max-old-space-size=4096 --experimental-vm-modules --trace-deprecation ./node_modules/jest-cli/bin/jest --logHeapUsage --config packages/enhanced/jest.embed.ts --silent",
"forwardAllArgs": false
}
]
Expand Down
140 changes: 70 additions & 70 deletions packages/enhanced/src/lib/container/ContainerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,15 @@ class ContainerPlugin {

compiler.hooks.make.tapAsync(
PLUGIN_NAME,
(
async (
compilation: Compilation,
callback: (error?: WebpackError | null | undefined) => void,
) => {
const hasSingleRuntimeChunk =
compilation.options?.optimization?.runtimeChunk;
const hooks = FederationModulesPlugin.getCompilationHooks(compilation);
const federationRuntimeDependency =
federationRuntimePluginInstance.getDependency(compiler);
const dep = new ContainerEntryDependency(
name,
//@ts-ignore
Expand All @@ -206,78 +208,96 @@ class ContainerPlugin {
this._options.experiments,
);
dep.loc = { name };
compilation.addEntry(
compilation.options.context || '',
dep,
{
name,
filename,
runtime: hasSingleRuntimeChunk ? false : runtime,
library,
},
(error: WebpackError | null | undefined) => {
if (error) return callback(error);
hooks.addContainerEntryModule.call(dep);
callback();
},
);

await new Promise((resolve, reject) => {
compilation.addEntry(
compilation.options.context || '',
dep,
{
name,
filename,
runtime: hasSingleRuntimeChunk ? false : runtime,
library,
},
(error: WebpackError | null | undefined) => {
if (error) return reject(error);
hooks.addContainerEntryModule.call(dep);
resolve(undefined);
},
);
}).catch(callback);

await new Promise((resolve, reject) => {
compilation.addInclude(
compiler.context,
federationRuntimeDependency,
{ name: undefined },
(err, module) => {
if (err) {
return reject(err);
}
hooks.addFederationRuntimeModule.call(
federationRuntimeDependency,
);
resolve(undefined);
},
);
}).catch(callback);

callback();
},
);

// this will still be copied into child compiler, so it needs a check to avoid running hook on child
// we have to use finishMake in order to check the entries created and see if there are multiple runtime chunks
compiler.hooks.finishMake.tapAsync(
PLUGIN_NAME,
async (compilation, callback) => {
// its a child compiler
(compilation: Compilation, callback) => {
if (
compilation.compiler.parentCompilation &&
compilation.compiler.parentCompilation !== compilation
) {
// dont include dependencies on child compilations
return callback();
}

const hooks = FederationModulesPlugin.getCompilationHooks(compilation);
const createdRuntimes = new Set();
const createdRuntimes = new Set<string>();

for (const entry of compilation.entries.values()) {
if (entry.options.runtime) {
if (createdRuntimes.has(entry.options.runtime)) {
continue;
}
createdRuntimes.add(entry.options.runtime);
const runtime = entry.options.runtime;
if (runtime) {
createdRuntimes.add(runtime);
}
}

// if it has multiple runtime chunks - make another with no name or runtime assigned
if (
createdRuntimes.size !== 0 ||
compilation.options?.optimization?.runtimeChunk
createdRuntimes.size === 0 &&
!compilation.options?.optimization?.runtimeChunk
) {
const dep = new ContainerEntryDependency(
name,
//@ts-ignore
exposes,
shareScope,
federationRuntimePluginInstance.entryFilePath,
this._options.experiments,
);
return callback();
}

const dep = new ContainerEntryDependency(
name,
//@ts-ignore
exposes,
shareScope,
federationRuntimePluginInstance.entryFilePath,
this._options.experiments,
);

dep.loc = { name };
dep.loc = { name };

compilation.addInclude(
compilation.options.context || '',
dep,
{ name: undefined },
(error: WebpackError | null | undefined) => {
if (error) return callback(error);
hooks.addContainerEntryModule.call(dep);
callback();
},
);
} else {
callback();
}
compilation.addInclude(
compilation.options.context || '',
dep,
{ name: undefined },
(error: WebpackError | null | undefined) => {
if (error) return callback(error);
hooks.addContainerEntryModule.call(dep);
callback();
},
);
},
);

Expand All @@ -301,11 +321,6 @@ class ContainerPlugin {
compiler.hooks.thisCompilation.tap(
PLUGIN_NAME,
(compilation: Compilation, { normalModuleFactory }) => {
const federationRuntimeDependency =
federationRuntimePluginInstance.getDependency(compiler);

const logger = compilation.getLogger('ContainerPlugin');
const hooks = FederationModulesPlugin.getCompilationHooks(compilation);
compilation.dependencyFactories.set(
FederationRuntimeDependency,
normalModuleFactory,
Expand All @@ -314,21 +329,6 @@ class ContainerPlugin {
FederationRuntimeDependency,
new ModuleDependency.Template(),
);

compilation.addInclude(
compiler.context,
federationRuntimeDependency,
{ name: undefined },
(err, module) => {
if (err) {
return logger.error(
'Error adding federation runtime module:',
err,
);
}
hooks.addFederationRuntimeModule.call(federationRuntimeDependency);
},
);
},
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ class FederationRuntimePlugin {
getDependency(compiler: Compiler) {
if (this.federationRuntimeDependency)
return this.federationRuntimeDependency;

this.ensureFile(compiler);

this.federationRuntimeDependency = new FederationRuntimeDependency(
this.getFilePath(compiler),
);
Expand Down
1 change: 0 additions & 1 deletion packages/enhanced/test/ConfigTestCases.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const categories = fs.readdirSync(casesPath).map((cat) => {
.sort(),
};
});
console.log(333, categories);
// .filter((i) => i.name === 'container');
const createLogger = (appendTarget) => {
return {
Expand Down
14 changes: 0 additions & 14 deletions packages/enhanced/test/script-experiments.js

This file was deleted.

14 changes: 0 additions & 14 deletions packages/enhanced/test/script.js

This file was deleted.

0 comments on commit 1b6bf0e

Please sign in to comment.