React 16.3.0项目搭建

使用create-react-app创建项目

  • 安装: npm install -g create-react-app
  • 创建项目:create-react-app my-app
    • cd my-app
    • yarn install
    • yarn start

使用react-app-rewired改写项目有配置

  • 安装yarn add react-app-rewired --save-dev
  • 在项目根目录创建config-overrides.js文件
module.exports = function override(config, env) {
  //do stuff with the webpack config...
  return config;
}
  • 改写package.json文件
  /* package.json */
  "scripts": {
-   "start": "react-scripts start",
+   "start": "react-app-rewired start",
-   "build": "react-scripts build",
+   "build": "react-app-rewired build",
-   "test": "react-scripts test --env=jsdom",
+   "test": "react-app-rewired test --env=jsdom"
}

react-app-rewire改写的项目中配置SassLess

1. Sass使用配置

  • 安装yarn add react-app-rewire-sass-modules sass-loader node-sass -D
  • config-overrides.js文件中添加配置
const rewireSass = require('react-app-rewire-sass-modules');
module.exports = function override(config, env) {
    config = rewireSass(config, env);
     return config;
}

2. Less使用配置

  • 安装yarn add react-app-rewire-less -D
  • config-overrides.js文件中添加配置
const rewireLess = require('react-app-rewire-less');
/* config-overrides.js */
module.exports = function override(config, env) {
    config = rewireLess(config, env);
    // with loaderOptions
    // config = rewireLess.withLoaderOptions(someLoaderOptions)(config, env);
    return config;
}

你可能感兴趣的:(React 16.3.0项目搭建)