Skip to content

Instantly share code, notes, and snippets.

@b2whats
Forked from yordis/MutateRuntimePlugin.js
Created February 9, 2021 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b2whats/56e6757bfcedc7a65b248203f8ad1012 to your computer and use it in GitHub Desktop.
Save b2whats/56e6757bfcedc7a65b248203f8ad1012 to your computer and use it in GitHub Desktop.
Remote PublicPath Modification
const PLUGIN_NAME = "MutateRuntimePlugin";
class MutateRuntimePlugin {
/**
*
* @param {FederationDashboardPluginOptions} options
*/
constructor(options) {}
/**
* @param {Compiler} compiler
*/
apply(compiler) {
const FederationPlugin = compiler.options.plugins.find((plugin) => {
return plugin.constructor.name === "ModuleFederationPlugin";
});
let FederationPluginOptions;
if (FederationPlugin) {
FederationPluginOptions = FederationPlugin._options;
}
compiler.hooks.make.tap("MutateRuntime", (compilation) => {
compilation.hooks.runtimeModule.tap("MutateRuntime", (module, chunk) => {
if (
module.constructor.name === "PublicPathRuntimeModule" &&
chunk.name === FederationPluginOptions.name
) {
const iffy = [
"+ (function(){",
"try {",
"return window.versions[window.versions.currentHost].override.find(function(override){return override.name === '" +
FederationPluginOptions.name +
"'}).version + '/'",
"} catch(e) {",
"console.warn(e);",
'return ""',
"}",
"})();",
].join("");
const generatedCode = module.getGeneratedCode();
const splitGeneration = generatedCode.split("=");
module._cachedGeneratedCode = generatedCode.replace(
splitGeneration[1],
splitGeneration[1].split(";")[0] + iffy
);
console.log(module._cachedGeneratedCode);
return module;
}
});
});
compiler.hooks.done.tap("MutateRuntime", () => {
// debugger;
});
}
}
module.exports = MutateRuntimePlugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment