Skip to content

Commit

Permalink
Add runtime mergeing (module-federation#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScriptedAlchemy authored Oct 25, 2020
1 parent 54627ca commit 5024990
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Module Federation For Next.js
====================================
# Module Federation For Next.js

This plugin enables Module Federation on Next.js

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const withModuleFederation = require("./withModuleFederation");
const patchSharing = require("./patchSharing");

const MergeRuntime = require("./merge-runtime");
module.exports = {
withModuleFederation,
patchSharing,
MergeRuntime,
};
45 changes: 45 additions & 0 deletions merge-runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const shell = require("shelljs");
const path = require("path");
const fs = require("fs");
module.exports = class MergeRemoteChunksPlugin {
// Define `apply` as its prototype method which is supplied with compiler as its argument
apply(compiler) {
// Specify the event hook to attach to
compiler.hooks.afterEmit.tap("MergeRemoteChunksPlugin", (output) => {
const emittedAssets = Array.from(output.emittedAssets);
const files = ["static/chunks/webpack", "static/runtime/remoteEntry"]
.filter((neededChunk) =>
emittedAssets.some((emmitedAsset) =>
emmitedAsset.includes(neededChunk)
)
)
.map((neededChunk) =>
emittedAssets.find((emittedAsset) =>
emittedAsset.includes(neededChunk)
)
)
.map((file) => path.join(output.compiler.context, ".next", file));

if (files.length > 1) {
console.log(files)
const runtime = fs.readFileSync(files[0], "utf-8");
const remoteContainer = fs.readFileSync(files[1], "utf-8");
const merged = [runtime,remoteContainer].join("\n");
const remotePath = path.join(output.compiler.context, ".next/static");
if (fs.existsSync(remotePath)) {
fs.mkdir(remotePath, { recursive: true }, (err) => {
if (err) throw err;
});
}
fs.writeFile(
path.resolve(
output.compiler.context,
".next/static/remoteEntry.js"
),
merged,
()=>{}
);
}
});
}
};
4 changes: 1 addition & 3 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"extends": [
"config:base"
]
"extends": ["config:base"]
}

0 comments on commit 5024990

Please sign in to comment.