Instructing Webpack on Locating Static Resources in a Vue Project

Recently, I made the switch to Vue 3 CLI. After setting up a Vue project, I installed webpack and included the following file in my project's base directory:

vue.config.js

const webpack = require('webpack')

module.exports = {
    configureWebpack: {
        plugins: [
            new webpack.optimize.LimitChunkCountPlugin({
                maxChunks: 1
            })
        ]
    },
    chainWebpack:
    config => {
        config.optimization.delete('splitChunks')
    }
}

My main objective is to create custom elements using the fantastic module found here: https://github.com/karol-f/vue-custom-element. Everything seems to be functioning as expected, but when I run npm run build, all hrefs in my index.html file are linked to the root directory (href=/css/app.11f77a6e.css), causing the browser to search in locations like:

`file:///C:/css/app.11f77a6e.css`

Is there a way to configure Webpack so that resource links are relative and searched for within the dist folder?

I attempted adding a webpack.config.js file to my project's root directory with the following content:

const path = require('path');

module.exports = {
    output: {
        path: path.resolve(__dirname, 'dist')
    }
}

Unfortunately, this did not solve the issue.

Below is my package.json file:

{
  "name": "build-flow",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build"
  },
  "dependencies": {
    "vue": "^2.5.21",
    "vue-custom-element": "^3.2.6",
    "webpack": "^4.28.4"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.3.0",
    "@vue/cli-service": "^3.3.0",
    "vue-template-compiler": "^2.5.21"
  }
}

Answer №1

When you see file:/// URLs, it indicates that you are not accessing the files through a development server. Chances are you are directly opening the index.html file (e.g., by double-clicking on it). In such cases, you should set up a server in the dist directory and access it from your web browser:

  1. Launch your Terminal and navigate to the <project_root>/dist directory.
  2. Execute python -m SimpleHTTPServer, which will display something like Serving on 0.0.0.0 port 8000. Remember the port number for the next step. (Alternatively, you can use http-server)
  3. Open your web browser and go to http://localhost:8000.

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 approach for deleting an element from an array based on its value

Is there a way to eliminate an element from a JavaScript array? Let's say we have an array: var arr = ['three', 'seven', 'eleven']; I want to be able to do the following: removeItem('seven', arr); I researc ...

execute an ajax request without including any authentication headers

I am looking to create an ajax call with authorization headers only when the user enters a username and password. If these variables are empty, I need to create an ajax call without authorization headers. Is it possible to achieve this using just one aja ...

Server nearby designated to handle requests to the api

Currently, I am working on a project involving automation. Within Adobe CEP, there is a local server that operates on Node.js/Express. My goal is to send an API request from a cloud server to this local server. How can I establish a connection between my l ...

Simultaneously sending jQuery.ajax data while submitting a form

I'm facing a bit of a dilemma here. I have a form with multiple fields, including one for entering links. When you input a link and click the add button, the link is added to a link_array using jQuery. My goal is to send this array through the jQuery. ...

Tips on when to display the "Email Confirmation" input text box only after updating the old email

Oh no!! Yes, that's exactly what I desire! I've been facing obstacles in trying to understand how to display the "Email Confirm" input text-box ONLY when the old email has been updated. Can someone point out where I might have gone wrong? :( ...

Exploring the Integration of Medium Widgets within a React Website

I stumbled upon an incredible link here that generates widgets for Medium posts. Unfortunately, I encountered difficulties when trying to incorporate the code into a react website. For example, for a random Medium author: <div id="medium-widget">& ...

React hook form submit not being triggered

import React, { useState } from "react"; import FileBase64 from "react-file-base64"; import { useDispatch } from "react-redux"; import { makeStyles } from "@material-ui/core/styles"; import { TextField, Select, Input ...

Exploring the Power of Backbone.js with Embedded One-To-Many Relationships

Creating a Survey App Developing an application for creating surveys where each survey contains multiple questions. The questions are embedded into the survey model using embeds_many in Mongoid, resulting in a survey structure like this: { "id": "4f ...

How come my customized directive is not taking precedence over the original methods in AngularJS?

Following the guidelines in the Angular Decorator manual (https://docs.angularjs.org/guide/decorators), I attempted to create a directive and apply decoration to it. The directive's purpose is to display the current date and time. I included a (point ...

Creating custom fields with user input in WordPress widgets

I'm currently working on a theme and I need to create dynamic input fields for a set of predefined labels in a custom widget. My goal is to achieve the following layout: CourseName FieldONE FieldTWO ----------------------------- ...

most efficient method to execute numerous API requests at the same time

Currently, I am developing a backend using expressJS. Imagine that I need to make 10,000 calls to an API consecutively and store the obtained data in a database. What would be the most effective approach for achieving this task? Is it possible that Promis ...

The issue of the Ajax beforeSend function not triggering at times, causing a delay in displaying the bootstrap progress

I'm experiencing issues with my Bootstrap tabs and the progress bar not consistently showing up. I have 3 tabs, each displaying query results in a table. Whenever the search button is clicked or a tab is changed, an ajax call triggers a function with ...

How to properly format JSON responses in Node.js or Express

I came across a question on Proper way to return JSON using node or Express and copied it for reference. I am looking for the response in a specific format. This is the sample format for the response API: { "success":true, "code":200, "message":"Ok", "da ...

Use CSS to position the SVG element to the bottom right corner of the screen

I have a <div> on the page with the CSS class .svgImage applied to it. Whenever I resize the browser window, the svg image resizes correctly but keeps shifting its position on the page. When I make the browser window vertically smaller, the svg imag ...

Using Java code to show a tag on the screen

I attempted to display the label "until" when the user selects "range", but unfortunately, the label did not appear. Below are the codes I used. <td><select onchange="Show(this,'vin','until1');" <option v ...

What could be causing my CSS code to not function properly within Vue.js?

Having trouble with css in VueJs. I can't seem to figure out what I'm doing wrong. Generally, my scoped style css works fine in vuejs, I can target ids and classes without any issues. The problem arises when trying to change the internal css va ...

If the value of an object is found in an array, then include it in the current

I am working on a function component that creates a linear chart in react using data from an array of objects. The API data I am retrieving appears like this: { "_id": "604face09b305032586fe235", "username" ...

Tips for concealing protruding sections of 3D shapes behind intricate 3D models

Currently, I am working on rendering a complex 3D mesh using Three.js (an iliac bone) and including some simple spheres to mark specific points on the surface where muscles would attach. However, I have encountered an issue where the markers protrude out ...

Divide a SINGLE BACKGROUND IMAGE in HTML into two separate links of equal size, one at the top and

As a beginner in HTML, I am trying to find a way to divide a background image into two equal sections without using image mapping. I attempted to split the links by setting the style to 0% and 50% to designate the top and bottom halves, but unfortunately, ...

Working with Angular's forEach method and handling null values

I'm encountering an issue where the array of selected devices is not getting the values when attempting to add multiple devices to a group. Can someone identify the problem or suggest an alternative method? I referred to http://www.dotnetawesome.com/2 ...