繁体   English   中英

如何在Webpack中使用字体和图标?

[英]How to work with fonts and icons in webpack?

我需要为使用reactjs,semantic-ui-react和nucleo图标的项目创建webpack配置。 它几乎可以构建除字体和图标之外的所有内容。 我不太了解如何构建它们,并且在构建后,核图标不会显示在项目中。我的配置:

const path      = require('path');
const webpack   = require('webpack');

const autoprefixer            = require('autoprefixer');
const ExtractTextPlugin       = require('extract-text-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const ASSETS_PATH             = './assets';
const BUILD_DIR               = path.resolve(__dirname, 'build');

var webpack_config = {

    context: path.resolve(__dirname, ASSETS_PATH),

    entry: {
        main           : [
                            "react",
                            "react-dom",
                            "react-props",
                            "redux",
                            "react-redux",
                            "redux-thunk"
                        ],
        module  : "./js/module/index.jsx",
    },

    output: {
        filename: '[name].min.js',
        path: BUILD_DIR + '/js'
    },

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

    devtool: 'inline-source-map',

    module : {
        loaders : [
            {
                test : /\.jsx?/,
                loader : 'babel-loader?compact=true&comments=true&minified=true',
                query: {
                    presets:[
                        'es2015',
                        'react',
                        'stage-1'
                    ]
                },
                exclude: /node_modules/
            },
            {
                test: /\.(woff|woff2|eot|ttf|svg)(\?.*)?$/,
                loader: 'file-loader?name=../css/fonts/[name].[ext]',
                options: {
                    limit: 10000
                }
            },
            {
                test: /\.(png|jpe?g|gif)(\?.*)?$/,
                loader: 'file-loader?name=../css/images/[name].[ext]'
            },
            {
                test: /\.json$/,
                loader: "json-loader"
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            }
        ]
    },


    plugins: [
        new webpack.DefinePlugin({
            "process.env": {
                NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'production')
            }
        }),
        new ExtractTextPlugin({
            filename: "../css/style.min.css",
            disable: false,
            allChunks: true
        }),
        new OptimizeCssAssetsPlugin({
            assetNameRegExp: /\.min\.css$/g,
            cssProcessor: require('cssnano'),
            cssProcessorOptions: { discardComments: { removeAll: true } },
            canPrint: true
        }),
        new webpack.optimize.CommonsChunkPlugin({
            names: ["main"]
        }),
        new webpack.optimize.UglifyJsPlugin({
            minimize  : true,
            sourceMap : false,
            beautify  : false,
            comments  : false,
            compress: {
                warnings: false
            }
        })
    ]
};

module.exports = webpack_config;

因此,我在地图“ js”中获得了js捆绑包,在css地图中获得了css捆绑包style.min.css。 webpack还创建图像映射,并放置jpg,png,svg。 但是他把带有长名称的字体文件(eot,ttf等)放在js映射中。 我应该如何重构我的配置才能解决此问题?

用这样的加载器结构解决了这个问题(也许对某些人有用):

{
                test: /\.(eot|svg|ttf|woff|woff2?)$/,
                use: {
                    loader: 'file-loader'
                    , options: {
                        name: '../css/fonts/[name]-[hash:8].[ext]'
                    }
                }
            },

暂无
暂无

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

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