npm install -g create-react-app
複製代碼
create-react-app <項目名稱>
複製代碼
cd <項目名>
npm run start
複製代碼
{
......
"homepage": ".",
"dependencies": {
"react": "^16.4.0",
"react-dom": "^16.4.0",
"react-scripts": "1.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
複製代碼
http://localhost:3000
訪問項目;"homepage": "."
(上面配置文件已給出), 同時build後的項目須要在服務器下才能訪問;不然打開的將是空白頁面;%PUBLIC_URL%
來指向public目錄路徑;npm run eject
說明: 執行eject腳本後,項目下會新增或對部分配置文件進行修改;項目下 script 目錄存放着腳本文件, config 目錄下存放着配置文件
複製代碼
npm install less-loader less -dev
複製代碼
{
+ test: /\.(css|less)$/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
+ importLoaders: 2,
},
},
{ .... },
+ {
+ loader: require.resolve('less-loader'),
+ }
],
}
複製代碼
react-app-rewired 的使用javascript
npm install react-app-rewired --save-dev
複製代碼
"scripts": {
+ "start": "react-app-rewired start",
+ "build": "react-app-rewired build",
+ "test": "react-app-rewired test --env=jsdom",
+ "eject": "react-app-rewired eject"
}
複製代碼
module.exports = function override(config, env) {
// 在這裏添加配置
return config;
}
複製代碼
修改配置文件 config-overrides.js 使得項目可以支持 lesscss
# 說明: 這裏再也不須要額外單獨安裝依賴包:less-loader less
npm install react-app-rewire-less --save
複製代碼
+ const rewireLess = require('react-app-rewire-less');
module.exports = function override(config, env) {
+ // 只須要一條配置信息便可實現對less的支持
+ config = rewireLess(config, env);
return config;
}
複製代碼
react-app-rewired 的使用: 使用上和舊版本基本同樣只是在配置上須要額外經過 customize-cra 插件來實現html
npm install react-app-rewired customize-cra --save-dev
複製代碼
"scripts": {
+ "start": "react-app-rewired start",
+ "build": "react-app-rewired build",
+ "test": "react-app-rewired test --env=jsdom",
+ "eject": "react-app-rewired eject"
}
複製代碼
const { override } = require('customize-cra');
module.exports = override();
複製代碼
修改配置文件 config-overrides.js 使得項目可以支持 lessjava
npm install less less-loader --save-dev
複製代碼
+ const { override, addLessLoader } = require('customize-cra');
module.exports = override(
+ addLessLoader({
+ strictMath: true,
+ noIeCompat: true
+ })
);
複製代碼
npm install antd --save
複製代碼
import antd/dist/antd.css
複製代碼
import { Button } from 'antd';
複製代碼
import Button from 'antd/lib/button';
import 'antd/lib/button/style';
// 或者經過import antd/lib/button/style/css 進行加載樣式
複製代碼
babel-plugin-import 是一個用於按需加載組件代碼和樣式的 babel 插件node
暴露配置react
npm run eject
複製代碼
npm install babel-plugin-import --save-dev
複製代碼
"babel": {
"presets": [
"react-app"
],
"plugins": [
+ ["import", { "libraryName": "antd", "libraryDirectory": "es", "style": "css" }]
]
},
複製代碼
import { Button } from 'antd';
複製代碼
babel-plugin-import 是一個用於按需加載組件代碼和樣式的 babel 插件;webpack
react-app-rewired 的使用在第四節已經有了詳細的描述這裏不在贅述;下文直接經過修改 config-overrides.js 配置來實現 antd 的按需加載git
安裝依賴包:babel-plugin-importgithub
npm install babel-plugin-import --save-dev
複製代碼
+ const { injectBabelPlugin } = require('react-app-rewired');
module.exports = function override(config, env) {
+ config = injectBabelPlugin(['import', { libraryName: 'antd', libraryDirectory: 'es', style: 'css' }], config);
return config;
};
複製代碼
+const { override, addLessLoader, fixBabelImports } = require('customize-cra');
module.exports = override(
addLessLoader({
strictMath: true,
noIeCompat: true
}),
+ fixBabelImports('import', { libraryName: 'antd', libraryDirectory: 'es', style: 'css' }),
);
複製代碼
const rewireLess = require('react-app-rewire-less');
const { injectBabelPlugin } = require('react-app-rewired');
module.exports = function override(config, env) {
// 擴展 webpack ==> 支持less
config = rewireLess(config, env);
// 配置 less-loader 加載參數
+ config = rewireLess.withLoaderOptions({modifyVars: { "@primary-color": "#1DA57A" },})(config, env);
// antd 按需加載配置
config = injectBabelPlugin(['import',
{ libraryName: 'antd', libraryDirectory: 'es', style: true }], config);
return config;
}
複製代碼
javascriptEnabled: true
style: true
const { override, addLessLoader, fixBabelImports } = require('customize-cra');
module.exports = override(
addLessLoader({
strictMath: true,
noIeCompat: true,
+ javascriptEnabled: true,
+ modifyVars: { "@primary-color": "#1DA570" }
}),
+ fixBabelImports('import', { libraryName: 'antd', libraryDirectory: 'es', style: true }),
);
複製代碼
# 若是你的 Babel < 7.x 則安裝 babel-plugin-transform-decorators-legacy
npm install --save-dev babel-plugin-transform-decorators-legacy
# 若是你的 Babel >= 7.x 則應該安裝 @babel/plugin-proposal-decorators
npm install --save-dev @babel/plugin-proposal-decorators
複製代碼
假設當前 Babel >= 7.x, 若是你的 Babel < 7.x 則須要將 ["@babel/plugin-proposal-decorators", {"legacy": true}]
修改成 ["transform-decorators-legacy"]
web
暴露配置
npm run eject
複製代碼
"babel": {
"presets": [
"react-app"
],
"plugins": [
+ ["@babel/plugin-proposal-decorators", {"legacy": true}]
]
},
複製代碼
// 導入添加babel插件的函數
+const { injectBabelPlugin } = require('react-app-rewired');
module.exports = function override(config, env) {
+ config = injectBabelPlugin(["@babel/plugin-proposal-decorators", {"legacy": true}], config)
return config;
};
複製代碼
+const {
+ override, addLessLoader, fixBabelImports,
+ addBabelPlugin, addBabelPlugins, useBabelRc
+} = require('customize-cra');
module.exports = override(
addLessLoader({
strictMath: true,
noIeCompat: true,
javascriptEnabled: true,
modifyVars: { "@primary-color": "#1DA570" }
}),
fixBabelImports('import', { libraryName: 'antd', libraryDirectory: 'es', style: true }),
+ addBabelPlugin(["@babel/plugin-proposal-decorators", {"legacy": true}]),
+ // ...addBabelPlugins(
+ // ["@babel/plugin-proposal-decorators", {"legacy": true}]
+ // ),
+ // useBabelRc(),
);
複製代碼
npm run eject
複製代碼
...
"eslintConfig": {
// 默認繼承 腳手架自帶的 eslint 配置
"extends": "react-app",
// 在這裏擴展新增配置
"rules":{
// 設置規則,具體看官網用戶指南下的規則文檔
"eqeqeq": "off"
}
}
複製代碼
npm install react-app-rewired react-app-rewire-eslint --save
複製代碼
+ const rewireEslint = require('react-app-rewire-eslint');
module.exports = function override(config, env) {
+ config = rewireEslint(config, env);
return config;
}
複製代碼
{
"rules": {
// 設置規則,具體看官網用戶指南下的規則文檔
"eqeqeq": "off"
}
}
複製代碼
+ const { override, useEslintRc } = require('customize-cra');
module.exports = override(
+ useEslintRc(),
);
複製代碼
{
"rules": {
// 設置規則,具體看官網用戶指南下的規則文檔
"eqeqeq": "off"
}
}
複製代碼
{
....
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test --env=jsdom",
"eject": "react-app-rewired eject"
},
...
}
複製代碼
The "injectBabelPlugin" helper has been deprecated as of v2.0. You can use customize-cra plugins in replacement
複製代碼