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

自古美人都是妖i 提交于 2021-02-18 13:55:41

问题


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.


回答1:


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,
              }
          }
      }
  }



回答2:


You can play with baseUrl and assetsDir parameters



来源:https://stackoverflow.com/questions/51396880/webpack-and-vuejs-cli-v3-need-relative-path-for-images-and-web-fonts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!