webpack配置代码-切换生产模式和开发模式

这篇博客介绍了如何在Webpack中配置切换生产模式和开发模式。通过创建webpack.config.base.js,webpack.config.dev.js和webpack.config.prod.js文件,将公共配置、开发模式配置和生产模式配置分别分离。在package.json的脚本中定义命令,使得运行特定命令可以启动对应的模式。
摘要由CSDN通过智能技术生成

切换生产模式和开发模式

创建webpack.config.prod.js文件和webpack.config.base.js文件。将公共配置放在webpack.config.base.js文件中,将开发模式配置放在webpack.config.js中,将生产模式放在webpack.config.prod.js中(代码如下)。在package.json的build中添加 --config webpack.config.prod.js当运行在命令行运行 yarn start 时为开发模式;当在命令行运行 yarn build 为生产模式。
在这里插入图片描述

webpack.config.js文件

const path = require('path');

const base = require('./webpack.config.base.js')

module.exports = {
  ...base,
  mode: 'development',
  devtool: 'inline-source-map',
  devServer: {
    contentBase: './dist',
  },
  optimization: {
    runtimeChunk: 'single',
  },
  module: {
    rules: [
      ...base.module.rules,
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      },
    ],
  },
};

webpack.config.prod.js文件

const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const base = require('./webpack.config.base.js')

module.exports = {
  ...base,
  mode: 'production',
  plugins: [
    ...base.plugins,
  new MiniCssExtractPlugin({
    filename: '[name].[contenthash].css',
    chunkFilename: '[id].[contenthash].css',
    ignoreOrder: false,
  }),
],
  module: {
    rules: [
      ...base.module.rules,
      {
        test: /\.css$/i,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: {
              publicPath: "/public/path/to/",
            },
          },
          "css-loader",
        ],
      },
    ],
  },
  optimization: {
    runtimeChunk: 'single',
  },
};

webpack.config.base.js文件

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

module.exports = {
  entry: './src/index.js',
  output: {
    filename: '[name].[contenthash].js',
  },
  plugins: [
    new HtmlWebpackPlugin({
    title: 'My App',
    template: 'src/assets/index.html'
  }),
  ],
  module: {
    rules: [
      {
        test: /\.less$/i,
        use: [
          // compiles Less to CSS
          'style-loader',
          'css-loader',
          'less-loader',
        ],
      },
      {
        test: /\.s[ac]ss$/i,
        use: [
          // 将 JS 字符串生成为 style 节点
          'style-loader',
          // 将 CSS 转化成 CommonJS 模块
          'css-loader',
          // 将 Sass 编译成 CSS
          'sass-loader',
        ],
      },
      {
        test: /\.styl$/,
        loader: ['style-loader','css-loader','stylus-loader'] // 将 Stylus 文件编译为 CSS
      },
      {
        test: /\.(png|jpg|gif)$/i,
        use: ['file-loader'],
      },
    ],
  },
};

package.json

{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack serve",
    "build": "rm -rf dist && webpack --config webpack.config.prod.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@webpack-cli/serve": "^1.7.0",
    "css-loader": "^3.2.0",
    "file-loader": "^6.2.0",
    "html-webpack-plugin": "^3.2.0",
    "less": "^4.1.3",
    "less-loader": "5.0.0",
    "mini-css-extract-plugin": "1.6.2",
    "sass": "^1.54.9",
    "sass-loader": "^10",
    "style-loader": "^1.0.1",
    "stylus": "^0.59.0",
    "stylus-loader": "3.0.2",
    "webpack": "^4.41.2",
    "webpack-dev-server": "^3.9.0"
  },
  "dependencies": {
    "webpack-cli": "^4.10.0"
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值