Excluding CommonJS libraries from the production build using Webpack in Vue-cli is a helpful strategy to optimize your application

Successfully implemented this for vue using the Webpack config externals


Started by adding the Vue CDN to my HTML file

index.html

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1167647451233f273f2023">[email protected]</a>"></script>

Followed by adjusting the webpack config

vue.config.js

module.exports = {
  configureWebpack: {
    // ...
    externals: {
      vue: 'Vue',
    }
    // ...
  },
}

Everything worked smoothly.


However, when attempting the same with vue-class-component and vue-property-decorator, it didn't go as planned

index.html

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcaaa9b99ceef2eaf2edee">[email protected]</a>"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="720407175f111e1301015f111d1f021d1c171c0632455c405c47">[email protected]</a>"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b7d7e6e267b79647b6e797f72266f6e6864796a7f64794b32253b253b">[email protected]</a>"></script>

vue.config.js

module.exports = {
  configureWebpack: {
    // ...
    externals: {
      vue: 'Vue',
      'vue-class-component': 'VueClassComponent',
      'vue-property-decorator': 'VuePropertyDecorator',
    }
    // ...
  },
}

Noticed that the file names ended with .common.js and .umd.js

vue-class-component.common.js
vue-property-decorator.umd.js

Attempted to modify

vue.config.js

module.exports = {
  configureWebpack: {
    // ...
    externals: {
      vue: 'Vue',
      'vue-class-component': 'commonjs2 vue-class-component',
      'vue-property-decorator': 'umd vue-property-decorator',
    }
    // ...
  },
}

Still no success

Here's how I imported them in my src/. Scripts written in TypeScript

import Vue from 'vue'

// ...

import { Component } from 'vue-property-decorator'

Any advice on handling externals in webpack with .common.js and .umd.js? Much appreciated!

Answer №1

It seems like the issue may not actually be related to the Webpack configuration...

When attempting to access

https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="94e2e1f1b9f7f8f5e7e7b9f7fbf9e4fbfaf1fae0d4a3baa6baa1">[email protected]</a>
, it redirects me to
Original file: /npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0472716129676865777729676b69746b6aada">[email protected]</a>/dist/vue-class-component.common.js
, which appears to be a CommonJS build that is not suitable for browsers. Consider using a link to a "browser-compatible" version like
https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbcdcede96d8d7dac8c896d8d4d6cbd4d5ded5cffb8c9589958d">[email protected]</a>/dist/vue-class-component.min.js

As for vue-property-decorator, accessing it as a UMD module should work in the browser...

By the way, what's the purpose of all this? Wouldn't it be simpler to let Webpack handle everything? It's usually more efficient to download a single large JS file rather than multiple smaller ones...

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

Converting Vuetify data-table computed properties to SQL query transformation

I have a straightforward attendance application that is currently storing data in a SQL database. However, I am faced with the task of storing a computed property in the SQL database as well. Here is a snippet of the configuration: ...

Unable to dispatch an event from a child component to a parent component in Vue.js

Within my parent component, I retrieve an array of strings from an API and pass it down to the child component. The child component then displays this data as a dropdown list. When an item is selected from the dropdown, I aim to assign it to a specific var ...

implement a JavaScript function for sprite toggling

I have been working on a JS/jQuery function to switch the position of an icon sprite. I have successfully created the following code: $('.toggle-completed').mouseup(function(){ var sp = ($(this).css('background-position')); $(this).css ...

Can I retrieve the element of any DOM element I'm currently hovering over using JavaScript?

Suppose I have this HTML snippet: <body> <div id="1"> <span class="title">I'm a title!</span> </div> <div id="2">I'm the first element!</div> <div ...

Optional parameters in Sammy.js

Utilizing ajax for paging has led me to choose Sammy.js, which works well. However, incorporating checkboxes to filter results poses a challenge. While defining a route for Sammy to intercept is feasible, the issue arises when I wish to avoid displaying ce ...

Is there a way to preserve the original index signature of an object while also defining the type of its values?

I have a large object containing various animals with different characteristics. I'm looking to properly type this object in TypeScript. interface Animal { legs: 0 | 1 | 2 | 3 | 4; } const Animals: Record<string, Animal> = { snake: { le ...

Utilizing reusable functionalities within Vuex

Currently, while working on my NUXT project, I am facing a situation where I find myself repeatedly copying the same actions into multiple store modules. To streamline this process, I have extracted these actions into a separate file and now import them in ...

Incorporate an imported type as a nested component within an interface, and subsequently utilize the interface as properties within the Vue3 script

While this may appear to be a straightforward question, I have not been able to find any discussions on this topic. Currently using Vue3 with script setup Objective: In short: I am attempting to utilize a child type definition to specify one key of an in ...

JQuery: Adding new table cells and iterating through them functions correctly in Firefox and Chrome, but encounters issues in Internet Explorer

I am facing a challenge in the following scenario: My objective is to add at least 2 input fields to a table. Then, I need the ability to loop through the newly generated cells to retrieve values from the new fields. The code snippet is as follows: Call ...

The router's handler function sends back a collection of objects, but for some reason, the client is not receiving them in JSON format even though the response

I am currently developing an Express.js project using Typescript. In my project, I have defined an enum and an interface as follows: export enum ProductCategory { ELECTRONICS = 'electronics', CLOTHING = 'clothing', TOYS = & ...

Simply looking to activate keyup, keydown, or space events using jQuery/JavaScript

What is the method to activate a keyup event? Imagine having a text field with the id "item" and triggering an event with a space should result in adding a space to the text field. I am interested in triggering this event solely from the JavaScript consol ...

Creating a file logging system with log4js to capture Console logs

Is there a way to automatically log all console logs, including failed expectations and exceptions, to a file without using try and catch in JavaScript? In Java's LOG4j, the rootlogger feature does this by default. Is there a similar functionality ava ...

Retrieve every video on React.js channel from YouTube

Currently, I am working on integrating react-youtube into my React App with the goal of accessing all videos from a specific YouTube channel. The challenge I am facing is that I need to display these videos as thumbnails, exactly how they are listed in the ...

Setting response query correctly in Solr using AJAX

Inspired by an example of using Solr's JSON output for AJAX, I have incorporated a drop-down menu into my project form and introduced faceting to the parameters. Parameters: function getstandardargs() { var params = [ 'wt=json' ...

Retrieve the component information from the JavaScript function located outside of the main code

Is there a way to retrieve the component data using an external JavaScript function? I am looking to access markers, labels, and images. Vue.component('home', { template: '#home', data: () => ({ markers: [ ...

Best Method for Confirming Deletion in Data Table with Shadow and UI

I have a query regarding the Shadcn/UI - Data Table component. In my case, I am working with a list of users where each row has a delete button in the last column. Upon clicking the delete button, a confirmation dialog pops up. Currently, I am implementi ...

The position of the scroll bar remains consistent as you navigate between different websites

Is it possible for me to click on a link within my HTML website and have the other website load with me in the exact same position? For example, if I'm halfway down a webpage and click a link, can I be taken to that same point on the new page? If so, ...

I am experiencing issues with my HTML select list not functioning properly when utilizing a POST service

When using Angularjs to automatically populate a list with *ngFor and then implementing a POST service, the list stops functioning properly and only displays the default option. <select id="descripSel" (change)="selectDescrip()" > <option >S ...

"Error: Unable to access the property '$emit' of an undefined value" - VueJS

I'm currently working on implementing a basic authentication system in vuejs. I have a set of objects containing valid usernames and passwords. I am looping through this list to validate the entered username and password. If there is a match, I trigge ...

Tips for customizing the IconButton appearance in material-ui

While Material-ui offers a default icon button, I am interested in customizing its design to resemble this: IconButton design needed Could you please advise me on how to make this change? Thank you. ...