Issues arise when attempting to load a vue module within a micro-frontend setup

Still fairly new to VUE, I'm excited to implement the best practices I've picked up from other frontend technologies, particularly the micro-frontend approach. While I have successfully been able to dynamically load my module, I've hit a roadblock when it comes to adding routes for my micro-apps to the router.

When I use this code snippet:

router.addRoute("root", {
    path: '/' + frontendModule.name.toLowerCase(),
    name: frontendModule.name,
    component: () => import(frontendModule.url),
    meta: { title: frontendModule.name }
})

It's not working as expected and I'm seeing an error message saying "Failed to resolve module specifier 'module-web-cli/App'."

However, when I modify the code like this:

router.addRoute("root", {
    path: '/' + frontendModule.name.toLowerCase(),
    name: frontendModule.name,
    component: () => import("module-web-cli/App"),
    meta: { title: frontendModule.name }
})

Surprisingly, it starts to work. Though interestingly enough, the value of "frontendModule.url" remains exactly as "module-web-cli/APP".

Answer №1

When considering the path transformation process, keep in mind that the compiler will automatically change "module-web-cli/App" to "./module-web-cli/App.vue" if the path is a constant string. However, this transformation won't occur if the path is stored in a variable. To address this issue, simply update frontendModule.url to point directly to './module-web-cli/App.vue'.

Answer №2

Thanks to the collaborative effort of my coworkers, we successfully managed to make it function:

The new route is added in the following manner:

        router.addRoute("root", {path: '/' + frontendModule.name.toLowerCase(), name: frontendModule.name, component: () => import(axios.defaults.baseURL + "assets/module-" +  frontendModule.url + ".js").then(value => value.App), meta: { title: frontendModule.name}})

The crucial element appears to be the ".then(value => value.App)" which instantiates the "App" type within the loaded javascript file.

We also made significant changes to the vite config of the modules.

The main application now includes this:

export default defineConfig({
  build: {
    target: 'esnext',
    rollupOptions: {
      external: ["vue"],
      output: {
        globals: {
          vue: "Vue",
        },
      },
    },
  },
  server: {
    port: 8085
  },
  plugins: [
    vue(),
    vueJsx(),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})

Here is the configuration for the dynamically loaded module:

export default defineConfig({
  define: { "process.env.NODE_ENV": '"production"' },
  build: {
    target: "esnext",
    lib: {
      entry: resolve(__dirname, "./src/lib.ts"),
      name: "CLI",
      fileName: "module-web-cli",
    },
    rollupOptions: {
      external: ["vue"],
      output: {
        globals: {
          vue: "Vue",
        },
      },
    },
  },
  plugins: [vue(), vueJsx()],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
});

Additionally, the index.html required a modulemap to specify where to load vue from:

<script type="importmap">
  {
    "imports": {
      "vue": "./vue.runtime.esm-browser.prod.js"
    }
  }
</script>

With this updated configuration, I am now able to effortlessly load my modules as needed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to access the element menu with element-ui?

I am looking for a way to programmatically open an element in my menu, but I haven't been able to find any information on how to do it. HTML <script src="//unpkg.com/vue/dist/vue.js"></script> <script src="//unpkg.com/<a hr ...

Placing a component within a div element using Vue

I recently integrated a Vue module to make objects draggable and resizable on my project. In my design, I envisioned creating movable and sizable boxes that contain dropdown menus, input fields, and radio buttons. As a starting point, I have included a sim ...

Designing personalized plugins with Typescript in Nuxt

In my Nuxt project, I have implemented a custom plugin file that contains an object with settings called /helpers/settings: export const settings = { baseURL: 'https://my-site.com', ... }; This file is then imported and registered in /plugi ...

Experience a clean slate with Vue.js 3 after running 'vue serve'

Hey there, I'm having trouble figuring out what's going on. I've tried using two different libraries with no success. I have VueJS 3 installed through Vue Client and it worked fine initially. But when I tried creating a view or component to ...

Deploy your Vue.js project using Jenkins for seamless integration

I am new to Jenkins and have successfully completed a few builds/deployment jobs of .net projects. Now I am attempting to build/deploy a Vue.js project through Jenkins, but I am facing difficulties... I can build the project directly on a server using th ...

I am having difficulty creating grid-template-columns in Vue

When I placed my code in the style scoped section... This is the desired output that I'm aiming for: .user-grid{ display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); grid-gap: 1rem; } .user-car ...

Guide to displaying a template using Vue.js

Incorporating jQuery and vuejs version 2.3.4: https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js The code snippet below demonstrates my current setup: // Instantiating Vue. vueVar = new Vue({ el: '#content', ...

Utilizing Vue JS to showcase a pop-up block when hovering over a specific image

There are four images displayed, and when you hover over each one, different content appears. For example, hovering over the first image reveals a green block, while hovering over the second image reveals a blue block. However, the current logic in place i ...

Unable to import createBatchingNetworkInterface from apollo-client

Currently, I am in the process of integrating graphql into my Vue project by following the guidance provided at https://github.com/Akryum/vue-apollo Although I have successfully installed 'apollo-client' via npm as per the requirements, I am enc ...

Connect a VueJS dynamic component to listen for events emitted from its child component

I am currently developing a VueJS (2.5.22) application with Typescript and facing the challenge of dynamically adding components at run-time. I encountered two issues specifically related to Typescript - adding child components as named slots and subscribi ...

Executing VueJS keyup handler after the previous onclick handler has been executed

Link to example code demonstrating the issue https://codepen.io/user123/pen/example-demo I am currently facing an issue with a text field named search_val that has a watcher attached to it. The text field includes a v-on keyup attribute to detect when th ...

Issue encountered while attempting to deactivate button until numerical data is entered in the OTP field using Vuejs

otp_value: '', isFadeout: false, verifyOtp() { this.disabled = true; this.otpBtnClicked = false; this.verified = true; }, <input class="o ...

What are the steps to develop a Vue application with a built-in "add to desktop" functionality?

Looking to develop a Vue application with the feature of adding to desktop, similar to the option near the share button on YouTube. Any suggestions on how to achieve this? https://i.stack.imgur.com/nXG4i.png ...

When attempting to define a route as a lambda function in vue-cli, the default lazy load code does not function

After using vue-cli to set up a new Typescript project with vue-router included, I noticed that the generated router/index.ts configuration looked like this: const routes: Array<RouteConfig> = [ { path: '/', name: 'Home' ...

Tips for retrieving data values in Vue.js

data(){ return { filters: [ { key: 'a', value: '12' }, { key: 'b', value: '34' }, { key: 'c', value: '56' }, { key: 'd', value: '78' }, ...

Issues with running Vue commands have been reported in Git-Bash, however they seem to work fine in

Whenever I check the version of Vue in my Terminus bash by running vue --version, here's the output I receive: $ vue -v /bin/sh: /Users/kirkb/AppData/Local/Yarn/bin/../Data/global/node_modules/.bin/vue: No such file or directory In PowerShell, when I ...

Looking to cycle through arrays containing objects using Vue

Within my array list, each group contains different objects. My goal is to iterate through each group and verify if any object in a list meets a specific condition. Although I attempted to achieve this, my current code falls short of iterating through eve ...

Best practices for managing login authentication using MongoDB in a RESTful API

I'm currently figuring out how to verify if the values align with the MongoDB data. In my approach, I'm utilizing the PUT method along with attempting to utilize findOneAndUpdate to validate the values. <script> const logindetails = new Vu ...

Using the same Vue component instance on different routes

I'm currently working on a Vuejs app where each page is rendered based on the route. For example: route = /, Component = Main.vue <template> <div> <toolbar :user="user"></toolbar> <app-content></app-conten ...

What is the process for importing a component along with all its dependencies?

I have a script where I am attempting to import a component and utilize its dependencies in the template. Specifically, I am looking to locally register a component FooComponent and use SharedComponent within the template of BarComponent. In Angular 2, one ...