react中webpack配置

本文介绍了在React项目中配置Webpack的基本步骤,包括处理Html、脚本、样式、图片和字体文件,使用webpack-dev-server实现代码热更新和路径转发,以及如何配置CommonsChunkPlugin提出公共模块。详细讲解了每一步的配置细节,如html-webpack-plugin、css-loader、sass-loader、url-loader、file-loader等的使用。
摘要由CSDN通过智能技术生成

webpack使用前的基础知识

webpack使用版本:3.10.0

需要处理文件类型

  1. Html:html-webpack-plugin
  2. 脚本:babel + babel-preset-react
  3. 样式:css-loader + sass-loader
  4. 图片/字体:url-loader + file-loader

webpack常用模块

  1. html-webpack-plugin,html单独打包成文件
  2. extract-text-webpack-plugin,样式打包成单独文件
  3. CommonsChunkPlugin:提出通用的模块(webpack自带)

webpack-dev-server

  1. 为webpack项目提供Web服务
  2. 使用版本:2.9.7
  3. 更改代码自动刷新,路径转发

webpack配置流程

  1. 初始化yarn和git,配置gitignore和readme
  2. 安装webpackyarn add webpack@3.10.0 --dev
  3. 根目录创建webpack.config.js,进行如下配置
const path = require('path');

module.exports = {
   
  entry: './src/app.js',
  output: {
   
    path: path.resolve(__dirname, 'dist'),
    filename: 'app.js'
  }
};
  1. 根目录创建src文件,在src文件下创建入口文件app.js(这一步创建的文件名以及文件目录和上面webpack.config.js中进行的配置相匹配即可)

    此时执行npx webpack即可进行最基本的打包工作

  2. 处理html文件的配置
    执行yarn add html-webpack-plugin@2.30.1 --dev

    在webpack.config.js中添加plugins的配置项,如下

    const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin')
    module.exports = {
         
      entry: './src/app.js',
      output: {
         
        path: path.resolve(__dirname, 'dist'),
        filename: 'app.js'
      },
      plugins: [
        new HtmlWebpackPlugin({
         
            template: './src/index.html'
        })
      ]
    };
    

    在src目录下创建index.html作为模板

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>react-template</title>
    </head>
    <body>
        <div id="app"></div>
    </body>
    </html>
    
  3. 处理脚本
    执行yarn add babel-core@6.26.0 babel-preset-env@1.6.1 babel-loader@7.1.2 --dev
    在webpack.config.js添加module

    const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin')
    module.exports = {
         
      entry: './src/app.js',
      output: {
         
        path: path.resolve(__dirname, 'dist'),
        filename: 'app.js'
      },
      module: {
         
        rules: [
          {
         
            test: /\.m?js$/,
            exclude: /(node_modules)/,
            use: {
         
              loader: 'babel-loader',
              options: {
         
                presets: ['env']
              }
            }
          }
        ]
      },
      plugins: [
        new HtmlWebpackPlugin({
         
            template: './src/index.html'
        })
      ]
    };
    
  4. react的处理
    执行yarn add babel-preset-react@6.24.1 --dev
    yarn add react@16.2.0 react-dom@16.2.0
    在babel的preset中添加'react'

    const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin')
    module.exports = {
         
      entry: './src/app.js',
      output: {
         
        path: path.resolve(__dirname, 'dist'),
        filename: 'app.js'
      },
      module: {
         
        rules: [
          {
         
            test: /\.m?js$/,
            exclude: /(node_modules)/,
            use: {
         
              loader: 'babel-loader',
              options: {
         
                presets: ['env', 'react']
              }
            }
          }
        ]
      
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值