webpack(五)常用打包配置示例

  • package.json
{
  "name": "webpack-domo",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "dependencies": {
    "clean-webpack-plugin": "^3.0.0",
    "css-loader": "^5.0.1",
    "file-loader": "^6.2.0",
    "html-loader": "^1.3.2",
    "markdown-loader": "^6.0.0",
    "style-loader": "^2.0.0",
    "url-loader": "^4.1.1",
    "webpack": "^5.11.0"
  },
  "devDependencies": {
    "html-webpack-plugin": "^4.5.0",
    "mini-css-extract-plugin": "^1.3.3",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack",   //打包生成dist命令
    "dev": "webpack-dev-server"    //只编译到内存中快速访问命令
  },
  "author": "",
  "license": "ISC"
}
  • webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
    mode: 'development',
    devtool: 'source-map',
    entry: {
        index: './src/index.js'
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: './js/[name].js'
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    // { loader: "style-loader" },
                    // 生成link
                    { loader: MiniCssExtractPlugin.loader },
                    {
                        loader: "css-loader",
                        options: {
                            // 启用/禁用 url() 处理 
                            url: true,
                            // 启用/禁用 @import 处理 
                            import: true,
                            // 启用/禁用 Sourcemap 
                            sourceMap: false
                        }

                    }
                ]
            },
            {
                test: /\.(png|jpe?g|gif)$/,
                use: {
                    loader: "url-loader",
                    options: {
                        // placeholder 占位符 [name] 源资源模块的名称
                        // [ext] 源资源模块的后缀
                        name: "[name]_[hash].[ext]",
                        //打包后的存放位置
                        outputPath: "./images",
                        // 打包后文件的 url
                        // 尽量使用静态资源(网站)的根目录地址
                        publicPath: '/images',
                        limit: 100
                    }
                }
            },
            {
                test: /\.md$/,
                use: [
                    'html-loader',
                    'markdown-loader'
                ]
            }
        ]
    },
    // 插件的执行顺序与书写顺序没有关系
    plugins: [new HtmlWebpackPlugin({
        template: './template/index.html',
        filename: 'index.html',
        title: "欢迎学习webpack"
    }), new CleanWebpackPlugin(), new MiniCssExtractPlugin({
        filename:'./css/[name].css'
    })],
    devServer: {
        contentBase: "./dist",
        // 自动开启浏览器
        open: true,
        // 即使 HMR 不生效,也不去刷新整个页面(选择开启)
        hotOnly: true,
        // 开启热更新
        hot:true,
        // 代理
        proxy: {
            '/api': {
                target: 'http://localhost:9999',
                pathRewrite: {
                    '^/api': ''
                }
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值