url-loader为什么没有生成base64的图片

来源:4-7 处理图片以及其他文件

hudy

2018-02-08 16:59

/*
* @Author: Marte
* @Date:   2018-02-06 14:41:11
* @Last Modified by:   Marte
* @Last Modified time: 2018-02-08 16:51:22
*/
var HtmlWebpackPlugin = require('html-webpack-plugin');
var path = require('path');
module.exports = {
    entry:'./src/app.js',
    output:{
        path:__dirname+'/dist',
        filename:'js/[name].bundle.js'
    },
    module:{
        rules: [
            {test: /\.css$/,
                use: ['style-loader', 'css-loader', {
                    loader: 'postcss-loader',
                    options: {
                        plugins: [
                            require("autoprefixer")({browsers: ["last 5 versions"]})
                        ]
                    }
                }]
            },
            {test:/\.less$/,
                use:['style-loader','css-loader',{
                    loader:'postcss-loader',
                    options:{
                        plugins:[
                            require('autoprefixer')({
                                browsers:['last 5 versions']
                            })
                        ]
                    }
                },
                'less-loader'
                ]
            },
            {test:/\.html$/,loader:'html-loader'},
            {test:/\.tpl$/,loader:'tpl-loader'},
            {test:/\.(jpg|png|gif|svg)$/i,
                use:[
                    {
                        loader:'url-loader',
                        options:{
                            limit:20000,
                            name:'assets/[name]-[hash:5].[ext]'
                        }
                    }
                ]
            }
        ],
        loaders: [{
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: path.resolve(__dirname, 'node_modules'),
            include: '/src/',
            query: {
                presets: ['es2016']
            }
        }]
    },
    plugins:[
        new HtmlWebpackPlugin({
            filename:'index.html',
            template:'index.html',
            inject:'body',
            title:'this is index.html'
        })
    ]
}

写回答 关注

1回答

  • hudy
    2018-02-08 17:04:39

    limit的值需要根据你引入图片的大小来设定,我的图片时44.5,我设置为20000是没有任何效果也没有报错,所以我修改为50000就能正常转为base64的图片了

    罐头冠

    谢谢,原来如此

    2018-04-04 00:01:29

    共 1 条回复 >

webpack深入与实战

webpack实战教程,用真实项目带你探索 webpack 强大的功能

86561 学习 · 721 问题

查看课程

相似问题