What is the proper way to incorporate a standalone Vue library into a Vue application?

I'm currently facing an issue with integrating a vue component library into my vue application. This component library is loaded by a script tag and is set up with webpack using the externals setting to exclude vue dependencies. Therefore, the host bundle that is using this library needs to include vue and other necessary dependencies. The component library is built using vue cli with the script

vue-cli-service build --target lib
to generate a lib.umd.js file. In the index.html of my host bundle, I include this file using a script tag. The webpack configuration of the host bundle uses
config.externals(['@test/my-component-library'])
to exclude the external component library. In my host bundle, I import and use the component library like this:

import { MyComponent } from '@test/my-component-library'

However, when I run my application, I encounter the error message:

Uncaught SyntaxError: Invalid or unexpected token
. This error seems to be originating from the following line in my host bundle:

/*!*******************************************************!*\
  !*** external "@test/my-component-library" ***!
  \*******************************************************/

Could someone please explain why webpack is generating this code and suggest possible solutions to resolve this issue?

Answer №1

After some troubleshooting, I managed to figure out the solution. Initially, the issue lied in an incorrect configuration for the externals setting. The correct setup should look like this:

config.externals({
  '@test/my-component-library': {
    root: '@test/my-component-library',
    commonjs2: '@test/my-component-library',
    commonjs: '@test/my-component-library',
    amd: '@test/my-component-library',
  },
})

Additionally, in the component library, I had to include a webpack configuration

config.output.library('MyComponentLibrary')
to ensure that the object MyComponentLibrary is globally accessible in the window scope. Following that, I had to eliminate the import statement from the host bundle. In order to register the component, I had to incorporate this line into my vue file:

components: {
  'MyComponent': MyComponentLibrary.MyComponent,
}

However, a new challenge emerged. The component library itself relies on vuetify, which is another component library. Despite adding vuetify to the externals option because it was already present in the host bundle, I started encountering numerous Unknown custom element errors. It appears that my component library is unable to locate the vuetify components within the host bundle. Any recommendations on how to resolve this issue?

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

Leverage JavaScript Object Properties within Angular 5 Components

Here's a question I have on using ngx-charts in Angular 5. I am experimenting with ngx-charts to create a chart within my Angular 5 project. The code snippet for my component is shown below: import { Component, OnInit } from '@angular/core&ap ...

Insert a hyperlink button using InnerHtml

I am facing an issue with displaying a list item that contains a tab and a link button: <li runat="server" id="liActivityInvoices"><a href="#tabActivityInvoices">Invoices</a><asp:LinkButton runat="server" ID="btnLoadInvoice" OnClick=" ...

Are there more efficient methods than having to include require('mongoose') in each models file?

Is it possible to only require mongoose once in the main app.js file and then pass it to other files without loading it again? Will the script do extra work every time the same module is required? var mongoose = require('mongoose'); I'm wo ...

AngularJS Element Connections

I have the following component: (function () { "use strict"; angular.module("application_module") .component('tab', { controller: 'TabCtrl', templateUrl: 'app/component/application/app-heade ...

a single function spread across two controllers[JavaScript]

My query pertains to a JavaScript program with 2 controllers. The issue I am facing is the need for one function in both controllers (data.duration). This function serves two purposes, once for features themselves and once for the summary. Currently, this ...

Obtain specific fields from a multidimensional array object using lodash

My dilemma involves working with an object that has the following structure: var data = [ { "inputDate":"2017-11-25T00:00:00.000Z", "billingCycle":6, "total":1 },{ "inputDate":"2017-11-28T00:00:00.000Z", "bi ...

Alert displayed on console during transition from MaterialUI lab

Whenever I try to run my MUI application, an error pops up in the console It seems you may have forgotten to include the ref parameter in your forwardRef render function. import { LoadingButton } from "@mui/lab"; const LoadData = ({loading,sig ...

Jasmine and Karma encountered a TypeError stating that the function this.role.toLowerCase is not valid

Currently, I am in the process of writing a test case for a page within an application that our team is actively developing. However, I have encountered a challenging error within one of the test cases that I am struggling to overcome. Below is my Spec fil ...

Creating Unique Identifiers in ExpressJS

I am currently utilizing mongoose to display admin and user information on a dashboard, but I am encountering difficulty rendering the id of a user. Below is the code I am using: function ensureAuthenticated(req, res, next){ if(req.isAuthenticated()){ ...

Can anyone offer me advice on troubleshooting a failed Capifony deployment caused by a time out during the assetic:dump process?

$ cap deploy Unfortunately, the deployment process is failing and I am receiving the following error message: * executing "cd /var/www/site/prod/releases/20120831164520 && php app/console assetic:dump web --env=prod --no-debug" servers: ["site ...

Update the pageExtensions setting in Next.js to exclude building pages with the file extension *.dev.*

I am currently working on a project using Next.js version v12.3, and I have encountered an issue related to excluding page files with a *.dev.* extension from the build process. In my configuration file next.config.js, I have configured the pageExtensions ...

Having difficulty installing 'node-sass' on Ubuntu 20

My development environment includes webpack 4, sass, pug, and bemto. While everything was functioning normally on Windows, I encountered issues when running the same module on Ubuntu: npm ERR! code ELIFECYCLE npm ERR! syscall spawn npm ERR! file sh npm ER ...

What is a way to transfer an Object from one Vue.js component to another without relying on vuex?

Using vue-router in app.vue, I defined the two components as shown below: <div class='app'> <div class='nav'> <router-link to='/a'>to A component</router-link> <router-link to= ...

Ways to activate JavaScript on a completely dynamic AJAX ASP.NET website

Encountering a dilemma: I have developed a fully dynamic site with only one update panel on the startpage. All content is generated dynamically through code behind, with different pages as web user controls. The issue arises when attempting to open two d ...

general structure used to activate every page in vue 3

AppTopbar.vue <Dropdown v-model="selected" :options="storesLists" @change="onChange" optionLabel="name" :filter="true" /> onChange(event) { console.log(event.value.value); localSt ...

Adding the Setting component to the Style Manager

I'm struggling to add component settings (traits) into the Style Manager panel section because of insufficient documentation. The way it is demonstrated in the demo is not clear to me. Is there a specific block of code that I should be using? I' ...

Guide to embedding bootstrap.min.js into ember-bootstrap extension

I'm currently experiencing an issue with the ember-bootstrap plugin. Following the instructions on their setup page at , I downloaded and installed it. Once I began working on the Navbar, I encountered a problem. The Hamburger menu, which is supposed ...

Unable to scroll after the webpage redirects

I am currently working on developing a website for uploading images with a comment feature. The comments section is displayed below the image in the 'imagedisplay.php' file. If a user is not logged in, they are redirected to the sign-up page and ...

Troubleshooting jsPDF problem with multi-page content in React and HTML: Converting HTML to PDF

I need to convert HTML content in my React application into a PDF file. My current approach involves taking an HTML container and executing the following code snippet: await htmlToImage .toPng(node) .then((dataUrl) => { ...

Is Cloud Spanner effectively handling session management?

I've spent some time researching this issue but unfortunately haven't been able to find a satisfactory answer. The Google Cloud Spanner client libraries automatically handle sessions, with a limit of 10,000 sessions per node. So far, so good. M ...