• 【ReactCli】开发模式脚手架配置


    dev

    const path = require('path'); //nodejs
    const ESLintPlugin = require('eslint-webpack-plugin');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    module.exports = {
        entry: "./src/main.js",
        output: {
            path: undefined,
            filename: "static/js/[name].js",
            chunkFilename: 'static/js/[name].chunk.js',
            assetModuleFilename: "static/media/[hash:10][ext][query]"
        },
        module: {
            rules: [
                {
                    test: /\.css$/,
                    use: [
                        "style-loader",
                         "css-loader", 
                         {
                        loader: 'postcss-loader',
                        options: {
                            postcssOptions: {
                                plugins: ["postcss-preset-env"]
                            }
                        }
                    }]
                },
                {
                    test: /\.less$/,
                    use: [
                        "style-loader", 
                        "css-loader",
                         {
                            loader: 'postcss-loader',
                            options: {
                                postcssOptions: {
                                    plugins: ["postcss-preset-env"]
                                }
                            }
                        },
                        "less-loader"
                    ]
                },
                {
                    test: /\.s[ac]ss$/,
                    use: ["style-loader", "css-loader", 
                    {
                        loader: 'postcss-loader',
                        options: {
                            postcssOptions: {
                                plugins: ["postcss-preset-env"]
                            }
                        }
                    }, 
                    "sass-loader"]
                },
                {
                    test: /\.styl$/,
                    use: ["style-loader", "css-loader", {
                        loader: 'postcss-loader',
                        options: {
                            postcssOptions: {
                                plugins: ["postcss-preset-env"]
                            }
                        }
                    }, 
                    "stylus-loader"]
    
                },
                {
                    test: /\.(jpe?g|png|gif|webp|svg)/,
                    type: "asset",
                    parser: {
                        dataUrlCondition: {
                            maxSize: 10 * 1024
                        }
                    }
                },
                {
                    test: /\.(woff2?|ttf)/,
                    type: "asset/resource",
                },
                {
                    test: /\.jsx?$/,
                    include: path.join(__dirname, "../src"),
                    loader: "babel-loader",
                    options: {
                        cacheDirectory: true,
                        cacheCompression: false
                    }
                }
            ]
        },
        plugins: [
            new ESLintPlugin({
                context: path.resolve(__dirname, '../src'),
                exclude: "node_modules",
                catch: true,
                cacheLocation: path.resolve(__dirname, '../node_modules/.catch/eslintcatche')
            }), new HtmlWebpackPlugin({
                template: path.resolve(__dirname, '../public/index.html')
            })
        ],
        mode: 'development',
        devtool: "cheap-module-source-map",
        optimization: {
            splitChunks: {
                chunks: 'all'
            },
            runtimeChunk: {
                name: entrypoint => `runtime~${entrypoint.name}.js`
            }
        },
        devServer: {
            host: 'localhost', //启动服务器域名
            port: "3000", //启动服务器端口号
            open: true, //是自动打开浏览器
            hot: true //关闭HML
        },
        //自动补全
        resolve: {
            extensions: [".jsx", ".js", ".json"]
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124

    prod

    const path = require('path'); //nodejs
    const ESLintPlugin = require('eslint-webpack-plugin');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const MiniCssExtractPlugin = require("mini-css-extract-plugin");
    const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
    const TerserWebpackPlugin = require('terser-webpack-plugin')
    const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
    module.exports = {
        entry: "./src/main.js",
        output: {
            path: path.resolve(__dirname, "../dist"),
            filename: "static/js/[name].[contenthash:10].js",
            chunkFilename: 'static/js/[name].[contenthash:10].chunk.js',
            assetModuleFilename: "static/media/[hash:10][ext][query]",
            clean: true
        },
        module: {
            rules: [{
                    test: /\.css$/,
                    use: [
                        MiniCssExtractPlugin, "css-loader",
                        {
                            loader: 'postcss-loader',
                            options: {
                                postcssOptions: {
                                    plugins: ["postcss-preset-env"]
                                }
                            }
                        }
                    ]
                },
                {
                    test: /\.less$/,
                    use: [
                        MiniCssExtractPlugin, "css-loader",
                        {
                            loader: 'postcss-loader',
                            options: {
                                postcssOptions: {
                                    plugins: ["postcss-preset-env"]
                                }
                            }
                        },
                        "less-loader"
                    ]
                },
                {
                    test: /\.s[ac]ss$/,
                    use: [MiniCssExtractPlugin, "css-loader",
                        {
                            loader: 'postcss-loader',
                            options: {
                                postcssOptions: {
                                    plugins: ["postcss-preset-env"]
                                }
                            }
                        },
                        "sass-loader"
                    ]
                },
                {
                    test: /\.styl$/,
                    use: [MiniCssExtractPlugin, "css-loader", {
                            loader: 'postcss-loader',
                            options: {
                                postcssOptions: {
                                    plugins: ["postcss-preset-env"]
                                }
                            }
                        },
                        "stylus-loader"
                    ]
    
                },
                {
                    test: /\.(jpe?g|png|gif|webp|svg)/,
                    type: "asset",
                    parser: {
                        dataUrlCondition: {
                            maxSize: 10 * 1024
                        }
                    }
                },
                {
                    test: /\.(woff2?|ttf)/,
                    type: "asset/resource",
                },
                {
                    test: /\.jsx?$/,
                    include: path.join(__dirname, "../src"),
                    loader: "babel-loader",
                    options: {
                        cacheDirectory: true,
                        cacheCompression: false
                    }
                }
            ]
        },
        plugins: [
            new ESLintPlugin({
                context: path.resolve(__dirname, '../src'),
                exclude: "node_modules",
                catch: true,
                cacheLocation: path.resolve(__dirname, '../node_modules/.catch/eslintcatche')
            }), new HtmlWebpackPlugin({
                template: path.resolve(__dirname, '../public/index.html')
            }), new MiniCssExtractPlugin({
                filename: "static/css/[name].[contenthash:10].css",
                chunkFilename: "static/css/[name].[contenthash:10].chunk.css"
            }), new ImageMinimizerPlugin({
                minimizerOptions: {
                    // Lossless optimization with custom option
                    // Feel free to experiment with options for better result for you
                    plugins: [
                        ["gifsicle", {
                            interlaced: true
                        }],
                        ["jpegtran", {
                            progressive: true
                        }],
                        ["optipng", {
                            optimizationLevel: 5
                        }],
                        // Svgo configuration here https://github.com/svg/svgo#configuration
                        [
                            "svgo",
                            {
                                plugins: extendDefaultPlugins([{
                                        name: "removeViewBox",
                                        active: false,
                                    },
                                    {
                                        name: "addAttributesToSVGElement",
                                        params: {
                                            attributes: [{
                                                xmlns: "http://www.w3.org/2000/svg"
                                            }],
                                        },
                                    },
                                ]),
                            },
                        ],
                    ],
                },
            }),new CopyPlugin({
                patterns: [
                  { from: path.relative(__dirname,"../public"), to:  path.relative(__dirname,"../dist"), exports
                  globOptions: {
                    dot: true,
                    gitignore: true,
                    ignore: ["**/index.html"],
                  },
                },
                ],
              }),
        ],
        mode: 'production',
        devtool: "source-map",
        optimization: {
            splitChunks: {
                chunks: 'all'
            },
            runtimeChunk: {
                name: entrypoint => `runtime~${entrypoint.name}.js`
            },
            minimizer: [
                new CssMinimizerPlugin(), new TerserWebpackPlugin()
            ]
        },
        //自动补全
        resolve: {
            extensions: [".jsx", ".js", ".json"]
        }
    }
    //https://webpack.docschina.org/plugins/image-minimizer-webpack-plugin/#root
    //npm install copy-webpack-plugin --save-dev
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    {
      "name": "react-cli",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "start": "npm run dev",
        "dev": "cross-env NODE_ENV=development webpack serve --config ./config/webpack.dev.js",
        "build": "cross-env NODE_ENV=production webpack --config ./config/webpack.prod.js"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "browsersList": [
        "last 2 version",
        "> 1%",
        "not dead"
      ],
      "devDependencies": {
        "@babel/core": "^7.20.2",
        "@babel/plugin-proposal-class-properties": "^7.18.6",
        "@babel/plugin-proposal-decorators": "^7.20.2",
        "@babel/plugin-syntax-dynamic-import": "^7.8.3",
        "@babel/preset-env": "^7.20.2",
        "@babel/preset-react": "^7.18.6",
        "babel-loader": "^9.1.0",
        "babel-preset-react-app": "^10.0.1",
        "copy-webpack-plugin": "^11.0.0",
        "cross-env": "^7.0.3",
        "css-loader": "^6.7.2",
        "css-minimizer-webpack-plugin": "^4.2.2",
        "eslint-config-react-app": "^7.0.1",
        "file-loader": "^6.2.0",
        "html-webpack-plugin": "^5.5.0",
        "less": "^4.1.3",
        "less-loader": "^11.1.0",
        "mini-css-extract-plugin": "^2.7.0",
        "rimraf": "^3.0.2",
        "style-loader": "^3.3.1",
        "stylus-loader": "^7.1.0",
        "terser-webpack-plugin": "^5.3.6",
        "url-loader": "^4.1.1",
        "webpack": "^5.75.0",
        "webpack-cli": "^5.0.0",
        "webpack-dev-server": "^4.11.1",
        "webpack-manifest-plugin": "^5.0.0"
      },
      "dependencies": {
        "@loadable/component": "^5.15.2",
        "axios": "^1.2.0",
        "core-js": "^3.26.1",
        "core-js-compat": "^3.4.7",
        "decimal.js": "^10.4.2",
        "eslint-webpack-plugin": "^3.2.0",
        "js-cookie": "^3.0.1",
        "mobx": "^6.7.0",
        "mobx-react": "^7.6.0",
        "postcss-loader": "^7.0.1",
        "react": "^18.2.0",
        "react-dom": "^18.2.0",
        "react-router-dom": "^6.4.3",
        "sass-loader": "^13.2.0"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    合并

    const path = require('path'); //nodejs
    const ESLintPlugin = require('eslint-webpack-plugin');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const MiniCssExtractPlugin = require("mini-css-extract-plugin");
    const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
    const TerserWebpackPlugin = require('terser-webpack-plugin')
    const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
    const isProduction = process.env.NODE_ENV === 'production';
    const getStyleLoaders = (pre) =>{
        return [
            isProduction? MiniCssExtractPlugin.loader :"style-loader",
            "css-loader",
            {
                loader: 'postcss-loader',
                options: {
                    postcssOptions: {
                        plugins: ["postcss-preset-env"]
                    }
                }
            },
            pre
        ].filter(Boolean)
    }
    module.exports = {
        entry: "./src/main.js",
        output: {
            path: isProduction?path.resolve(__dirname, "../dist"):undefined,
            filename:isProduction? "static/js/[name].[contenthash:10].js":"static/js/[name].js",
            chunkFilename: isProduction? 'static/js/[name].[contenthash:10].chunk.js':'static/js/[name].chunk.js',
            assetModuleFilename: "static/media/[hash:10][ext][query]",
            clean: true
        },
        module: {
            rules: [{
                    test: /\.css$/,
                    use: getStyleLoaders()
                },
                {
                    test: /\.less$/,
                    use:getStyleLoaders('less-loader')
                },
                {
                    test: /\.s[ac]ss$/,
                    use: getStyleLoaders('sass-loader')
                },
                {
                    test: /\.styl$/,
                    use:  use: getStyleLoaders('stylus-loader')
                },
                {
                    test: /\.(jpe?g|png|gif|webp|svg)/,
                    type: "asset",
                    parser: {
                        dataUrlCondition: {
                            maxSize: 10 * 1024
                        }
                    }
                },
                {
                    test: /\.(woff2?|ttf)/,
                    type: "asset/resource",
                },
                {
                    test: /\.jsx?$/,
                    include: path.join(__dirname, "../src"),
                    loader: "babel-loader",
                    options: {
                        cacheDirectory: true,
                        cacheCompression: false
                    }
                }
            ]
        },
        plugins: [
            new ESLintPlugin({
                context: path.resolve(__dirname, '../src'),
                exclude: "node_modules",
                catch: true,
                cacheLocation: path.resolve(__dirname, '../node_modules/.catch/eslintcatche')
            }), new HtmlWebpackPlugin({
                template: path.resolve(__dirname, '../public/index.html')
            }),  isProduction && new MiniCssExtractPlugin({
                filename: "static/css/[name].[contenthash:10].css",
                chunkFilename: "static/css/[name].[contenthash:10].chunk.css"
            }),  new ImageMinimizerPlugin({
                minimizerOptions: {
                    // Lossless optimization with custom option
                    // Feel free to experiment with options for better result for you
                    plugins: [
                        ["gifsicle", {
                            interlaced: true
                        }],
                        ["jpegtran", {
                            progressive: true
                        }],
                        ["optipng", {
                            optimizationLevel: 5
                        }],
                        // Svgo configuration here https://github.com/svg/svgo#configuration
                        [
                            "svgo",
                            {
                                plugins: extendDefaultPlugins([{
                                        name: "removeViewBox",
                                        active: false,
                                    },
                                    {
                                        name: "addAttributesToSVGElement",
                                        params: {
                                            attributes: [{
                                                xmlns: "http://www.w3.org/2000/svg"
                                            }],
                                        },
                                    },
                                ]),
                            },
                        ],
                    ],
                },
            }), isProduction && new CopyPlugin({
                patterns: [
                  { from: path.relative(__dirname,"../public"), to:  path.relative(__dirname,"../dist"), exports
                  globOptions: {
                    dot: true,
                    gitignore: true,
                    ignore: ["**/index.html"],
                  },
                },
                ],
              }),
        ],
        mode: isProduction? 'production':'development',
        devtool: "source-map",
        optimization: {
            minimize:isProduction,
            splitChunks: {
                chunks: 'all'
            },
            runtimeChunk: {
                name: entrypoint => `runtime~${entrypoint.name}.js`
            },
            minimizer: [
                new CssMinimizerPlugin(), new TerserWebpackPlugin()
            ]
        },
        //自动补全
        resolve: {
            extensions: [".jsx", ".js", ".json"]
        }
    }
    //https://webpack.docschina.org/plugins/image-minimizer-webpack-plugin/#root
    //npm install copy-webpack-plugin --save-dev
    /*
    
     "scripts": {
        "start": "npm run dev",
        "dev": "cross-env NODE_ENV=development webpack serve --config ./config/webpack.dev.js",
        "build": "cross-env NODE_ENV=production webpack --config ./config/webpack.prod.js"
      },
    
      server
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162

    Vue CLI

    1、CLI是什么意思?

    是Command-Line Interface,翻译为命令行界面,但是俗称脚手架
    Vue CLI是一个官方发布vue.js项目脚手架
    使用vue-cli可以快速搭建Vue开发环境以及对应的webpack配置

    2、Vue CLI使用前提 --Node(需要安装node)
    然而使用Node,必须涉及到npm,
    什么是NPM?

    NPM的全称是Node Package Manager
    是一个NodeJS包管理和分发工具,已经成为了非官方的发布Node模块(包)的标准
    经常使用NPM来安装一些开发过程中依赖包。

    3、Vue CLI的使用

    安装Vue脚手架

      npm install -g @vue/cli
    
    • 1

    注意:上面安装的是Vue CLI3的版本

    Vue CLI2初始化项目

    vue init webpack my-project
    
    • 1
  • 相关阅读:
    2.10 - 存储管理 2.11 - 页式存储 2.12 - 段式存储 2.13 - 段页式存储
    maven使用时候出现,jdk1.5的情况的解决办法
    JAVA基础--MAVEN
    【MySQL】MySQL中的复制技术是什么?它有哪些组成部分?
    安卓--多指触控
    PositiveSSL通配符SSL证书能保护几个域名
    uni 结合vuex 编写动态全局配置变量 this.baseurl
    InstantMesh:利用稀疏视图大规模重建模型从单张图像高效生成3D网格
    PCL 点云投影到圆柱(C++详细过程版)
    mysql报错:Duplicate entry ‘...‘ for key ‘field‘
  • 原文地址:https://blog.csdn.net/qq_43547255/article/details/128083957