react 总结(一) 项目创建

一、创建项目(已安装 create-react-app)

create-react-app demo

装完后发现没有webpack,于是安装了webpack

npm install webpack webpack-cli --save-dev

突然想看没有webpack自带打包是啥样的,跑npm run build跑不起来,npm start也挂了,卸载webpack,删除node_modules,package-lock.json,然后重新 npm i后再跑即可

自定义配置文件,运行npm run eject,就多了config文件夹,有了webpack,修改打包地址参考

chunk.js文件是大文件拆分出来的部分代码,为了提高浏览器的加载速度,vendor.js打包的第三方库代码

二、webpack配置

参考地址

授权管理项目地址

三、非脚手架创建项目

npm init   //项目初始化生成package.json文件
mkdir src  //生成src文件夹
npm install html-webpack-plugin html-loader --save-dev
npm install react react-dom --save
npm install webpack-dev-server --save-dev

新建webpack.config.js文件

const path = require("path");
const webpack = require("webpack");
const HtmlPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
// const pxtorem = require("postcss-pxtorem");
// pxtorem({
//   rootValue: 100,
//   propList: ["*", "!border"],
//   selectorBlackList: [".px-"] // 过滤掉.px-开头的class,不进行rem转换
// });

module.exports = {
  mode: "development",
  devtool: "source-map",
  entry: ["react-hot-loader/patch", "./src/index.js"],
  output: {
    filename: "index.js",
    path: path.resolve(__dirname, "dist")
  },
  module: {
    rules: [
      {
        test: /\.(css|scss)$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader", // css node(commonJS )风格的 string
          use: [
            "css-loader", //   把 css 转义成  commonJS 规范的js 代码
            {
              loader: "postcss-loader", // 转成 css 风格代码
              options: {
                plugins: function() {
                  return [
                    require("cssgrace"), // 美化css 缩进
                    // require("postcss-px2rem-exclude")({
                    //   remUnit: 160 // px 转 rem
                    //   // exclude: /antd/i // 排除antd 里面rem 转换
                    // }),
                    require("autoprefixer")() // 自动补全 moz ms webkit
                  ];
                }
              }
            },
            "sass-loader" // scss 编译
          ]
        })
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        loader: "url-loader",
        options: {
          limit: 10000,
          name: "img/[name].[hash:7].[ext]"
        }
      },
      {
        test: /\.(js|jsx)$/,
        loader: "babel-loader",
        exclude: /node_modules/
      }
    ]
  },
  devServer: {
    contentBase: "./dist",
    port: 8081,
    inline: true,
    hot: true,
    historyApiFallback: true,
    open: false
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new HtmlPlugin({
      template: "src/index.html",
      filename: "index.html"
    }),
    new ExtractTextPlugin("indexStyles.css"),
    // new CopyWebpackPlugin([
    //   {
    //     from: __dirname + "/src/img",
    //     to: __dirname + "/dist/img"
    //     // ignore: ['.*']
    //   }
    // ])
  ]
};

解决问题

npm i -D extract-text-webpack-plugin@next

src下新建index.html   index.js文件

package.json加个脚本

"start": "webpack-dev-server --open --mode development"

三、创建路由

下载安装

npm install react-router-dom --save-dev 
npm install --save react-router

处理报错

下载

npm i @babel/plugin-proposal-class-properties

.babelrc中添加

options: {

    plugins: ['@babel/plugin-proposal-class-properties']

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值