春风得意马蹄疾
一日看尽长安花

Webpack构建react单页面项目配置

安装依赖

  "devDependencies": {
    "@babel/core": "^7.12.3",
    "@babel/preset-env": "^7.12.1",
    "@babel/preset-react": "^7.12.1",
    "babel-loader": "^8.1.0",
    "clean-webpack-plugin": "^3.0.0",
    "css-loader": "^5.0.0",
    "html-webpack-plugin": "^4.5.0",
    "mini-css-extract-plugin": "^1.2.0",
    "node-sass": "^4.14.1",
    "sass-loader": "^10.0.4",
    "style-loader": "^2.0.0",
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0",
    "webpack-merge": "^5.2.0"
  },
  "dependencies": {
    "react": "^17.0.1",
    "react-dom": "^17.0.1"
  }

编译react

  1. 安装@babel/preset-react
  2. 配置babel的预设,加上@babel/preset-react

基础配置(webpack.base.config.js)

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');//html模板
module.exports = {
  entry: './src/main.js',//入口文件
  output: {
    filename: 'js/bundle.js',//输出文件
    path: path.join(__dirname, '../dist'),
    publicPath: '/'
  },

  plugins: [
    new HtmlWebpackPlugin({
      template: path.join(__dirname, '../public/index.html'),
      filename: 'index.html',
      title: '首页'
    }),
  ],

  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,//使用babel-loader插件解析js和jsx文件
        loader: 'babel-loader',
        exclude: /node_modules/,//忽略node-modules中的js文件
        options: {
          plugins: [],
          presets: [//预设
            '@babel/preset-env',//将es6以上语法转为es5语法
            "@babel/preset-react"//将react语法转为es5语法
          ]
        }
      }
    ]
  },

  resolve: {
    // 可以省略的文件后缀
    extensions: ['.js', '.json', '.jsx'],
    // 路径别名
    alias: {
      '@': path.join(__dirname, '../src'),
      '@c': path.join(__dirname, '../src/components'),
      '~c': path.join(__dirname, '../src/components'),
    }
  }

}

开发环境配置(webpack.dev.config.js)

const path = require("path");
const baseConfig = require("./webpack.base.config");
const {merge} = require('webpack-merge');
module.exports = merge(baseConfig, {
  mode: "development",
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.scss$/,
        use: ['style-loader', 'css-loader', 'sass-loader']
      },
      {
        test: /\.(jpg|jpeg|png|gif|svg|webp)$/,
        loader: 'file-loader'
      }
    ]
  },
  devServer: {
    open: true,
    contentBase: path.join(__dirname, "./dist"),
  },
});

生产环境配置(webpack.pro.config.js)

const { merge } = require("webpack-merge");
const baseConfig = require("./webpack.base.config");
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = merge(baseConfig, {
  mode: "production",
  plugins: [
    new CleanWebpackPlugin(),
    new MiniCssExtractPlugin({
      filename: 'css/main.css'
    })
  ],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader']
      },
      {
        test: /\.scss$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
      },
      {
        test: /\.(jpg|jpeg|png|gif|svg|webp)$/,
        loader: 'url-loader',
        options: {
          limit: 1024*1024*1
        }
      }
    ]
  }
});
Like
Like Love Haha Wow Sad Angry
赞(1) 打赏
未经允许不得转载:栗子纪blog » Webpack构建react单页面项目配置
分享到: 更多 (0)
0 0 vote
Article Rating
Subscribe
提醒
guest
0 评论
Inline Feedbacks
View all comments

创作不易,打赏一下作者买瓶洗发露

支付宝扫一扫打赏

微信扫一扫打赏

0
Would love your thoughts, please comment.x
()
x