1. const paths = require('react-scripts/config/paths');
  2. paths.appBuild = path.join(path.dirname(paths.appBuild), 'dist'); // 修改打包目录
  1. // Override `create-react-app` default configs for Jest.
  2. const jestConfig = require('./jest.config');
  3.  
  4. module.exports = {
  5. jest(config) {
  6. return { ...config, ...jestConfig };
  7. },
  8. };
  1. module.exports = function override(config, env) {
  2. config.module.rules = config.module.rules.map(rule => {
  3. if (rule.oneOf instanceof Array) {
  4. return {
  5. ...rule,
  6. oneOf: [
  7. {
  8. test: /\.(svg|png|jpg|jpeg|gif|bmp|tiff)$/i,
  9. use: [
  10. {
  11. loader: 'file-loader',
  12. options: {
  13. name: '[path][name]-[hash:8].[ext]'
  14. }
  15. }
  16. ]
  17. },
  18. ...rule.oneOf
  19. ]
  20. };
  21. }
  22.  
  23. return rule;
  24. });
  25.  
  26. return config;
  27. }

Putting react-app-rewired config AFTER customize-cra worked for me. It doesn't work though if I assign an object to config, but works if I fix config line by line. I'm sure there is a more elegant solution.

  1. module.exports = function override(config, env) {
  2. const APP = process.env.REACT_APP_APP
  3. const BRAND = process.env.REACT_APP_BRAND
  4.  
  5. addWebpackAlias({
  6. ['@app-config']: path.resolve(__dirname, `./src/brands/${BRAND}/app`),
  7. })
  8.  
  9. config.entry = `./src/${APP}/index.js`
  10. config.resolve.alias['@app-config'] = path.resolve(__dirname, `./src/brands/${BRAND}`)
  11. config.resolve.modules = [
  12. path.join(__dirname, `src/brands/${BRAND}`)
  13. ].concat(config.resolve.modules)
  14.  
  15. return config
  16. };

  

Can i replace options only for specific plugin without reseting all plugins created by react-app?

  1. const path = require('path');
  2. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  3. const HtmlWebpackPlugin = require('html-webpack-plugin');
  4. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  5.  
  6. module.exports = function override(config, env) {
  7. if( !config.plugins ) {
  8. config.plugins = [];
  9. }
  10.  
  11. const miniCssOptions = {
  12. filename: "[name].[hash].css"
  13. }
  14. let miniCssAdded = false;
  15.  
  16. if( config.plugins.length ) {
  17. config.plugins.forEach( p => {
  18. if( p instanceof MiniCssExtractPlugin) {
  19. delete p;
  20. p = new MiniCssExtractPlugin( miniCssOptions );
  21. miniCssAdded = true;
  22. }
  23. })
  24. }
  25.  
  26. if( !miniCssAdded ) {
  27. config.plugins.push( new MiniCssExtractPlugin( miniCssOptions ) );
  28. }
  29.  
  30. config.plugins.push( new CleanWebpackPlugin() );
  31.  
  32. config.plugins.push( new HtmlWebpackPlugin({
  33. title: 'Caching',
  34. }) );
  35.  
  36. config.output.filename ='static/js/[name].[hash].js';
  37. config.output.chunkFilename ='static/js/[name].[hash].js';
  38. config.output.path = path.resolve(__dirname, 'build');
  39.  
  40. config.optimization.splitChunks= {
  41. cacheGroups: {
  42. vendor: {
  43. test: /[\\/]node_modules[\\/]/,
  44. name: 'vendors',
  45. chunks: 'all',
  46. }
  47. }
  48. }
  49.  
  50. return config;
  51. } 

I think you almost made it. But delete p is NOT a way to delete item from an array. you should use .splice, which can delete items from array and add items in the meantime:

  1. config.plugins.forEach( (p,i) => {
  2. if( p instanceof MiniCssExtractPlugin) {
  3. //delete p;
  4. config.plugins.splice(i,1, new MiniCssExtractPlugin( miniCssOptions ));
  5. }
  6. }) 

https://stackoverflow.com/questions/59045454/react-webpack-config-is-it-possible-to-replace-config-for-only-one-plugin-in-ar?r=SearchResults  

  1. const path = require('path');
  2. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  3. const HtmlWebpackPlugin = require('html-webpack-plugin');
  4. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  5.  
  6. module.exports = function override(config, env) {
  7. if( !config.plugins ) {
  8. config.plugins = [];
  9. }
  10.  
  11. const miniCssOptions = {
  12. filename: "[name].[hash].css"
  13. }
  14. let miniCssAdded = false;
  15.  
  16. if( config.plugins.length ) {
  17. config.plugins.forEach( p => {
  18. if( p instanceof MiniCssExtractPlugin) {
  19. delete p;
  20. p = new MiniCssExtractPlugin( miniCssOptions );
  21. miniCssAdded = true;
  22. }
  23. })
  24. }
  25.  
  26. if( !miniCssAdded ) {
  27. config.plugins.push( new MiniCssExtractPlugin( miniCssOptions ) );
  28. }
  29.  
  30. config.plugins.push( new CleanWebpackPlugin() );
  31.  
  32. config.plugins.push( new HtmlWebpackPlugin({
  33. title: 'Caching',
  34. }) );
  35.  
  36. config.output.filename ='static/js/[name].[hash].js';
  37. config.output.chunkFilename ='static/js/[name].[hash].js';
  38. config.output.path = path.resolve(__dirname, 'build');
  39.  
  40. config.optimization.splitChunks= {
  41. cacheGroups: {
  42. vendor: {
  43. test: /[\\/]node_modules[\\/]/,
  44. name: 'vendors',
  45. chunks: 'all',
  46. }
  47. }
  48. }
  49.  
  50. return config;
  51. }
  1. "scripts": {
  2. "format": "prettier --write",
  3. "start": "PORT=3001 react-app-rewired start",
  4. "build": "react-app-rewired build",
  5. "test": "react-app-rewired test"
  6. },

  

  1. https://stackoverflow.com/questions/58344625/react-application-production-build-static-folder-path-changes?r=SearchResults
  1. You can use useBabelRc() function of customize-cra and in .babelrc, you can write babel config for antd.
  1. {
  2. "plugins": ["@babel/plugin-proposal-optional-chaining"]
  3. }
  1. https://medium.com/@adostes/enabling-optional-chaining-in-a-create-react-app-a9f626a515d9
  1. var paths = require('react-scripts-ts/config/paths')
  2.  
  3. module.exports = function override(config) {
  4. config.module.rules.push({
  5. test: /\.(js|jsx)$/,
  6. include: paths.appSrc,
  7. loader: require.resolve('babel-loader'),
  8. options: {
  9. babelrc: false,
  10. presets: [require.resolve('babel-preset-react-app')],
  11. cacheDirectory: true,
  12. },
  13. });
  14. config.module.rules.push({
  15. test: /\.(ts|tsx)$/,
  16. include: paths.appSrc,
  17. loader: ['ts-loader'],
  18. });
  19.  
  20. return config
  21. }

Import typescript file inside javascript file

https://stackoverflow.com/questions/48389087/import-typescript-file-inside-javascript-file?r=SearchResults  

https://mikebridge.github.io/post/getting-started-typescript-react/  

  1. How do I change `src` folder to something else in create-react-app
  1. const path = require('path');
  2.  
  3. module.exports = {
  4. paths: function (paths, env) {
  5. paths.appIndexJs = path.resolve(__dirname, 'mysrc/client.js');
  6. paths.appSrc = path.resolve(__dirname, 'mysrc');
  7. return paths;
  8. },
  9. }
  1. https://stackoverflow.com/questions/44448851/how-do-i-change-src-folder-to-something-else-in-create-react-app/54625811?r=SearchResults#54625811

 https://github.com/timarney/react-app-rewired#extended-configuration-options 

  1. module.exports = override(
  2. config => ({
  3. ...config,
  4. output: {
  5. ...config.output,
  6. globalObject: 'this',
  7. },
  8. }),
  9. addWebpackModuleRule({
  10. test: /\.worker\.js$/,
  11. use: { loader: 'worker-loader' },
  12. }),
  13. // addWebpackModuleRule({
  14. // test: /\.wasm$/,
  15. // use: { loader: 'wasm-loader' },
  16. // }),
    //https://github.com/YumcoderCom/yumgram/blob/master/config-overrides.js
  1. module.exports = {
  2. webpack (config, env) {
  3. // Log current env
  4. console.log('Current env', env)
  5.  
  6. // Inject global vars
  7. config.plugins = (config.plugins || []).concat([new webpack.DefinePlugin({
  8. ...constants[env], ENV: `'${env}'`
  9. })])
  10.  
  11. config = override(
  12. // Inject lazy loading for ant design css
  13. fixBabelImports('import', {
  14. libraryName: 'antd',
  15. libraryDirectory: 'es',
  16. style: true,
  17. }),
  18. // And override styles
  19. addLessLoader({
  20. javascriptEnabled: true,
  21. modifyVars: {
  22. '@primary-color': '#3DBD7D',
  23. '@link-color': '#5678a4',
  24. '@success-color': '#3DBD7D',
  25. '@warning-color': '#ffaa00',
  26. '@error-color': '#E94A4C',
  27. '@border-color-base': '#3DBD7D',
  28. '@box-shadow-base' : 'none'
  29. }
  30. }),
  31. // Inject translation
  32. ...addBabelPlugins('@lingui/babel-plugin-transform-js', '@lingui/babel-plugin-transform-react')
  33. )(config, env)
  34.  
  35. // Return the webpack config
  36. return config
  37. }
  38. }

  

  1. const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
  2.  
  3. module.exports = function override(config, env) {
  4. config.resolve.plugins = config.resolve.plugins.filter(plugin => !(plugin instanceof ModuleScopePlugin));
  5.  
  6. return config;
  7. };

按照 npm 包页上的步骤操作(安装包并在包.json 文件中翻转调用),并使用与此类似的文件:config-overrides.js  

这将从使用的 WebPack 插件中删除 ModuleScopePlugin,但保留其余内容,并消除弹出的必要性。  


  1.  
  1. module.exports = config => {
  2. const scopePluginIndex = config.resolve.plugins.findIndex(
  3. ({ constructor }) => constructor && constructor.name === "ModuleScopePlugin"
  4. );
  5.  
  6. config.resolve.plugins.splice(scopePluginIndex, 1);
  7.  
  8. return config;
  9. };
  10.  
  11. 配置重写.js
  12. const { removeModuleScopePlugin } = require('customize-cra')
  13.  
  14. module.exports = removeModuleScopePlugin()
  15.  
  16. 配置重写.js
  17. const ModuleScopePlugin = require("react-dev-utils/ModuleScopePlugin");
  18. const path = require("path");
  19.  
  20. module.exports = function override(config) {
  21. config.resolve.plugins.forEach(plugin => {
  22. if (plugin instanceof ModuleScopePlugin) {
  23. plugin.allowedFiles.add(path.resolve("./config.json"));
  24. }
  25. });
  26.  
  27. //https://stackoverflow.com/questions/44114436/the-create-react-app-imports-restriction-outside-of-src-directory/58863926?r=SearchResults#
  28. return config;
  29. };
  30.  
  31. //Customizing antd Less or Css for create-react-app
  32. function addLessRule(config, env)
  33. {
  34. const { getLoader, loaderNameMatches } = require("react-app-rewired");
  35.  
  36. const fileLoader = getLoader(
  37. config.module.rules,
  38. rule => loaderNameMatches(rule, 'file-loader')
  39. );
  40.  
  41. const lessExtension = /\.less$/;
  42. fileLoader.exclude.push(lessExtension);
  43.  
  44. const cssRules = getLoader(
  45. config.module.rules,
  46. rule => String(rule.test) === String(/\.css$/)
  47. );
  48.  
  49. var lessModifyVars = {
  50. 'primary-color': '#990000',
  51. 'link-color': '#1DA57A',
  52. 'border-radius-base': '2px',
  53. 'layout-header-background' : '#f0f2f5'
  54. };
  55.  
  56. var lessLoaders = [
  57. { loader: 'style-loader'} ,
  58. { loader: 'css-loader' },
  59. { loader: 'less-loader', options: {
  60. modifyVars: lessModifyVars
  61. } }
  62. ];
  63.  
  64. lessRule = {
  65. test : lessExtension,
  66. use: lessLoaders,
  67. }
  68.  
  69. config.module["rules"].push( lessRule );
  70. return config
  71. }
  72. //https://stackoverflow.com/questions/44611056/customizing-antd-less-or-css-for-create-react-app/51109561?r=SearchResults#51109561
  73.  
  74. //django-webpack-loader not rendering react app using react-app-rewired
  75. var BundleTracker = require('webpack-bundle-tracker');
  76.  
  77. module.exports = {
  78. webpack: (config, env) => {
  79.  
  80. config.plugins.push(
  81. new BundleTracker({
  82. path: __dirname,
  83. filename: './build/webpack-stats.json'
  84. }),
  85. );
  86.  
  87. return config;
  88. },
  89. };
  90. //https://stackoverflow.com/questions/54241751/django-webpack-loader-not-rendering-react-app-using-react-app-rewired?r=SearchResults
  91.  
  92. //How do I debug a local typescript module used by an app created from create-react-app
  93. /* config-overrides.js */
  94. const rewireAliases = require("react-app-rewire-aliases");
  95. const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
  96.  
  97. var path = require("path");
  98.  
  99. module.exports = function override(config, env) {
  100. config.module.rules.push({
  101. test: /\.ts$/,
  102. use: ["awesome-typescript-loader"],
  103. enforce: "pre"
  104. });
  105.  
  106. config.resolve.plugins = config.resolve.plugins.filter(plugin => !(plugin instanceof ModuleScopePlugin));
  107.  
  108. config = rewireAliases.aliasesOptions({
  109. "@common": path.resolve(
  110. __dirname,
  111. `node_modules/my-common-module/src/index.ts`
  112. )
  113. })(config, env);
  114.  
  115. return config;
  116. };
  117. //https://stackoverflow.com/questions/57605815/how-do-i-debug-a-local-typescript-module-used-by-an-app-created-from-create-reac/57649317?r=SearchResults#57649317
  118.  
  119. //Create-react-app + react-app-rewired + typescript + antd
  120. const {
  121. getLoader,
  122. injectBabelPlugin
  123. } = require("react-app-rewired");
  124. const tsImportPluginFactory = require('ts-import-plugin')
  125.  
  126. module.exports = function override(config, env) {
  127. // do stuff with the webpack config...
  128. const tsLoader = getLoader(
  129. config.module.rules,
  130. rule =>
  131. rule.loader &&
  132. typeof rule.loader === 'string' &&
  133. rule.loader.includes('ts-loader')
  134. );
  135.  
  136. tsLoader.options = {
  137. getCustomTransformers: () => ({
  138. before: [
  139. tsImportPluginFactory([{
  140. libraryDirectory: 'es',
  141. libraryName: 'antd',
  142. style: 'css',
  143. }]),
  144. ]
  145. })
  146. };
  147.  
  148. return config;
  149. };
  150.  
  151. //https://stackoverflow.com/questions/49698564/create-react-app-react-app-rewired-typescript-antd/50022172?r=SearchResults#50022172
  1. optimization: {
  2. runtimeChunk: {
  3. name: 'manifest'
  4. },
  5. splitChunks: {
  6. maxInitialRequests: 10,
  7. cacheGroups: {
  8. vendor: {
  9. test: /(jquery|fastclick|vue.esm.js)/,
  10. name: 'vendor',
  11. chunks: 'all'
  12. },
  13. common: {
  14. test: /(\.(png|jpe?g|gif|svg))/,
  15. name: 'app',
  16. chunks: 'initial',
  17. minChunks: 10
  18. },
  19. app: {
  20. test: /(css[\\/]style.css)/,
  21. name: 'app',
  22. chunks: 'initial',
  23. minChunks: 1
  24. }
  25. }
  26. }
  27. },
  28. //你可以配置optimization,把你想要抽取的css,js,甚至是公用图片都抽取出来,例如:
  29. //https://segmentfault.com/q/1010000017990233/

  

  

自定义组件:

cra的更多相关文章

  1. 摄像头模组光学CRA(chief ray angle)

    http://blog.csdn.net/sylorchen/article/details/54618874 Lens CRA CRA(Chief Ray Angle):从镜头的传感器一侧,可以聚焦 ...

  2. camera中LENS和SENSOR的CRA是如何搭配的?

    camera中LENS和SENSOR的CRA是如何搭配的? camera中,lens和sensor的搭配是非常关键的问题.但这两者是如何搭配的呢? 一般在Sensor data sheet中会附有全视 ...

  3. react CRA antd 按需加载配置 lessloader

    webpack配置 webpack.config.dev.js, webpack.config.prod同理. 'use strict'; const autoprefixer = require(' ...

  4. 使用CRA开发的基于React的UI组件发布到内网NPM上去

    前言:构建的ES组件使用CNPM发布内网上过程 1. 使用Create-React-APP开的组件 如果直接上传到NPM,你引用的时候会报: You may need an appropriate l ...

  5. [React] Remove React PropTypes by using Flow Annotations (in CRA)

    Starting from v15.5 if we wanted to use React's PropTypes we had to change our code to use a separat ...

  6. cacert.pem

    ## ## Bundle of CA Root Certificates ## ## Certificate data from Mozilla as of: Wed Sep 14 03:12:05 ...

  7. java web学习总结(七) -------------------HttpServletResponse对象(一)

    Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...

  8. STM32F105解密STM32F105VB芯片解密STM32F105R8单片机破解多少钱?

    STM32F105解密STM32F105VB芯片解密STM32F105R8单片机破解 STM32F105芯片Cortex-M3单片机解密: [凯基迪科技] STM32F105R8解密 | STM32F ...

  9. JMeter学习-038-JMeter Linux 环境配置

    1.1.下载 Download URL:http://mirrors.tuna.tsinghua.edu.cn/apache//jmeter/binaries/apache-jmeter-3.0.tg ...

随机推荐

  1. HanLP《自然语言处理入门》笔记--1.新手上路

    1. 新手上路 自然语言处理(Natural Language Processing,NLP)是一门融合了计算机科学.人工智能及语言学的交叉学科,它们的关系如下图所示.这门学科研究的是如何通过机器学习 ...

  2. Shell使用技巧之逐行读取

    重定向读取 #!/bin/bash while read line do echo $line done < /etc/passwd 管道读取 #!/bin/bash cat /etc/pass ...

  3. 文件系统(01):基于SpringBoot框架,管理Excel和PDF文件类型

    本文源码:GitHub·点这里 || GitEE·点这里 一.文档类型简介 1.Excel文档 Excel一款电子表格软件.直观的界面.出色的计算功能和图表工具,在系统开发中,经常用来把数据转存到Ex ...

  4. Hibernate(五)

    ================================criteria(QBC)查询========================QBC,(容器)又名对象查询:采用对象的方式(主要是cri ...

  5. python——3种字符串反转方法

    在学习过程中,总结了3种字符串反转方法: 1.切片法 这种方法最为简便 str='abad' print(str[::-1]) · 用切片操作,将字符串以步长-1重新整理,即 'str[-1],str ...

  6. thinkphp3关闭Runtime中的日志方法

    将LOG_LEVEL允许记录的日志级别设置为空,则不会记录日志

  7. Codevs 1205 单词反转(Vector以及如何输出string)

    题意:倒序输出句子中的单词 代码: #include<cstdio> #include<iostream> #include<string> #include< ...

  8. Java程序员必备英文单词

    列表中共有769个单词,这些单词是从JDK.Spring.SpringBoot.Mybatis的源码中解析得到,按照在源码中出现的频次依次排列,页面中的单词是出现频次大于1000的.单词的音标.翻译结 ...

  9. Linux 性能分析 工具命令

    背景知识:具备背景知识是分析性能问题时需要了解的.比如硬件 cache:再比如操作系统内核.应用程序的行为细节往往是和这些东西互相牵扯的,这些底层的东西会以意想不到的方式影响应用程序的性能,比如某些程 ...

  10. HSRP 详解

    简介 HSRP(Hot Standby Router Protocol 热备份路由器协议)是Cisco的专有协议.HSRP把多台路由器组成一个“热备份组”,形成一个虚拟路由器.这个组内只有一个路由器是 ...