0

I got a vueJS app where people can use widgets. Widgets are composed of 2 VueJS components and an index.ts where the index.ts export default object like :

{name: "Foo", component: vueComponent, settingsComponent; OtherVueComponent}.

This is bundled with vite:

build({
      plugins: [vue()],
      build: {
        lib: {
          entry: [path.resolve(dataComponentsDir, dataComponent, "index.ts")],
          name: dataComponent,
          fileName: (_format) => `${dataComponent}.js`,
          formats: ["es"],
        },
        rollupOptions: {
          external: ["vue", "typeorm", /^@repo\//, "vue-echarts", /^echarts\//],
          output: {
            inlineDynamicImports: false,
            preserveModules: false,
            format: "es",
            exports: "named",
            globals: {
              vue: "Vue",
            },
          },
        },
        preserveEntrySignatures: "strict",
        outDir: path.resolve(tempDir, dataComponent),
        emptyOutDir: true,
      },
    });

. It gives me a one JS file esmodule that i stored as base64 in a postgreDB. My issue is how could i now import and use this module in a vue client?

I tried to import the modules but they cannot access vue, echarts and other external libraries: TypeError: Failed to resolve module specifier "vue". Here is how i import modules:

  const moduleText = atob(base64Module);

  const blob = new Blob([moduleText], { type: 'application/javascript' });
  const moduleURL = URL.createObjectURL(blob);

  try {
    const module = await import(moduleURL);
  } catch(e) {
    console.error(e)
  }

I was expecting import to share current context.

5
  • You need to decode the base64 string and create a Blob url. This url should dynamically import the module and use it in the vue component. Commented Mar 7 at 14:19
  • "I tried to import the modules" - how exactly? " I was expecting import to share current context" - it won't work that way without additional measures on your side. A cheap way is to share modules that are common to bundles you use through global scope, i.e. you load Vue as CDN and configure all bundles to map vue import to window.Vue global. A more high-tech way is to use module federation for chunks that are supposed to be loaded separately Commented Mar 7 at 14:36
  • @KeyboardCorporation Thanks for your quick response, this is already how i import modules, i updated the post. Commented Mar 10 at 13:04
  • @EstusFlask Thanks for your response, never heard about module federation, i will have a look. The base64 modules still have import keyword inside so i can't figure out how i can make it through global scope. Commented Mar 10 at 13:17
  • @LeopoldBriand You need to configure both widget and main app builds to map vue import to window.Vue. Considerting that both builds are vite, it would be something like this stackoverflow.com/a/78153788/3731501 , with vue: 'Vue' instead of react: 'react' Commented Mar 10 at 13:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.