javascript - Webpack - 样式表加载但没有字体

标签 javascript css fonts sass webpack

我可以毫无问题地在页面中查看我的样式表。但是我无法让我的网络字体工作。我试图保存我的 css 的输出,但没有发生。我相信这就是字体无法正常工作的原因。

网页包

var webpack = require ('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {

    resolve: {
        extensions: ['', '.js']
    },

    entry: ['webpack-hot-middleware/client','./src/client/js/Kindred.js'],

    output: {
        path: './public',
        filename: 'bundle.js',
        publicPath: '/public/js'
    },

    devtool: 'cheap-module-source-map',
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                query: {presets: ['es2015', 'react', 'react-hmre', 'stage-0']}
            },
            {test: /\.scss$/, loaders: [
                'style?sourceMap&modules',
                'css?sourceMap&modules&importLoaders=1&localIdentName=[path]___[name]__[local]___[hash:base64:5]',
                'resolve-url',
                'sass?sourceMap&modules']},
            { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader?modules!css-loader?sourceMap&modules" )},
            {test: /\.png$/, loader: "url-loader?limit=100000"},
            {test: /\.jpg$/, loader: "file-loader"},
            {
                test: /\.(eot|svg|ttf|woff|woff2)$/,
                loader: 'file?name=public/font/[name].[ext]'
            }
        ]
    },

    sassLoader: {
        includePaths: [ 'src/client/scss' ]
    },

    plugins: process.env.NODE_ENV === 'production' ? [
        new ExtractTextPlugin ('app.css', {allChunks: true}),
        new webpack.optimize.DedupePlugin (),
        new webpack.optimize.OccurrenceOrderPlugin (),
        new webpack.optimize.UglifyJsPlugin ()
    ] :  [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        new ExtractTextPlugin("[name].css")
    ]

};

字体.scss

@font-face {
  font-family: 'fontello';
  src: url('/public/font/fontello.eot?42978343');
  src: url('/public/font/fontello.eot?42978343#iefix') format('embedded-opentype'),
       url('/public/font/fontello.woff2?42978343') format('woff2'),
       url('/public/font/fontello.woff?42978343') format('woff'),
       url('/public/font/fontello.ttf?42978343') format('truetype'),
       url('/public/font/fontello.svg?42978343#fontello') format('svg');
  font-weight: normal;
  font-style: normal;
}

扩建

Hash: 6dbe5412ed2de3ad1f84
Version: webpack 1.13.1
Time: 5989ms
                                   Asset      Size  Chunks             Chunk Names
                               bundle.js    2.2 MB       0  [emitted]  main
    0.4dfc2adf9da9e1d82440.hot-update.js    402 kB       0  [emitted]  main
    4dfc2adf9da9e1d82440.hot-update.json  36 bytes          [emitted]  
                           bundle.js.map   2.51 MB       0  [emitted]  main
0.4dfc2adf9da9e1d82440.hot-update.js.map    419 kB       0  [emitted]  main
chunk    {0} bundle.js, 0.4dfc2adf9da9e1d82440.hot-update.js, bundle.js.map, 0.4dfc2adf9da9e1d82440.hot-update.js.map (main) 2.08 MB [rendered]
  [565] ./~/css-loader?sourceMap&modules&importLoaders=1&localIdentName=[path]___[name]__[local]___[hash:base64:5]!./~/resolve-url-loader!./~/sass-loader?sourceMap&modules!./src/client/scss/main.scss 401 kB {0} [built]
     + 565 hidden modules
webpack: bundle is now VALID.

文件夹结构

enter image description here

HTML

<!doctype html public="storage">
<html>
<link rel='stylesheet' href='/public/styles.css' type='text/css' />
<title>MyKindred.com</title>
<div id=app></div>
<script src="/public/js/bundle.js"></script>

最佳答案

由于 Sass 不提供 url 重写,使用 url() 有点 tricky .一个简单的修复方法是使用条目文件的相对路径。

@font-face {
  font-family: 'fontello';
  src: url('../font/fontello.eot?42978343');
  src: url('../font/fontello.eot?42978343#iefix') format('embedded-opentype'),
       url('../font/fontello.woff2?42978343') format('woff2'),
       url('../font/fontello.woff?42978343') format('woff'),
       url('../font/fontello.ttf?42978343') format('truetype'),
       url('../font/fontello.svg?42978343#fontello') format('svg');
  font-weight: normal;
  font-style: normal;
}

关于javascript - Webpack - 样式表加载但没有字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38516601/

相关文章:

javascript - 如何在将元素插入数组时使用三元运算符

javascript - 如何使大图像不滚动?

html - 无法找出带换行符的正则表达式 - HTML

html - 如何让 CSS 三 Angular 形显示在一行而不是垂直

css - 智能手机的字体和字体大小单位

javascript - 如何在 js 文件中轻松找到所需的 jquery 函数?

javascript - 如何从 Protractor 返回对象

CSS - 选择没有第一列的行表

css - 如何在电子邮件模板中使用自定义字体

java - SWT 中的字形指标?