Skip to content

Commit 919e4a5

Browse files
authored
fix/solve medusa next plugin post issues (#471)
* fix: solve NextMedusaPlugin issues * chore: add remoteBasePath option * chore: add skipPost option
1 parent a99c0f0 commit 919e4a5

File tree

2 files changed

+59
-50
lines changed

2 files changed

+59
-50
lines changed

dashboard-plugin/FederationDashboardPlugin.js

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class AddRuntimeRequiremetToPromiseExternal {
5151
compiler.hooks.compilation.tap(
5252
"AddRuntimeRequiremetToPromiseExternal",
5353
compilation => {
54-
const {RuntimeGlobals} = compiler.webpack;
54+
const { RuntimeGlobals } = compiler.webpack;
5555
if (compilation.outputOptions.trustedTypes) {
5656
compilation.hooks.additionalModuleRuntimeRequirements.tap(
5757
"AddRuntimeRequiremetToPromiseExternal",
@@ -83,7 +83,7 @@ class FederationDashboardPlugin {
8383
*/
8484
constructor(options) {
8585
this._options = Object.assign(
86-
{debug: false, filename: "dashboard.json", fetchClient: false},
86+
{ debug: false, filename: "dashboard.json", fetchClient: false },
8787
options
8888
);
8989
this._dashData = null;
@@ -125,6 +125,7 @@ class FederationDashboardPlugin {
125125
"__REMOTE_VERSION__",
126126
""
127127
);
128+
128129
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
129130
compilation.hooks.processAssets.tapPromise(
130131
{
@@ -195,14 +196,18 @@ class FederationDashboardPlugin {
195196
if (graphData) {
196197
const dashData = (this._dashData = JSON.stringify(graphData));
197198

198-
if (this._options.dashboardURL && !this._options.nextjs) {
199-
this.postDashboardData(dashData).catch(err => {
200-
if (err) {
201-
curCompiler.errors.push(err);
202-
// eslint-disable-next-line promise/no-callback-in-promise
203-
throw err;
204-
}
205-
});
199+
if (this._options.skipPost) {
200+
console.info('Skipping post to dashboard')
201+
} else {
202+
if (this._options.dashboardURL && !this._options.nextjs) {
203+
this.postDashboardData(dashData).catch(err => {
204+
if (err) {
205+
curCompiler.errors.push(err);
206+
// eslint-disable-next-line promise/no-callback-in-promise
207+
throw err;
208+
}
209+
});
210+
}
206211
}
207212

208213
return Promise.resolve().then(() => {
@@ -248,6 +253,7 @@ class FederationDashboardPlugin {
248253
typeof rawData.version === "string"
249254
? `_${rawData.version.split(".").join("_")}`
250255
: `_${rawData.version.toString()}`;
256+
251257
let codeSource;
252258
if (!remoteEntry.source._value && remoteEntry.source.source) {
253259
codeSource = remoteEntry.source.source();
@@ -311,7 +317,7 @@ class FederationDashboardPlugin {
311317
const stringifiableChunk = Array.from(subset).map(sub => {
312318
const cleanSet = Object.getOwnPropertyNames(sub).reduce((acc, key) => {
313319
if (key === "_groups") return acc;
314-
return Object.assign(acc, {[key]: sub[key]});
320+
return Object.assign(acc, { [key]: sub[key] });
315321
}, {});
316322

317323
return this.mapToObjectRec(cleanSet);
@@ -422,9 +428,9 @@ class FederationDashboardPlugin {
422428
if (this._options.filename) {
423429
const hashPath = path.join(stats.outputPath, this._options.filename);
424430
if (!fs.existsSync(stats.outputPath)) {
425-
fs.mkdirSync(stats.outputPath, {recursive: true});
431+
fs.mkdirSync(stats.outputPath, { recursive: true });
426432
}
427-
fs.writeFileSync(hashPath, dashData, {encoding: "utf-8"});
433+
fs.writeFileSync(hashPath, dashData, { encoding: "utf-8" });
428434
}
429435
if (this._options.debug) {
430436
console.log(
@@ -436,7 +442,7 @@ class FederationDashboardPlugin {
436442
try {
437443
file = assets[this.FederationPluginOptions.filename]._value;
438444

439-
const {version} = JSON.parse(dashData);
445+
const { version } = JSON.parse(dashData);
440446
if (!version) {
441447
throw new Error("no version provided, cannot version remote");
442448
}
@@ -451,7 +457,7 @@ class FederationDashboardPlugin {
451457
}
452458
fs.mkdir(
453459
path.join(stats.outputPath, version),
454-
{recursive: true},
460+
{ recursive: true },
455461
err => {
456462
if (err) throw err;
457463
fs.writeFile(
@@ -485,16 +491,26 @@ class FederationDashboardPlugin {
485491
fs.writeFile(
486492
statsPath,
487493
JSON.stringify(stats),
488-
{encoding: "utf-8"},
494+
{ encoding: "utf-8" },
489495
() => {
490496
}
491497
);
492498
}
499+
}
493500

501+
class NextMedusaPlugin {
502+
constructor(options) {
503+
this._options = options;
504+
if (this._options.debug) {
505+
console.log("NextMedusaPlugin constructor", options);
506+
}
507+
}
494508
async postDashboardData(dashData) {
509+
495510
if (!this._options.dashboardURL) {
496511
return Promise.resolve();
497512
}
513+
498514
const client = this._options.fetchClient
499515
? this._options.fetchClient
500516
: fetch;
@@ -509,22 +525,15 @@ class FederationDashboardPlugin {
509525
});
510526

511527
if (!res.ok) throw new Error(res.statusText);
528+
529+
return res
512530
} catch (err) {
513531
console.warn(
514532
`Error posting data to dashboard URL: ${this._options.dashboardURL}`
515533
);
516534
console.error(err);
517535
}
518536
}
519-
}
520-
521-
class NextMedusaPlugin {
522-
constructor(options) {
523-
this._options = options;
524-
if (this._options.debug) {
525-
console.log("medusa plugin constructor", options);
526-
}
527-
}
528537

529538
apply(compiler) {
530539
if (
@@ -560,40 +569,39 @@ class NextMedusaPlugin {
560569
nextjs: true
561570
}).apply(compiler);
562571

563-
return;
572+
const hostData = path.join(
573+
compiler.options.output.path,
574+
compiler.options.name + "-" + filename
575+
);
564576
compiler.hooks.done.tap(PLUGIN_NAME, () => {
565-
const sidecarData = path.join(
566-
compiler.options.output.path,
567-
`child-dashboard.json`
568-
);
569-
const hostData = path.join(
570-
compiler.options.output.path,
571-
"dashboard.json"
572-
);
573-
console.log("sidecar data", sidecarData);
574-
console.log("host data", hostData);
575-
if (fs.existsSync(sidecarData) && fs.existsSync(hostData)) {
576-
console.log("will write merged files");
577+
if (fs.existsSync(hostData)) {
577578
fs.writeFileSync(
578579
hostData,
579-
JSON.stringify(mergeGraphs(require(sidecarData), require(hostData)))
580+
JSON.stringify(require(hostData))
580581
);
581582
}
582583
});
583584

584-
compiler.hooks.afterDone.tap("NextMedusaPlugin", (stats, done) => {
585-
if (fs.existsSync(sidecarData) && fs.existsSync(hostData)) {
585+
compiler.hooks.afterDone.tap("NextMedusaPlugin", (stats) => {
586+
if (fs.existsSync(hostData)) {
586587
const dashboardData = fs.readFileSync(hostData, "utf8");
587-
MedusaPlugin.postDashboardData(dashboardData)
588-
.then(done)
589-
.catch(done);
590-
} else {
591-
done();
588+
if (this._options.skipPost) {
589+
console.info('Skipping post to dashboard')
590+
} else {
591+
this.postDashboardData(dashboardData)
592+
.then(() => {
593+
console.info('Data has been successfully sent to the dashboard')
594+
})
595+
.catch((error) => {
596+
console.error('Failed to send data to the dashboard:', error)
597+
});
598+
}
592599
}
593600
});
601+
594602
}
595603
}
596604

597605
module.exports = FederationDashboardPlugin;
598606
module.exports.clientVersion = require("./client-version");
599-
module.exports.NextMedusaPlugin = NextMedusaPlugin;
607+
module.exports.NextMedusaPlugin = NextMedusaPlugin;

dashboard-plugin/client-version.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = ({ currentHost, remoteName, dashboardURL }) => {
1+
module.exports = ({ currentHost, remoteName, dashboardURL, remoteBasePath }) => {
22
//language=JS
33
return `promise new Promise((resolve, reject) => {
44
fetch("${dashboardURL}&currentHost=${currentHost}&remoteName=${remoteName}", {
@@ -13,8 +13,9 @@ module.exports = ({ currentHost, remoteName, dashboardURL }) => {
1313
.then(function (data) {
1414
var name = data.name + "_" + data.version;
1515
var filename = data.version + '.remoteEntry.js';
16-
var url = new URL(filename, data.remoteURL)
17-
16+
var path = \`${remoteBasePath}/\${filename}\`.replace(/\\/\\/+/g, '/');
17+
var url = new URL(path, data.remoteURL);
18+
1819
new Promise(function (resolve, reject) {
1920
var __webpack_error__ = new Error()
2021
if (typeof window[name] !== 'undefined') return resolve();

0 commit comments

Comments
 (0)