react-webpack

2022-06-20  本文已影响0人  spfi

1、安装webpakck

npm install --save-dev webpack webpack-cli

刚刚安装的版本是 5.73.0

2、新建config配置文件夹

在根目录下创建一个webpack.config文件夹,在此文件夹下创建三个配置文件

在根目录下再创建一个src文件夹,新建 app.js文件

3、在webpack.common.config.js文件中输入以下代码

const path = require('path');

module.exports = {
  entry: {
    app: './src/app.js',
  },
  output: {
    filename: 'js/bundle.js',
    path: path.resolve(__dirname, '../dist'),
  },
};

4、安装webpack-merge

用于合并webpack配置

npm install --save-dev webpack-merge

5、配置开发环境,生产环境webpack

const path = require('path');

module.exports = {
    entry: {
        app: './src/app.js'
    },

    output: {
        filename: 'js/bundle.js',
        path: path.resolve(__dirname, '../dist')
    }
}

6、安装React

npm install --save react react-dom

7、修改app.js

import React, { Component } from 'react';

class App extends Component {
  render() {
    return (
      <div>my-project!!!!!</div>
    );
  }
}

export default App;

8、在src下新增index.js,代码如下

import React from 'react';
import ReactDOM from 'react-dom';
import App from './app.jsx';

ReactDOM.render(<App />, document.getElementById('root'))

9、修改webpack.common.js配置文件入口为:index.js

10、安装bable

npm install --save-dev babel-loader @babel/core @babel/preset-env @babel/preset-react @babel/plugin-proposal-class-properties @babel/plugin-transform-runtime

各个bable的作用:

11、在webpack.common.js加入bable配置

const path = require('path');

module.exports = {
    entry: {
        app: './src/index.js'
    },

    output: {
        filename: 'js/bundle.js',
        path: path.resolve(__dirname, '../dist')
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env', '@babel/preset-react'],
                        plugins: [
                            '@babel/plugin-transform-runtime',
                            '@babel/plugin-proposal-class-properties'
                        ]
                    }
                }
            }
        ]
    }
}

12、安装HtmlWebpackPlugin

npm install --save-dev html-webpack-plugin

13、配置webpack.config.js

const { merge } = require('webpack-merge');

const common = require('./webpack.common.config');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = merge(common, {
    mode: 'production',
    plugins: [
        new HtmlWebpackPlugin({
            template: './public/index.html',
            filename: 'index.html',
            inject: 'body',
            minify: {
                removeComments: true
            }
        })
    ]
})

14、生成文件添加hash值

    output: {
        filename: 'js/[name]-bundle-[hash:8].js',
        path: path.resolve(__dirname, '../dist')
    }

15、打包前清理dist目录(新版本可以不引入插件)

npm install --save-dev clean-webpack-plugin

代码如下:

const { merge } = require('webpack-merge');

const common = require('./webpack.common.config');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = merge(common, {
    mode: 'production',
    plugins: [
        new HtmlWebpackPlugin({
            template: './public/index.html',
            filename: 'index.html',
            inject: 'body',
            minify: {
                removeComments: true
            }
        }),
        new CleanWebpackPlugin()
    ]
})

注意:新版可配置output对象属性clean为true,来清理

16、安装webpack-dev-server

npm install -D webpack-dev-server

修改webpack.dev.js

const { merge } = require('webpack-merge');
const path = require('path')
const common = require('./webpack.common.config');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = merge(common, {
    mode: 'development',
    devServer: {
        // 新版本不再支持contentBase,而是使用static,路径指定有问题
        // contentBase: path.join(__dirname, 'dist'),
        prot: 8888,
        compress: true,
        static: {
           directory: path.join(__dirname, '/public')
        }
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'public/index.html',
            inject: 'body',
            hash: false
        })
    ]
})

17、样式配置

安装style-loader、css-loader

npm install --save-dev style-loader css-loader

新增配置

    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            }
        ]
    },

18、打包css

安装插件

npm i -D mini-css-extract-plugin

配置打包

  module: {
        rules: [
            {
                test: /\.css$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader']
            }
        ]
    },

在插件里面新增配置

 new MiniCssExtractPlugin({
     filename: 'style/[name].[hash].css'
 })

19、压缩打包出来的css文件

安装依赖

npm i -D css-minimizer-webpack-plugin

配置压缩

    optimization: {
        minimize: true,
        minimizer: [new CssMinizerPlugin()]
    },

20、配置less、sass

安装依赖

npm i less less-loader -D
npm i -D sass sass-loader node-sass

配置less,sass

           {
                test: /\.less$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'less-loader'
                ]
            },
            {
                test: /\.s[ac]ss$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'sass-loader'
                ]
            }

21、压缩js代码

下载插件

npm i -D terser-webpack-plugin

配置压缩文件

    optimization: {
        minimize: true,
        minimizer: [
            new CssMinizerPlugin(),
            new TerserPlugin({
                terserOptions: {
                    compress: {
                        drop_console: true
                    }
                }
            })
        ]
    },

22、配置css浏览器前缀

安装依赖

npm i -D postcss postcss-loader autoprefixer

在根目录,新增postcss.config.js文件,代码如下:

module.exports = {
    plugins: [
        require('autoprefixer')({
            overrideBrowserslist: [
                'Android 4.1',
                'iOS 7.1',
                'Chrome > 31',
                'ff > 31',
                'ie >= 8',
            ]
        })
    ]
};

在css-loader后面新加postcss-loader

23、配置图片load,使用url-loader

安装url-loader,url-loader依赖于file-loader,所以需要安装两个loader

npm i -D file-loader url-loader

配置代码

            {
                test: /\.(png|jpe?g|gif)$/,
                loader: 'url-loader',
                options: {
                    name: '[name].[ext]',
                    outputPath: 'images',
                    limit: 8192
                }
            }

24、配置source-map

devtool: 'eval-cheap-module-source-map',

25、配置resolve.alias

 resolve: {
        extensions: ['.js', '.jsx', '.json', '.less', '.scss'],
        modules: [ path.resolve(__dirname, '../src'), 'node_modules' ],
        alias: {
          '@': path.join(__dirname, '../src')
        }
      }
上一篇下一篇

猜你喜欢

热点阅读