【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"]
    }
}

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
{
  "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"
  }
}

合并

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
*/

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

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

Vue CLI2初始化项目

vue init webpack my-project
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在React脚手架配置路由,需要进行以下步骤: 1. 安装React-Router相关的模块(React-Router,React-Router-DOM)。 2. 在需要使用路由的组件中,引入BrowserRouter、Route、Link等组件。 3. 使用Route组件来定义路由规则,指定路径和对应的组件。 4. 使用Link组件来创建链接,实现页面跳转。 5. 使用withRouter函数加工一般组件后,可将一般组件变为路由组件,从而可以使用路由组件的方法。 6. 可以使用路由懒加载来提高页面加载速度。 下面是一个简单的React脚手架配置路由的例子: 1. 首先,在终端中使用npm安装React-Router和React-Router-DOM模块: npm install react-router-dom --save 2. 在需要使用路由的组件中,引入BrowserRouter、Route、Link等组件: import { BrowserRouter, Route, Link } from 'react-router-dom'; 3. 使用Route组件来定义路由规则,指定路径和对应的组件: <BrowserRouter> <div> <ul> <li><Link to="/">Home</Link></li> <li><Link to="/about">About</Link></li> <li><Link to="/contact">Contact</Link></li> </ul> <Route exact path="/" component={Home} /> <Route path="/about" component={About} /> <Route path="/contact" component={Contact} /> </div> </BrowserRouter> 4. 使用Link组件来创建链接,实现页面跳转: <Link to="/about">About</Link> 5. 使用withRouter函数加工一般组件后,可将一般组件变为路由组件,从而可以使用路由组件的方法: import { withRouter } from 'react-router-dom'; const MyComponent = ({ history }) => ( <button onClick={() => history.push('/about')}> Go to About page </button> ); export default withRouter(MyComponent); 6. 可以使用路由懒加载来提高页面加载速度: import Loadable from 'react-loadable'; const Home = Loadable({ loader: () => import('./Home'), loading: () => <div>Loading...</div>, }); const About = Loadable({ loader: () => import('./About'), loading: () => <div>Loading...</div>, }); const Contact = Loadable({ loader: () => import('./Contact'), loading: () => <div>Loading...</div>, });

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可可鸭~

想吃糖~我会甜

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值