简体   繁体   English

Webpack 2 CSS的字体和图像路径错误

[英]Webpack 2 CSS with wrong path to font and images

I'm having a problem with my configuration of WebPack2, because it places the font and images in the directories I want, but it makes the CSS urls point to another directory.. 我在配置WebPack2时遇到问题,因为它会将字体和图像放在我想要的目录中,但是却使CSS网址指向另一个目录。

My Original CSS (SASS after being compiled by compass) and fonts have the following structure 我的原始CSS(由指南针编译后为SASS)和字体具有以下结构 CSS / FONT结构

The font-face here are correct: 此处的字体是正确的:

@font-face {
 font-family: 'FS Albert Web Regular';
 src: url("../fonts/FSAlbertWeb-Regular.eot");
 src: url("../fonts/FSAlbertWeb-Regular.eot?#iefix") format("embedded-opentype"), url("../fonts/FSAlbertWeb-Regular.woff2") format("woff2"), url("../fonts/FSAlbertWeb-Regular.woff") format("woff");
 font-weight: normal;
 font-style: normal;
}
.
.
.

The javascript where I require my css is located here 我需要CSS的javascript位于此处

需要JS

and has the css require: 并且具有CSS要求:

require("../../css/app.css");

My Webpack configuration is like this 我的Webpack配置是这样的

var PRODUCTION = process.env.NODE_ENV === "production";

const cssIdentifier = PRODUCTION
  ? '[hash:base64:10]'
  : '[path][name]--[local]';

const cssLoader = PRODUCTION
    ? ExtractTextPlugin.extract({
        fallback: "style-loader",
        publicPath: "",
        use: 'css-loader?localIdentName=' + cssIdentifier
    })
    : [
      { loader: 'style-loader' },
      {
          loader: 'css-loader?localIdentName=' + cssIdentifier,
          options: { sourceMap: true }
      }
    ];

module.exports = {
    entry: {
        "header": './assets/campus/common/js/_deployment/header.js'
    },
    output: {
        path: path.resolve(__dirname, './assets/dist'),
        publicPath: '/assets/dist',
        filename: '[name].bundle.js',
        chunkFilename: "[name].commons.js"
    },
    module: {
        rules: [
        {
           test: /\.vue$/,
           loader: 'vue-loader',
           options: {
              // vue-loader options go here
           }
        },
        {
           test: /\.js$/,
           loader: 'babel-loader',
           exclude: /node_modules/
        },
        {
           test: /\.(eot|svg|ttf|woff|woff2)$/,
           loader: 'file-loader',
           options: {
              name: './font/[name].[ext]'
           },
           exclude: /node_modules/
        },
        {
           test: /\.(png|jpg|gif|svg)$/,
           loader: 'file-loader',
           options: {
              name: './img/[name].[ext]?[hash]'
           }
       },
       {
           test: /\.css$/,
           use: cssLoader
       }
    ]
}

My files are copied where I want them: 我的文件被复制到我想要的位置:

结果

But my fonts (and my Images) have now a wrong Url (It doesn't go up on level in the structure) 但是我的字体(和我的图像)现在有一个错误的网址(它在结构中没有上升)

@font-face{
    font-family:FS Albert Web Regular;
    src:url(./font/FSAlbertWeb-Regular.eot);
    src:url(./font/FSAlbertWeb-Regular.eot?#iefix) format("embedded-opentype"),url(./font/FSAlbertWeb-Regular.woff2) format("woff2"),url(./font/FSAlbertWeb-Regular.woff) format("woff");
    font-weight:400;
    font-style:normal}

What am I doing wrong? 我究竟做错了什么?

Thanks for the help 谢谢您的帮助

Just in case someone stumbles on it.... I had to change the 以防万一有人偶然发现...。我不得不改变

publicPath: "" 

of my cssloader to 我的cssloader

publicPath: '/assets/dist'

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

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