webpack整理开发模式以及生产模式的配置文件

整理开发模式以及生产模式的配置文件

首先在项目根目录下新建一个config文件夹

然后将webpack.config.js配置文件移动到该文件夹中并修改名称为webpack.dev.js

然后将webpack.dev.js复制一份也放在这个文件夹中并重命名为webpack.prod.js(生产模式的配置)

修改开发模式的配置文件内容为:

  • 修改绝对路径地址
  • 将output中的path删掉,开发模式不需要输出文件
const path = require('path')
const ESLintPlugin = require('eslint-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports =  {
    entry: './src/main.js',
    output: {
        filename: 'static/js/main.js',
        clean: true
    },
    module: {
        rules: [
            {
                test: /\.css$/, // 正则表达式,用于匹配需要加载的文件
                use: [ "style-loader", "css-loader"] // 使用的加载器插件模块,从右向左调用插件
            },
            {
                test: /\.png$/,
                type: "asset",
                parser: {
                    dataUrlCondition: {
                        maxSize: 10 * 1024
                    }
                },
                generator: {
                    filename: "static/images/[hash][ext][query]"
                }
            },
            {
                test: /\.(ttf|woff2?)$/,
                type: "asset/resource",
                generator: {
                    filename: "static/media/[hash:10][ext][query]"
                }
            },
            {
                test: /\.js$/,
                // exclude排除node_modules文件,即不对该文件夹内的文件进行处理
                exclude: /node_modules/,
                use: { loader: 'babel-loader' }
            }
        ]
    },
    plugins: [
        new ESLintPlugin({
            context: path.resolve(__dirname, '../src')
        }),
        new HtmlWebpackPlugin({
            // 指定html模板,会保留该模板的结构
            // 依据该模板创建新的html文件
            template: path.resolve(__dirname, '../public/index.html')
        })
    ],
    devServer: {
        host: 'localhost',
        port: 3000,
        open: true
    },
    mode: 'development'
}

修改生产模式配置文件内容为:

  • 修改绝对路径地址
  • 删除devServer对象
  • 将mode值修改为production
const path = require('path')
const ESLintPlugin = require('eslint-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports =  {
    entry: './src/main.js',
    output: {
        path: path.resolve(__dirname, '../dist'),
        filename: 'static/js/main.js',
        clean: true
    },
    module: {
        rules: [
            {
                test: /\.css$/, // 正则表达式,用于匹配需要加载的文件
                use: [ "style-loader", "css-loader"] // 使用的加载器插件模块,从右向左调用插件
            },
            {
                test: /\.png$/,
                type: "asset",
                parser: {
                    dataUrlCondition: {
                        maxSize: 10 * 1024
                    }
                },
                generator: {
                    filename: "static/images/[hash][ext][query]"
                }
            },
            {
                test: /\.(ttf|woff2?)$/,
                type: "asset/resource",
                generator: {
                    filename: "static/media/[hash:10][ext][query]"
                }
            },
            {
                test: /\.js$/,
                // exclude排除node_modules文件,即不对该文件夹内的文件进行处理
                exclude: /node_modules/,
                use: { loader: 'babel-loader' }
            }
        ]
    },
    plugins: [
        new ESLintPlugin({
            context: path.resolve(__dirname, '../src')
        }),
        new HtmlWebpackPlugin({
            // 指定html模板,会保留该模板的结构
            // 依据该模板创建新的html文件
            template: path.resolve(__dirname, '../public/index.html')
        })
    ],
    mode: 'production'
}

然后在package.json中修改scripts

"scripts": {
	"serve": "npx webpack --config config/webpack.dev.js",
	"build": "npx webpack --config config/webpack.prod.js"
}

最后分别运行npm run serve以及npm run build测试命令是否能执行成功

npm run serve将会打开浏览器

npm run build将会在项目根目录下生成一个dist文件夹

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值