介紹
基本使用
module.exports = function override(config, env) { // do stuff with the webpack config... return config }
{ // ... "scripts": { "start": "react-app-rewired start", "build": "react-app-rewired build", "test": "react-app-rewired test", "eject": "react-scripts eject" }, // ... } 使用ES7的裝飾器
const { override, addDecoratorsLegacy } = require('customize-cra') module.exports = override( // enable legacy decorators babel plugin addDecoratorsLegacy(), ) 使用Less
const { override, // ... addLessLoader, // ... } = require('customize-cra') module.exports = override( // ... // less // addLessLoader(), addLessLoader({ lessOptions: { javascriptEnabled: true, // Optionally adjust URLs to be relative. When false, URLs are already relative to the entry less file. relativeUrls: false, modifyVars: { '@primary-color': '#A80000' }, // cssModules: { // // if you use CSS Modules, and custom `localIdentName`, default is '[local]--[hash:base64:5]'. // localIdentName: "[path][name]__[local]--[hash:base64:5]", // } } }) // ... ) antd-mobile按需引入
const { override, // ... fixBabelImports } = require('customize-cra') module.exports = override( // ... // antd-mobile按需加載 - babel-plugin-import fixBabelImports('import', { libraryName: 'antd-mobile', style: 'css' }) )
const { override, // ... addLessLoader, fixBabelImports, // ... } = require('customize-cra') module.exports = override( // ... // less addLessLoader({ // 現(xiàn)在的寫法 lessOptions: { javascriptEnabled: true, modifyVars: { '@brand-primary': '#1DA57A' }, }, // 原來的寫法 // javascriptEnabled: true, // modifyVars: { // '@primary-color': '#1DA57A', // }, // localIdentName: '[local]--[hash:base64:5]' // 自定義 CSS Modules 的 localIdentName }), // antd-mobile按需加載 - babel-plugin-import fixBabelImports('import', { libraryName: 'antd-mobile', libraryDirectory: 'es', // style: 'css' style: true }), // ... ) 添加別名
const { override, // ... addWebpackAlias } = require('customize-cra') const path = require('path') module.exports = override( // ... // 路徑別名 addWebpackAlias({ '@': path.resolve(__dirname, 'src') }) ) 配置多環(huán)境
REACT_APP_URL_API=http:// REACT_APP_URL_UPLOAD=http://upload.
REACT_APP_URL_API=http:// REACT_APP_URL_UPLOAD=http://upload.
REACT_APP_URL_API=http:// REACT_APP_URL_UPLOAD=http://upload.
{ // ... "scripts": { "start": "dotenv -e .env.dev react-app-rewired start", "build:sit": "dotenv -e .env.sit react-app-rewired build", "build:prod": "dotenv -e .env.prod react-app-rewired build", "test": "react-app-rewired test", "eject": "react-scripts eject" }, // ... }
proxy開發(fā)環(huán)境下跨域問題,前端一般是給本地的
const proxy = require('http-proxy-middleware') module.exports = function(app) { app.use( proxy('/api', { target: 'http://localhost:3001/', changeOrigin: true, // pathRewrite: { // '^/api': '' // } }) ) }
以上方法配置會(huì)出現(xiàn) 解決辦法是修改 const { createProxyMiddleware } = require('http-proxy-middleware'); module.exports = function(app) { app.use(createProxyMiddleware('/api', { target: 'http://localhost:3001/', changeOrigin: true, // pathRewrite: { // '^/api': '' // } })) } |
|