Coder Social home page Coder Social logo

Comments (15)

DileSoft avatar DileSoft commented on May 20, 2024 5

Maybe __federation_method_getRemote and remotesMap should be attached to window and could be accesses from anywhere.

from vite-plugin-federation.

ruleeeer avatar ruleeeer commented on May 20, 2024 1

Thank you, but if i understand correclty this needs to happen on build level. And can not add Remotes in run time right?

You can try to dynamically fetch a component or register a remote by adding the following code to the compiled code,Please note that the values of the parameters from , format etc. are adjusted according to the different configurations

// dynamic is your remote name
remotesMap['dynamic'] = {
    url: "http://localhost:5003/assets/remoteEntry.js",
    format: 'esm',
    from: 'vite'
}
const comp = await __federation_method_getRemote('dynamic', '. /Button');

Or you can refer to the previous issue #121 (comment)

from vite-plugin-federation.

T0miii avatar T0miii commented on May 20, 2024 1

Very nice, this is what i was looking for, ill give it a spin and come back with feedback, thx a lot.

from vite-plugin-federation.

flyfishzy avatar flyfishzy commented on May 20, 2024

Not supported yet, but contributions are welcome!

from vite-plugin-federation.

T0miii avatar T0miii commented on May 20, 2024

Hello, thank you for you work.

Is there an example how the dynamic remotes are working? Is it similar to webpacks approach?


await __webpack_init_sharing__("default");
// TODO: load the script tag somehow. e. g. with a script loader
const container = window.application_a;
// Initialize the container, it may provide shared modules
await container.init(__webpack_share_scopes__.default);
const module = await container.get("./module");

from vite-plugin-federation.

ruleeeer avatar ruleeeer commented on May 20, 2024

Hello, thank you for you work.

Is there an example how the dynamic remotes are working? Is it similar to webpacks approach?


await __webpack_init_sharing__("default");
// TODO: load the script tag somehow. e. g. with a script loader
const container = window.application_a;
// Initialize the container, it may provide shared modules
await container.init(__webpack_share_scopes__.default);
const module = await container.get("./module");

I have released version 1.1.5, you can check the readme and demo for usage

from vite-plugin-federation.

T0miii avatar T0miii commented on May 20, 2024

Thank you, but if i understand correclty this needs to happen on build level. And can not add Remotes in run time right?

from vite-plugin-federation.

philals avatar philals commented on May 20, 2024

Thanks

This test works for me:

   "@squad2": {
          external: `Promise.resolve(window.localStorage.setItem("address2","http://localhost:3002/assets/remoteEntry.js") || window.localStorage.getItem('address2'))`,
          externalType: "promise",
        },

from vite-plugin-federation.

DileSoft avatar DileSoft commented on May 20, 2024

It will be good if I can change remotesMap directly from code, if it will be attached to window object for example.

from vite-plugin-federation.

T0miii avatar T0miii commented on May 20, 2024

Collaborator

i think with this solution you can change it in the code, but before resolveing your remote component, you would have to update the remoteMap, if i understand you correctly.

from vite-plugin-federation.

DileSoft avatar DileSoft commented on May 20, 2024

I want to use it on dev and prod both and in main code, don't manually edit generated code by plugin.

from vite-plugin-federation.

DileSoft avatar DileSoft commented on May 20, 2024

What I want exactly:

In React code made dynamic import of remote app and remote component, both names of app and component set from variables dynamically depend of props.

from vite-plugin-federation.

T0miii avatar T0miii commented on May 20, 2024

Thank you, but if i understand correclty this needs to happen on build level. And can not add Remotes in run time right?

You can try to dynamically fetch a component or register a remote by adding the following code to the compiled code,Please note that the values of the parameters from , format etc. are adjusted according to the different configurations

// dynamic is your remote name
remotesMap['dynamic'] = {
    url: "http://localhost:5003/assets/remoteEntry.js",
    format: 'esm',
    from: 'vite'
}
const comp = await __federation_method_getRemote('dynamic', '. /Button');

Or you can refer to the previous issue #121 (comment)

@ruleeeer i see that the

is exported, but when using it in the code its undefined, do we need to import someting else first?
@DileSoft did you had any success with this?

from vite-plugin-federation.

iconag-bbasmer avatar iconag-bbasmer commented on May 20, 2024

I have setup an example for loading components completely dynamic, reading which components to load from a .json file. The only issue there is: There seems to be some kind of race condition for loading multiple components when it comes to Hooks. If I only add one component to load in the json file, then everything works fine. If I have multiple, like in the example, the components that use React functionality like Hooks fail loading with an error message telling that Hooks functionality does not exist (see vapp1 in the example).

Repo with example: https://github.com/iconag-bbasmer/vite-module-federation-test

from vite-plugin-federation.

SepVeneto avatar SepVeneto commented on May 20, 2024

Thank you, but if i understand correclty this needs to happen on build level. And can not add Remotes in run time right?

You can try to dynamically fetch a component or register a remote by adding the following code to the compiled code,Please note that the values of the parameters from , format etc. are adjusted according to the different configurations

// dynamic is your remote name
remotesMap['dynamic'] = {
    url: "http://localhost:5003/assets/remoteEntry.js",
    format: 'esm',
    from: 'vite'
}
const comp = await __federation_method_getRemote('dynamic', '. /Button');

Or you can refer to the previous issue #121 (comment)

@ruleeeer i see that the

is exported, but when using it in the code its undefined, do we need to import someting else first? @DileSoft did you had any success with this?

@T0miii maybe need to add the fields remotes in plugin options, even if there is no static remote components.
like this, packages/examples/vue3-demo-esm

import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import federation from '@originjs/vite-plugin-federation'

// https://vitejs.dev/config/
export default defineConfig({
    server: {
        // host: "192.168.56.1",
        // port: 5100
    },
    cacheDir: 'node_modules/.cacheDir',
    plugins: [
        vue(),
        federation({
            name: 'layout',
            filename: 'remoteEntry.js',
+           remotes: {} ,
-           remotes: {
-               home: {
-                   external: `Promise.resolve('http://localhost:5001/assets/remoteEntry.js')`,
-                   externalType: "promise"
-               },
-               'common-lib': {
-                   external:`new Promise(resolve=>resolve('http://localhost:5002/assets/remoteEntry.js'))`,
-                   externalType:"promise"
-               },
-               'css-modules': 'http://localhost:5003/assets/remoteEntry.js'
-           },
            shared: {
                vue:{
                    // This is an invalid configuration, because the generate attribute is not supported on the host side
                },
                pinia:{
                }
            }
        })
    ],
    build: {
        target: 'esnext',
        minify: false,
        cssCodeSplit: true,
        rollupOptions: {
            output: {
                minifyInternalExports: false
            }
        }
    }
})

from vite-plugin-federation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.