如何在webpack生产中获取图像?

时间:2017-08-02 00:31:56

标签: webpack gulp gruntjs webpack-3 webpack-production

生产文件夹中没有图像。是否有任何简单的方法来解决webpack配置中的问题?

packaage.json

"scripts": {
  "start": "npm run build",
  "build": "webpack -d && cp src/index.html dist/index.html && webpack-dev-server --content-base src/ --inline --hot --history-api-fallback",
  "build:prod": "webpack -p && cp src/index.html dist/index.html"
},

webpack.config.js

const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

const extractPlugin = new ExtractTextPlugin({
    filename: '[name].css'
});
const DIST_DIR = path.resolve(__dirname, 'dist');
const SRC_DIR = path.resolve(__dirname, 'src');

const config = {
    entry: SRC_DIR + '/App.js',
    output: {
        path: DIST_DIR,
        filename: 'bundle.js'
    },
    module: {
        loaders: [
            {
                test: /\.jsx?/,
                exclude: /node_modules/,
                include: SRC_DIR,
                loader: 'babel-loader',
                query:{
                    presets: ['es2015']
                }
            },
            {
                test: /\.scss$/,
                use: extractPlugin.extract({
                    use: [
                        {
                            loader: 'css-loader',
                        },
                        {
                            loader: 'postcss-loader',
                            options: {
                                plugins: (loader) => [
                                    require('autoprefixer')({
                                        browsers: ['last 2 version']
                                    }),
                                    require('postcss-flexbugs-fixes')(),
                                    require('css-mqpacker')
                                ]
                            }
                        },
                        {
                            loader: 'sass-loader',
                        }
                    ]
                })
            },
            {
                test: /\.(jpeg|png)$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: '[name].[ext]',
                            outputPath: 'img/',
                            publicPath: 'img/'
                        }
                    }
                ]
            },
            {
                test: /\.(woff2?|svg)$/,
                use: 'url-loader?limit=10000&name=fonts/[name].[ext]'
            },
            {
                test: /\.html$/,
                use: ['html-loader']
            }
        ]
    },
    plugins:[
        extractPlugin,
        new CleanWebpackPlugin(['dist'])
    ]
};

module.exports = config;

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您在应用程序代码中未使用require('image.jpg');,则webpack将无法解析它(因此它不会移动到您的dist文件夹中)。如果您使用url()在css文件中需要图像,则需要在webpack中包含url-loader来解析它。 See more