From 73e7e0edc5b25c686299c8e7fb916c8b66ae600e Mon Sep 17 00:00:00 2001 From: Maximilian Klein Date: Mon, 2 Aug 2021 08:12:01 +0200 Subject: [PATCH 1/2] Add update mechanism for cache-busting --- src/NodeModuleFederation/index.js | 60 +++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/src/NodeModuleFederation/index.js b/src/NodeModuleFederation/index.js index 3eda07c..6ab4039 100644 --- a/src/NodeModuleFederation/index.js +++ b/src/NodeModuleFederation/index.js @@ -24,24 +24,48 @@ const rpcPerformTemplate = ` `; const rpcProcessTemplate = (mfConfig) => ` - function rpcProcess(remote) { - return {get:(request)=> remote.get(request),init:(arg)=>{try {return remote.init({ + function rpcProcess(remoteUrl) { + let initArgs = null; + const init = (remote, arg)=>{ + try { + return remote.init({ ...arg, - ${Object.keys(mfConfig.shared) - .filter( - (item) => - mfConfig.shared[item].singleton && - mfConfig.shared[item].requiredVersion - ) - .map(function (item) { - return `"${item}": { - ["${mfConfig.shared[item].requiredVersion}"]: { - get: () => Promise.resolve().then(() => () => require("${item}")) - } - }`; - }) - .join(",")} - })} catch(e){console.log('remote container already initialized')}}} + ${Object.keys(mfConfig.shared) + .filter( + (item) => + mfConfig.shared[item].singleton && + mfConfig.shared[item].requiredVersion + ) + .map(function (item) { + return `"${item}": { + ["${mfConfig.shared[item].requiredVersion}"]: { + get: () => Promise.resolve().then(() => () => require("${item}")) + } + }`; + }) + .join(",")} + }) + } catch(e) { + console.log('remote container already initialized') + } + }; + return { + get:(request)=> { + return async () => { + // refetch and init + const remote = await rpcPerform(remoteUrl); + const initRes = await init(remote, initArgs); + console.log('initRes', initRes); + const getRes = await remote.get(request); + return getRes(); + } + }, + init: async (arg) => { + initArgs = arg; + const remote = await rpcPerform(remoteUrl); + return init(remote, arg); + }, + } } `; @@ -54,7 +78,7 @@ function buildRemotes(mfConf) { acc[name] = { external: `external (function() { ${builtinsTemplate} - return rpcPerform("${config}").then(rpcProcess) + return rpcProcess("${config}") }())`, }; return acc; From 3187267147b04f3314a2830d815657113492eda0 Mon Sep 17 00:00:00 2001 From: Maximilian Klein Date: Sun, 12 Sep 2021 21:06:18 +0200 Subject: [PATCH 2/2] Make SSR references eager. --- src/NodeModuleFederation/index.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/NodeModuleFederation/index.js b/src/NodeModuleFederation/index.js index 6ab4039..0a9723b 100644 --- a/src/NodeModuleFederation/index.js +++ b/src/NodeModuleFederation/index.js @@ -55,15 +55,13 @@ const rpcProcessTemplate = (mfConfig) => ` // refetch and init const remote = await rpcPerform(remoteUrl); const initRes = await init(remote, initArgs); - console.log('initRes', initRes); const getRes = await remote.get(request); return getRes(); } }, - init: async (arg) => { + init: (arg) => { initArgs = arg; - const remote = await rpcPerform(remoteUrl); - return init(remote, arg); + return; }, } } @@ -97,4 +95,4 @@ class NodeModuleFederation { } } -module.exports = NodeModuleFederation; \ No newline at end of file +module.exports = NodeModuleFederation;