Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack and VueJs CLI v3 – Need relative path for images and web-fonts

Using: Vue CLI 3.0.0-rc.3

How can I config my app, that it is loading the A) css itself, B) the fonts loaded in css and C) the images from a relative path depending to the parent-folder the app is located?

My complete vue app is currently running without extra webpack config file. I already know, I would need to create a webpack.config.js, but I don't know what plugins or configuration is necessary.

The app is full functional under absolute path http:whatever.local/ of course.

But I need to deliver the app complete independent from absolute path, so customer can use it under folder structure he wants and I don't know jet. http:customerssite.com/i-dont-know-his-structure/vue-app/ (I just don't know).

Thank you very much.

like image 511
Herr_Hansen Avatar asked Jul 18 '18 08:07

Herr_Hansen


People also ask

Does Vue CLI use webpack?

Vue CLI is built on top of Webpack and so for beginners, you do not need to know what Webpack is as the Vue CLI has taken care of the configurations with great default presets and you literally need 0 configs to start.

Is webpack necessary for Vue?

js files using Webpack (see below). However, you can also create Vue components in *. js files, the template that the component uses is then defined in a different file or given as a simple string. In that case, you do not need Webpack.

How does Vue CLI service Build work?

vue-cli-service build produces a production-ready bundle in the dist/ directory, with minification for JS/CSS/HTML and auto vendor chunk splitting for better caching. The chunk manifest is inlined into the HTML.

How do I add a proxy to Vue?

The Vue config API allows us to configure a proxy with the devServer. proxy option. So let's go ahead and add that to the export object. We'll set the value of the proxy to the server URL.


1 Answers

The described situation contains two different problems:

1) Relative Path to assets at all.

To have the web-app functional in every nested folder, you need to load all assets from relative starting point.

Solution: baseUrl = './' (the folder itself, were the html starts loading the vue app.)

Documented here: https://cli.vuejs.org/config/#baseurl

module.exports = {
  baseUrl: './',
}

2) ignore url paths in css-loader

To be able to use relative paths in the urls used in css for images and fonts, you need to avoid that the css-loader (webpack) is trying to manipulate/controll your used urls.

Solution: Configure this css-loader with option url: false. And just use the relative url, that starts from the folder were the html starts loading.

Documented here: https://cli.vuejs.org/guide/css.html#css-modules

 module.exports = {
    baseUrl: './',
       css: {
         loaderOptions: {
            css: {
               url: false,
              }
          }
      }
  }
like image 200
Herr_Hansen Avatar answered Sep 28 '22 09:09

Herr_Hansen