简体   繁体   English

Webpack-Production您如何启用不同的文件路径 <link href=“”> 和css文件中的图像路径

[英]Webpack - Production how do you enable different file paths for <link href=“”> and image paths in css files

I will explain in more detail. 我将更详细地解释。

    module.exports = {
  build: {
    env: require('./prod.env'),
    index: path.resolve(__dirname, '../dist/index.html'),
    assetsRoot: path.resolve(__dirname, '../dist/'),
    assetsSubDirectory: 'assets',
    assetsPublicPath: './',
    productionSourceMap: true,
  }
}

Above is my config setup for my webpack build. 上面是我的webpack构建的配置设置。 This works fine for the paths required for embedding assets such as CSS files into my index.html file. 这对于将资产(例如CSS文件)嵌入到我的index.html文件中所需的路径来说效果很好。 As seen in the below code:- 如以下代码所示:

<link href=./assets/css/app.345e4523874bd70b2f24b4356d30ed9e.css rel=stylesheet>

But when looking at app.345e4523874bd70b2f24b4356d30ed9e.css of course webpack has built all the paths for images and fonts to be constructed like as follows:- 但是当查看app.345e4523874bd70b2f24b4356d30ed9e.css时,webpack当然已经构建了图像和字体的所有构建路径,如下所示:

./assets/img/home-bg.jpg

Problem is I need to change all CSS related asset paths to ../img or ../fonts. 问题是我需要将所有与CSS相关的资产路径更改为../img或../fonts。

I have tried so many things and looked online but can't seem to find a solution!? 我已经尝试了很多事情,并且在网上看了一下,但似乎找不到解决方案!? It must be so simple. 它必须是如此简单。

Here is my folder structure. 这是我的文件夹结构。 Thanks 谢谢

css img js index.html CSS img js index.html

just solved this by adding 'publicPath '../' to 刚刚通过添加'publicPath'../'来解决这个问题

{
    test: /\.(jpg|png|jpe?g|gif|svg)(\?.*)?$/,
    loader: 'url-loader',
    options: {
      limit: 10000,
      name: utils.assetsPath('img/[name].[hash:7].[ext]'),
      publicPath: '../'
    }
  },
  {
    test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
    loader: 'url-loader',
    options: {
      limit: 10000,
      name: utils.assetsPath('fonts/[name].[hash:7].[ext]'),
      publicPath: '../'
    }

Hope this helps someone else 希望这对别人有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM