Skip to content

Commit a84dc1f

Browse files
Merge pull request #27 from module-federation/fix_build_order
2 parents ef6bf02 + 3d0a9c8 commit a84dc1f

File tree

3 files changed

+65
-65
lines changed

3 files changed

+65
-65
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ withFederatedSidecar(
160160
},
161161
},
162162
{
163+
ssr: true, // if you want to disable the server runtimes, set to false. This will mean client side only.
163164
removePlugins: [
164165
// optional
165166
// these are the defaults
@@ -200,17 +201,18 @@ const remotes = (isServer) => {
200201
};
201202

202203
const nextConfig = {
203-
// your original next.config.js export
204-
// we attach next internals to share scope at runtime
204+
// your original next.config.js export
205+
// we attach next internals to share scope at runtime
205206

206-
webpack(config, options) {
207-
const { webpack, isServer } = options;
208-
config.module.rules.push({
209-
test: [/_app.[jt]sx?/, /_document.[jt]sx?/],
210-
loader: "@module-federation/nextjs-ssr/lib/federation-loader.js",
211-
});
207+
webpack(config, options) {
208+
const {webpack, isServer} = options;
209+
config.module.rules.push({
210+
test: [/_app.[jt]sx?/, /_document.[jt]sx?/],
211+
loader: "@module-federation/nextjs-ssr/lib/federation-loader.js",
212+
});
212213

213-
return config;
214+
return config;
215+
}
214216
};
215217

216218
module.exports = withPlugins(

lib/with-federated-sidecar.js

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ const withModuleFederation =
181181
experiments = {
182182
flushChunks: false,
183183
},
184+
ssr = true,
184185
removePlugins = [
185186
"BuildManifestPlugin",
186187
"DropClientPage",
@@ -391,44 +392,38 @@ const withModuleFederation =
391392
log.warning("warning:");
392393
log.warning(info.warnings);
393394
}
394-
const { outputPath, assets, name, chunks } = info;
395+
const { chunks } = info;
396+
const distPath = path.join(
397+
compilation.options.context,
398+
nextConfig.distDir
399+
);
400+
const hasStats = fs.existsSync(
401+
path.join(distPath, "static/federated-stats.json")
402+
);
403+
const hasRemote = fs.existsSync(
404+
path.join(distPath, "static/ssr/remoteEntry.js")
405+
);
406+
if (hasRemote && hasStats && experiments.flushChunks) {
407+
let remoteEntry = path.join(
408+
distPath,
409+
'static/ssr/remoteEntry.js'
410+
);
411+
395412

396-
if (name === "federated-server" && experiments.flushChunks) {
397-
const remoteEntry = assets.find((asset) => {
398-
if (!asset && !asset.name) {
399-
return false;
400-
}
401-
return asset.name.includes("remoteEntry");
402-
});
403413
const statContent = require(path.join(
404-
outputPath,
405-
"../federated-stats.json"
414+
distPath,
415+
"static/federated-stats.json"
406416
));
407417

408418
let loadableContent;
409419
if (
410420
fs.existsSync(
411-
path.join(
412-
compilation.options.output.path,
413-
"../../federated-loadable-manifest.json"
414-
)
421+
path.join(distPath, "federated-loadable-manifest.json")
415422
)
416423
) {
417424
loadableContent = require(path.join(
418-
compilation.options.output.path,
419-
"../../federated-loadable-manifest.json"
420-
));
421-
} else if (
422-
fs.existsSync(
423-
path.join(
424-
compilation.options.output.path,
425-
"../federated-loadable-manifest.json"
426-
)
427-
)
428-
) {
429-
loadableContent = require(path.join(
430-
compilation.options.output.path,
431-
"../federated-loadable-manifest.json"
425+
distPath,
426+
"federated-loadable-manifest.json"
432427
));
433428
} else {
434429
log(
@@ -439,17 +434,13 @@ const withModuleFederation =
439434
statContent.loadable = loadableContent;
440435

441436
const remoteContentPromise = new Promise((resolve, reject) => {
442-
fs.readFile(
443-
path.join(outputPath, remoteEntry.name),
444-
"utf-8",
445-
function (err, data) {
446-
if (err) {
447-
reject(err);
448-
complete();
449-
}
450-
resolve(data);
437+
fs.readFile(remoteEntry, "utf-8", function (err, data) {
438+
if (err) {
439+
reject(err);
440+
complete();
451441
}
452-
);
442+
resolve(data);
443+
});
453444
});
454445

455446
const collectedChunkHash = chunks
@@ -469,21 +460,20 @@ const withModuleFederation =
469460
`init: () => (init), chunkMap: () => (${JSON.stringify(
470461
statContent
471462
)}), hash: ()=>(${JSON.stringify(hash)})`
463+
472464
);
465+
473466
process.nextTick(() => {
474-
fs.writeFile(
475-
path.join(outputPath, remoteEntry.name),
476-
newSource,
477-
(err) => {
478-
log("emitted!");
479-
if (typeof done === "function") {
480-
complete();
481-
pending.then(() => {
482-
done(err);
483-
});
484-
}
467+
fs.rmSync(remoteEntry);
468+
fs.writeFile(remoteEntry, newSource, (err) => {
469+
log("emitted!");
470+
if (typeof done === "function") {
471+
complete();
472+
pending.then(() => {
473+
done(err);
474+
});
485475
}
486-
);
476+
});
487477
});
488478
});
489479
} else {
@@ -517,6 +507,14 @@ const withModuleFederation =
517507
* @returns {import("webpack").Configuration}
518508
*/
519509
webpack(config, options) {
510+
if(!ssr && options.isServer) {
511+
log('SSR Disabled')
512+
if (typeof nextConfig.webpack === "function") {
513+
return nextConfig.webpack(config, options);
514+
}
515+
516+
return config;
517+
}
520518
const { webpack } = options;
521519
Object.assign(config.experiments, {
522520
topLevelAwait: true,
@@ -641,17 +639,17 @@ const withModuleFederation =
641639

642640
// in production or on server build use tapAsync to wait for the full compilation of the sidecar
643641
if (isProd || compiler.options.mode === "production") {
644-
// if (
645-
// compiler.options.name === "server" ||
646-
// compiler.options.name === "client"
647-
// ) {
642+
if (
643+
compiler.options.name === "server" ||
644+
compiler.options.name === "client"
645+
) {
648646
compiler.hooks.afterCompile.tapAsync(
649647
"NextFederation",
650648
(compilation, done) => {
651649
run(compilation, done);
652650
}
653651
);
654-
// }
652+
}
655653
} else {
656654
compiler.hooks.afterCompile.tap(
657655
"NextFederation",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"public": true,
33
"name": "@module-federation/nextjs-ssr",
4-
"version": "0.1.0-rc.8",
4+
"version": "0.2.0-rc.0",
55
"description": "Module Federation CSR & SSR Support for Next.js",
66
"main": "bundle.js",
77
"types": "index.d.ts",

0 commit comments

Comments
 (0)