Solving the image path issue in a Vue Component using webpack (inside Vuepress)

Incorporating the most recent Vuepress version (1.0.0-alpha.46), I have set up the documentation off the root directory with an assets folder for image storage.

Referencing these images in markdown poses no issues. For example:

![ ](../assets/foobar.jpg)

This works smoothly despite Webpack automatically adding an alias to the image, such as assets/foobar.57589334.jpg. However, complications arise when utilizing a Vue component within Vuepress. In this scenario, I simply insert the following into my markdown file:

this is some markdown
<zoom-image src="../assets/foobar.jpg" />

Now, I receive the raw string without Webpack's postfix inclusion. While one potential solution might involve relocating the image to .vuepress/public, it feels incorrect and could lead to unwanted caching by the service worker. The documentation discusses configuring webpack with aliases, prompting me to attempt defining this in the .vuepress/config.js file:

configureWebpack: {
  resolve: {
    alias: {
      "@images": "assets"
    }
  }
},

Following this adjustment, the markdown appears as:

this is some markdown
<zoom-image src="~@images/foobar.jpg" />

While no errors occur, unsurprisingly, the raw string is once again passed into my component. Exploring options like importing exports from webpack to enforce image name transformation has not yielded success. Can anyone provide assistance?

Answer №1

Hey there! If I understand correctly, my solution would be something like this: the configuration in config.js is:

configureWebpack: {
  resolve: {
    alias: {
      "@assets": path.resolve(__dirname, '../assets')
    }
  }
},

My file structure looks like this:

|-- .vuepress
|   |-- config.js
|-- assets
|   |-- icon.png
|-- guide
|   |-- 1.md
|   |-- 2.md
|   |-- 3.md
|   |-- 4.md

Using the alias like this: ![icon](~@assets/icon.png)

Answer №2

If you're having trouble getting it to work using configureWebpack, consider trying the chainWebpack approach instead.

const path = require("path"); // Remember this

module.exports = {
  chainWebpack: config => {
    config.resolve.alias.set('@assets', path.resolve(__dirname, "../assets"))
  }
}

This is how my structure looks:

|-- .vuepress
|   |-- config.js
|-- assets
|   |-- foobar.jpg

You can create an alias like

![My foobar](~@assets/foobar.jpg)

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

Why doesn't the Vuex store update the data property of Vue components?

Within the code snippet provided, there seems to be an issue where the c2 property of the component (app.vue) does not update when the increment function updates the counter in the store using this.$store.state.counter++; I am aware that this can be resol ...

What is the best way to implement date range filtering in vue js?

Currently, I am delving into the realm of vue.js and experimenting with a filtering feature that involves date ranges. The process goes like this: initially filter by type, then proceed to filter by a specified date range, consisting of start and end dat ...

Preventing FOUC when navigating away from a vue-flickity carousel/slider - the ultimate guide!

Currently, I've integrated the vue-flickity package by MetaFizzy's Flickity into my Vue application. Upon navigating away from a route containing a vue-flickity slider instance, there is a brief flash of unstyled slides visible in the document wh ...

Utilize the clearChart() function within Google charts in conjunction with vue-google-charts

I have integrated vue-google-charts to display various charts on my website. I want to allow users to compare different data sets, by enabling them to add or delete data from the chart. In order to achieve this functionality, I need to find a way to clear ...

Generate random floating numbers at intervals and calculate their sum

I've been given a task to complete. Upon page load, there should be 10 fields labeled as A, B, C, D ... each with the initial value of 3. After the page has loaded, every 2 seconds all field values should change randomly. The change will be a rand ...

Encountered an issue with locating the module 'webpack-cli/bin/config-yargs' while attempting to run webpack-dev

Encountering an error while trying to start the webpack dev server with the command provided below. Despite suggestions that it could be due to outdated webpack versions, I am confident that all components are up to date: [email protected] [email ...

Integration Between AppEngine and GitHub

Seeking assistance - encountering an issue with my Github account. It appears to be flagged and the files are not updating following commits. The trigger seems to be working correctly, but the actual files remain outdated. How can this problem be resolve ...

Creating dynamic VueFire references is a powerful feature that allows for more flexibility and customization

I am currently exploring the creation of refs dynamically: The initial ref I created works fine as it is hardcoded, but the second one does not seem to work because it is dynamic: firebase: function(){ return { categories: db.ref('categ ...

Problem with JWT authentication causing SockJS handshake to block WebSocket connection attempts

I have an operational Spring Boot server with Authentication/Authorization features. However, I am facing issues when trying to establish a connection with SockJS due to my security protocols blocking it. Although I do not have a complete understanding of ...

The loading spinner in the UI of Nuxt 3 app is not appearing correctly

After successfully loading the data, there is an issue where the loader does not appear to indicate to the user that the data is being loaded. Interestingly, when trying to conditionally display the loader using v-if="!loader.value", it appears c ...

Issue with Full-Calendar Vue refetching events; no visible changes occur

Attempting to refresh the calendar, but no changes seem to take effect. updatePlan () { //let calendarApi = this.$refs.fullCalendar.getApi() this.$refs.fullCalendar.$emit('refetch-events');//no visible changes are occurring } ...

Despite being present in the node_modules folder, the ag-grid-vue module appears to be missing

Currently, I am diligently following the Vue.js AgGrid getting started tutorial step by step: https://www.ag-grid.com/vuejs-grid/ However, upon adding the <script> section and saving my progress, an error promptly appears: ERROR Failed to compile ...

Using boolean .env variables in combination with v-if conditionals

I'm currently in the process of migrating an old vue2 project to vue3. I've initiated a vite project for this purpose. Within my component, there's a div with a v-if condition that controls the display of certain buttons based on the environ ...

How can you effectively blend Vue/Angular with other JavaScript resources to enhance your development process?

My curiosity lies in understanding how front-end javascript libraries such as Vue and Angular can seamlessly integrate with other libraries and assets. For instance, if I were to procure a website template already equipped with additional javascript, is it ...

Looking to access XML file data using Vue.js on the server side?

I am trying to access an XML file located in a specific path on the server side using VueJS without using jQuery. Can you provide me with some ideas or advice? Scenario: 1. The file is stored at /static/label/custom-label.xml. 2. I need to read this file ...

A step-by-step guide on how to capture user input in Vue.js after the "

Is there a way to capture the character entered after a key is pressed or after the user presses the enter key? I tried using the npm package npm i vue-keypress, but I couldn't figure out how to capture the characters. Can you suggest another method ...

What causes elements to transition in from the top-left corner when using TransitionGroup in Vue 3?

I'm currently working on adding animations to a small component that consists of a title, an image, and a set of buttons. The animation rules are as follows: Initially, only the title and the image are visible, On hover, the title disappears upwards ...

The content of the string is not appearing

I've hit a snag while trying to create components and templates. I'm closely following the instructions provided in this link: My code is very similar to the one shown in the instructional video (except for the error, of course). The issue I&ap ...

Could an average user potentially access a hidden button meant only for management by examining a website and discovering how to activate it?

Create a website catering to both regular users and management. For instance, normal users are identified as 1, so if their account ID is also 1, they can access a page with limited functionality. Similarly, management is identified as 2, and they too acce ...

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 ...