const {resolve} = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    mode: 'production',
    entry: './src/js/index.js',
    output: {
        //mode:'none',//developement,production
        filename: './js/bundle.js',
        path: resolve(__dirname, 'dist')
    },
    module: {
        rules: [
            {
                //webpack 根据正则表达式,来确定应该查找哪些文件,并将其提供给指定的 loader。在这种情况下,以 .css 结尾的全部文件,都将被提供给 style-loader 和 css-loader。
                // .在正则中需要转义 \.
                test: /\.css+$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test:/\.less$/,
                use:[
                    'style-loader',
                    'css-loader',
                    'less-loader'
                ]
            },
            {
                test: /\.(png|jpg|gif|svg)$/,
                loader: 'url-loader',
                options:{
                    name: '[name]_[hash:10].[ext]',
                    limit: 8 * 1024,
                    //指定输出文件
                    outputPath: 'images',
                    esModule: false
                }
            },
            //html-loader 是处理 html 的 img url,然后给 url-loader 处理的
            {
                test: /\.html$/,
                loader: 'html-loader'
            },
            //其他资源
            {
                exclude: /\.(js|css|less|img|jpg|png|gif|html)$/,
                loader: 'file-loader',
                options: {
                    name: '[name]_[hash:10].[ext]',
                    //指定输出文件
                    outputPath: 'media'
                }
            }
        ]
    },
    plugins:[
        new HtmlWebpackPlugin({
            template:'./src/index.html'
        })
    ],
    mode:'development',
    devServer:{
        contentBase: resolve(__dirname,'dist'),
        //当它被设置为true的时候对所有的服务器资源采用gzip压缩
        //优点:对JS,CSS资源的压缩率很高,可以极大得提高文件传输的速率,从而提升web性能
        //缺点:服务端要对文件进行压缩,而客户端要进行解压,增加了两边的负载
        compress: true,
        open: true,
        port: 8080,
    }
}

 

指定outputPath 可以构造整齐的目录

 

posted on 2020-12-04 12:04  京鸿一瞥  阅读(138)  评论(0)    收藏  举报