module-federation-examples icon indicating copy to clipboard operation
module-federation-examples copied to clipboard

How to make complete production build without http://localhost:3001/remoteEntry.js in production main.js?

Open pravosleva opened this issue 4 years ago • 9 comments

изображение

CRA based for example: https://github.com/module-federation/module-federation-examples/tree/master/cra

изображение

pravosleva avatar Dec 28 '21 00:12 pravosleva

Use promise new promise to change remote url at runtime or use env vars

ScriptedAlchemy avatar Dec 28 '21 09:12 ScriptedAlchemy

Young technology =) this code works, but final static build does not work correctly without external magic. @ScriptedAlchemy Could you please, add to this example best way to get final build (ready to be hosted by NGINX, for example) in your opinion.

const isDev = !!process.env.NODE_ENV ? process.env.NODE_ENV === 'development' : true
const PUBLIC_PATH_1 = process.env.PUBLIC_PATH_1 || 'remote/'
const plugins = []

// ...

plugins.push(new ModuleFederationPlugin({
  name: "host", // consumer
  filename: "remoteEntry.js",
  remotes: {
    'remote': isDev
      ? 'remote@http://localhost:3001/remoteEntry.js'
      : `promise new Promise(resolve => {
  const thisScript = document.currentScript;
  thisScript.src = './${PUBLIC_PATH_1}main.js';
})`,
  },
  exposes: {},
  shared: {
    ...deps,
    react: {
      singleton: true,
      requiredVersion: deps.react,
    },
    "react-dom": {
      singleton: true,
      requiredVersion: deps["react-dom"],
    },
  },
}))

pravosleva avatar Dec 28 '21 14:12 pravosleva

I've manage that using '.env'. You cant take a look at the project example i've made here At my config i use env properties and they are replace by the wepack itself according with the build, dev or prod

schirrel avatar Jan 01 '22 15:01 schirrel

Does anyone have a good approach on getting the URLs for different environments during runtime? My approach is to build a production image once and reuse it for every different environment in openshift with different configurations injected at deploy time instead of building different images for each environment.

mario-subo avatar Jan 05 '22 07:01 mario-subo

promise new Promise is the best syntax for it since you can read and inject any remote from any location that you please.

you can also expose and set public path at runtime via a webpack runtime global. If you search my repo i also have a external remote plugin that reads values off the window or something else as you need.

ScriptedAlchemy avatar Jan 06 '22 22:01 ScriptedAlchemy

i've seen promise new promise recommended in quite a few issues. is it worth adding a real world example on how this is done in a real world applications that supports multiple stages (dev, beta, prod, etc...)? curious on best practices around this.

thunderousNinja avatar Jan 26 '22 22:01 thunderousNinja

Would be worth doing so but Its usually too much effort to actually build out a deployable example of that mechanism.

How I have it configured is usually a multi stage fallback approach. So pass a remote@url then have a parser that loops over the strings and generates a promise new promise variant where it looks for a window.remoteNameOverride || cookie.get(renoteNameOverride) || detectStageAndRewrite || use the original @ url that was passed to the plugin to begin with.

Now I can read location.href and point to stage if im in stage and there's no configuration to override it, and if im in prod it'll do the same. But at any point on any machine - I can still swap out the remote webpack will inject.

You could also point to an api or json and fetch() the config for the current host

ScriptedAlchemy avatar Jan 28 '22 03:01 ScriptedAlchemy

@ScriptedAlchemy interesting approach! Curious if you have a sample gist? I’m trying to get a similar approach working with the dynamic-system-host example since I have like 15 remotes (with many more upcoming) and I don’t want to have dozens of remoteEntry calls at all startup.

thunderousNinja avatar Feb 22 '22 21:02 thunderousNinja

I don't have a gist but use promise new promise and you can write the logic. I'll get around to an example at some point since I'll be implementing this exact capabilities for work. Since we have many codebases on different envs, QA needs a way to change the graph at will

ScriptedAlchemy avatar Feb 27 '22 04:02 ScriptedAlchemy