create-react-app
是一款普遍使用的脚手架,默认它只能使用eject
命令暴露出webpack
配置,其实这样使用很不优雅,修改内容文件的话也不利于维护,react-app-rewired
正式解决这样问题的工具,今天咱们就好好学习下它的用法。css
npm install react-app-rewired --save-dev
npm install react-app-rewired@1.6.2 --save-dev
/* config-overrides.js */ module.exports = function override(config, env) { //do stuff with the webpack config... return config; }
固然咱们也能够把config-overrides.js
放到其余位置,好比咱们要指向node_modules
中某个第三方库提供的配置文件,就能够添加下面配置到package.json
:html
"config-overrides-path": "node_modules/other-rewire"
打开package.json
:node
/* 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", "eject": "react-scripts eject" }
webpack
字段能够用来添加你的额外配置,固然这里面不包含Webpack Dev Server
。react
const { override, overrideDevServer, fixBabelImports, addLessLoader, addWebpackAlias, addWebpackModuleRule } = require('customize-cra'); const removeManifest = () => config => { config.plugins = config.plugins.filter( p => p.constructor.name !== "ManifestPlugin" ); return config; }; module.exports = { webpack: override( removeManifest(), fixBabelImports('import', { libraryName: 'antd', libraryDirectory: 'es', style: 'css', }), addLessLoader(), addWebpackModuleRule({ test: require.resolve('snapsvg/dist/snap.svg.js'), use: 'imports-loader?this=>window,fix=>module.exports=0', },), addWebpackAlias({ Snap: 'snapsvg/dist/snap.svg.js' }), ), devServer: overrideDevServer( ... ) }
jest
配置webpack
经过devServer
咱们能够作一些开发环境的配置,好比设置proxy
代理,调整publicPath
,经过disableHostCheck
禁用转发域名检查等。web
从CRA 2.0
开始,推荐搭配customize-cra
使用,里面提供了一些经常使用的配置,能够方便咱们直接使用。shell
const { override, overrideDevServer, } = require('customize-cra'); const addProxy = () => (configFunction) => { configFunction.proxy = { '/v2ex/': { target: 'https://www.v2ex.com', changeOrigin: true, pathRewrite: { '^/v2ex': '/' }, }, }; return configFunction; } module.exports = { webpack: override( ... ), devServer: overrideDevServer( addProxy() ) }
paths
里面是create-react-app
里面的一些路径变量,包含打包目录、dotenv
配置地址、html
模板地址等。npm
module.exports = { dotenv: resolveApp('.env'), appPath: resolveApp('.'), appBuild: resolveApp('build'), appPublic: resolveApp('public'), appHtml: resolveApp('public/index.html'), appIndexJs: resolveModule(resolveApp, 'src/index'), appPackageJson: resolveApp('package.json'), appSrc: resolveApp('src'), appTsConfig: resolveApp('tsconfig.json'), appJsConfig: resolveApp('jsconfig.json'), yarnLockFile: resolveApp('yarn.lock'), testsSetup: resolveModule(resolveApp, 'src/setupTests'), proxySetup: resolveApp('src/setupProxy.js'), appNodeModules: resolveApp('node_modules'), publicUrl: getPublicUrl(resolveApp('package.json')), servedPath: getServedPath(resolveApp('package.json')), // These properties only exist before ejecting: ownPath: resolveOwn('.'), ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3 appTypeDeclarations: resolveApp('src/react-app-env.d.ts'), ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'), };
好比咱们要修改appHtml
即html
模板的默认位置,能够这样作:json
const path = require('path'); module.exports = { paths: function (paths, env) { // 指向根目录的test.html paths.appHtml = path.resolve(__dirname, "test.html"); return paths; }, }
首先安装react-app-rewire-multiple-entry
。antd
npm install react-app-rewire-multiple-entry --save-dev
而后在config-overrides.js
配置:
const { override, overrideDevServer } = require('customize-cra'); const multipleEntry = require('react-app-rewire-multiple-entry')([{ entry: 'src/pages/options.tsx', template: 'public/options.html', outPath: '/options.html', }]); const addEntry = () => config => { multipleEntry.addMultiEntry(config); return config; }; const addEntryProxy = () => (configFunction) => { multipleEntry.addEntryProxy(configFunction); return configFunction; } module.exports = { webpack: override( addEntry(), ), devServer: overrideDevServer( addEntryProxy(), ) }
const { override, } = require('customize-cra'); const removeManifest = () => config => { config.plugins = config.plugins.filter( p => p.constructor.name !== "ManifestPlugin" ); return config; }; module.exports = { webpack: override( removeManifest(), ), }
const { override, fixBabelImports, addLessLoader } = require('customize-cra'); module.exports = { webpack: override( fixBabelImports('import', { libraryName: 'antd', libraryDirectory: 'es', style: 'css', }), addLessLoader(), ), }
最后,若是使用上有什么问题欢迎留言,我会尽我所能解答你们的问题。
本文首发: create-react-app 优雅定制指南
另外推荐关注公众号:湖中剑,实时获取文章更新。