webpack4配置完整的react的项目(一:开发环境配置)

webpack4配置完整的react的项目(一:开发环境配置)

初始化项目

npm init
一直回车就好

安装依赖

npm i --save react react-dom
    npm i --save-dev babel-loader @babel/core @babel/preset-env @babel/preset-react
    npm i --save-dev webpack webpack-cli webpack-dev-server
react相关依赖包
webpack依赖包
babel相关依赖包,把jsx转成js可识别的函数

创建工作目录,和项目配置目录

创建一个config目录,用于存放webpack的相关配置
创建一个src目录,用于存放工作代码

创建开发环境文件

在config文件夹中创建webpack.config.dev.js文件,配置出口/入口/loader/插件
const resolve = (dir) => path.join(__dirname, '../', dir)

    module.exports = {
        mode: 'development', // 指定运行环境
        devtool: 'cheap-module-eval-source-map', // source-map
        entry: { 
            app: resolve('src/index.jsx')
        },
        output: {
            filename: '[name].js',
            path: resolve('dist')
        },
        module: {
            
        }
        plugins: [
            
        ]
    }
接下来装插件和loader完成dev的webpack配置

安装插件

npm i --save-dev html-webpack-plugin
这个插件的作用是复制一个文件到另外一个文件里,我们可以用来复制一个入口模板的HTML到打包目录里
plugins: [
        new HtmlWebpackPlugin({
            filename: resolve('dist/index.html'),
            template: resolve("static/index.html")
        })
    ]

安装loader

npm i --save-dev style-loader css-loader postcss-loader sass-loader
    npm i --save-dev file-laoder
    npm i --save-dev svg-inline-loader
    npm i --save-dev react-hot-loader
  1. 把sass成webpack识别的模块
  2. 为了引入图片
  3. 为了引入svg
  4. 热更新loader
module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                options: {
                    plugins: ['react-hot-loader/babel'], // 热更新插件
                    presets: ['@babel/preset-env', '@babel/preset-react'] // jsx转为js函数
                },
            },
            {
                test: /\.scss/, // 把scss转为webpack可识别的模块
                loaders: [
                    'style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            importLoaders: 1,
                        },
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                            options: {},
                          }
                    },
                    'sass-loader'
                   
                ]
            },
            {
                test: /\.(png|jpg|gif)$/, 
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: '[path][name].[ext]'
                        }
                    }
                ]
            },
            {
                test: /\.svg/,
                use: [
                    {
                        loader: 'svg-inline-loader'
                    }
                ]
            }
        ]
    },

开启本地服务

devServer: { // dev server 
        host: 'localhost',
        port: '8001',
        open: true
    },

修改package.json

"scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "dev": "webpack-dev-server --config config/webpack.config.dev.js"
    },

最后完整的的webpack.config.dev.js

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

const resolve = (dir) => path.join(__dirname, '../', dir)

module.exports = {
    mode: 'development',
    devtool: 'cheap-module-eval-source-map',
    entry: { // 创建src文件夹/并创建index.jsx入口文件
        app: resolve('src/index.jsx')
    },
    output: {
        filename: '[name].js',
        path: resolve('dist')
    },
    resolve: {
        extensions: ['.js', '.jsx', '.scss', '.css']
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                options: {
                    plugins: ['react-hot-loader/babel'],
                    presets: ['@babel/preset-env', '@babel/preset-react']
                },
            },
            {
                test: /\.scss/,
                loaders: [
                    'style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            importLoaders: 1,
                        },
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                            options: {},
                          }
                    },
                    'sass-loader'
                   
                ]
            },
            {
                test: /\.(png|jpg|gif)$/, 
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: '[path][name].[ext]'
                        }
                    }
                ]
            },
            {
                test: /\.svg/,
                use: [
                    {
                        loader: 'svg-inline-loader'
                    }
                ]
            }
        ]
    },
    devServer: {
        host: 'localhost',
        port: '8001',
        open: true
    },
    plugins: [
        new HtmlWebpackPlugin({
            filename: resolve('dist/index.html'),
            template: resolve("static/index.html")
        })
    ]
}

模板文件

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>hello webpack</title>
    </head>
    <body>
        <div id="root"></div>
    </body>
    </html>

react入口文件

import ReactDOM from 'react-dom'
    import React from 'react'

    ReactDOM.render(
    <h1>hello webpack</h1>,
    document.getElementById('root')
    )

结束语

npm run dev 运行成功后,浏览器会自动打开

一个有热更新的简单的react的开发环境就配置好了。如果有什么问题欢迎留言指正

相关推荐