亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

React腳手架config-overrides.js文件的配置方式

 更新時(shí)間:2023年10月20日 15:07:54   作者:Goat惡霸詹姆斯  
這篇文章主要介紹了React腳手架config-overrides.js文件的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

React腳手架config-overrides.js文件配置

主要講講在react腳手架在不使用eject命令的情況下,如何進(jìn)行webpack的配置。

網(wǎng)上查詢了好多,只有針對(duì)相關(guān)的配置,這次全面的看一看配置。

還是一樣,我們需要插件的幫助:

npm install react-app-rewired customize-cra --save-dev

配置package.json:

 
"scripts": {
  "start": "react-app-rewired start",
  "build": "react-app-rewired build",
  "test": "react-app-rewired test --env=jsdom",
  "eject": "react-scripts eject"
}

在根目錄下創(chuàng)建文件config-overrides.js,這里主要是由插件customize-cra來實(shí)現(xiàn),配置包含兩部分,customizer和utilities

const { override, addTslintLoader,addExternalBabelPlugins,addBabelPlugins,addBabelPresets,babelInclude,babelExclude,removeInternalBabelPlugin,fixBabelImports,addDecoratorsLegacy,disableEsLint,useEslintRc,enableEslintTypescript,addWebpackAlias,addWebpackResolve,addWebpackPlugin,addWebpackExternals,addWebpackModuleRule,setWebpackTarget,setWebpackStats,addBundleVisualizer,addBundleVisualizer,adjustWorkbox,useBabelRc,adjustWorkbox,addLessLoader,addPostcssPlugins,disableChunk} = require("customize-cra");
const path = require("path");
 
module.exports = override(
        //customizers
 
        //addTslintLoader(loaderOptions),需要安裝tslint-loader
        addTslintLoader(),
 
        //addExternalBabelPlugins(plugins)/addExternalBabelPlugin(plugin),create-reat-app有2種方式配置babel-loader,一種是默認(rèn)配置,另外一種通過external loader配置,在這里可以使用addExternalBabelPlugin添加插件,同樣你可以使用addBabelPlugin,作用是一樣的。注意如果添加多個(gè)插件要使用拓展符。
        ...addExternalBabelPlugins(
            "babel-plugin-transform-do-expressions",
            "@babel/plugin-proposal-object-rest-spread"
        ),
 
        //addBabelPlugin(plugin)/addBabelPlugins(plugins)
        ...addBabelPlugins(
            "polished",
            "emotion",
            "babel-plugin-transform-do-expressions"
        ),
 
        //addBabelPreset(preset)/addBabelPresets(...presets)添加babel preset配置,用法如下,與上述的addExternalBabelPlugins寫法相同
        ...addBabelPresets(
            [
              "@babel/env",
              {
                targets: {
                  browsers: ["> 1%", "last 2 versions"]
                },
                modules: "commonjs"
              }
            ],
            "@babel/preset-flow",
            "@babel/preset-react"
        ),
 
        //babelInclude(exclude)重寫babel-loader的exclude配置
        babelInclude([
            path.resolve("src"), // make sure you link your own source
            path.resolve("node_modules/native-base-shoutem-theme"),
            path.resolve("node_modules/react-navigation"),
            path.resolve("node_modules/react-native-easy-grid")
        ]),
 
        //babelExclude(exclude)重寫babel-loader的exclude配置
        babelExclude([path.resolve("src/excluded-folder")]),
 
        //removeInternalBabelPlugin(pluginName)從配置中移除構(gòu)造函數(shù)名稱與pluginname匹配的特定babel插件
        removeInternalBabelPlugin("plugin-name"),
 
        //fixBabelImports(libraryName, options),添加 babel-plugin-import 插件,主要用于按需引入
        fixBabelImports("lodash", {
            libraryDirectory: "",
            camel2DashComponentName: false
        }),
        fixBabelImports("react-feather", {
            libraryName: "react-feather",
            libraryDirectory: "dist/icons"
        }),
 
        //addDecoratorsLegacy()開啟修飾符,先安裝@babel/plugin-proposal-decorators插件
        addDecoratorsLegacy(),
 
        //disableEsLint()您可能需要將它與addDecoratorsLegacy一起使用,以便讓裝飾器和導(dǎo)出一起解析。如果你想在EsLint中使用@babel/plugin-proposal-decorators,你可以啟用useEslintRc,如下所述,在你的.eslintrc或package.json中eslintonfig下進(jìn)行以下配置:
            //"parserOptions": {
            //    "ecmaFeatures": {
            //      "legacyDecorators": true
            //    }
            // }
        disableEsLint(),
 
        //useEslintRc(configFile)configFile是一個(gè)可選參數(shù),允許指定ESLint配置文件的確切路徑。配置eslint的規(guī)范
        useEslintRc(configFile),
 
        //enableEslintTypescript()更新Webpack eslint-loader來檢測(cè).js(x)和.ts(x)文件,并在控制臺(tái)上顯示檢測(cè)錯(cuò)誤/警告。
        enableEslintTypescript(),
 
        //addWebpackAlias(alias)將提供的別名信息添加到webpack的別名部分。傳遞一個(gè)帶有任意數(shù)量條目的對(duì)象文字,整個(gè)對(duì)象將被合并。
        addWebpackAlias({
            '@': path.resolve('./src')
        }),
 
        //addWebpackResolve(resolve)導(dǎo)入文件的時(shí)候可以不用添加文件的后綴名
        addWebpackResolve({
            extensions: ['.tsx', '.ts', '.js', '.jsx', '.json'],
        }),
        
        //addWebpackPlugin(plugin)添加webpack插件,會(huì)被合并到plugins數(shù)組中
        addWebpackPlugin(new MiniCssExtractPlugin()),
 
        //addWebpackExternals(deps) 添加外部依賴,在嘗試將庫(kù)卸載到CDN時(shí)非常有用。addWebpackExternals也可以接受字符串、函數(shù)或正則表達(dá)式。有關(guān)更多信息,請(qǐng)參閱webpack文檔。
        addWebpackExternals({ //如直接以cdn引入的
            react: "React",
            "react-dom": "ReactDom"
            echarts: "window.echarts",
            // highcharts:"window.highcharts"
        }),
 
        //addWebpackModuleRule(rule)將提供的規(guī)則添加到webpack配置的module.rules數(shù)組中
        addWebpackModuleRule({test: /\.txt$/, use: 'raw-loader'}),
 
        //setWebpackTarget(target)為webpack設(shè)置目標(biāo)配置變量。正如webpack文檔中描述的那樣,這可以是一個(gè)字符串或一個(gè)函數(shù)。如: webpack默認(rèn)打包的target是瀏覽器模式。因?yàn)槲易龅氖且粋€(gè)nwjs項(xiàng)目,所以需要生成node-webkit的包。
        setWebpackTarget('node-webkit')
 
        
        //setWebpackStats(stats)設(shè)置webpack的stats屬性。這個(gè)屬性允許你在產(chǎn)品版本中定制Webpack的錯(cuò)誤消息行為。正如webpack文檔中描述的那樣,這可以是一個(gè)字符串或一個(gè)對(duì)象。
        setWebpackStats('error-only'), //或者如下對(duì)象形式
        setWebpackStats({
            warningsFilter: [
                'filter',
                /filter/,
                (warning) => true
            ]
        }),
 
        //addBundleVisualizer(options, behindFlag = false)將bundle可視化插件添加到webpack配置中。確保安裝了webpack-bundle-analyzer。默認(rèn)情況下,傳遞給插件的選項(xiàng)是:
        //{
          //"analyzerMode": "static",
          //"reportFilename": "report.html"
        //}
        addBundleVisualizer({}, true),
 
        //setWebpackOptimizationSplitChunks(target)設(shè)置自定義優(yōu)化。splitChunks配置到你的webpack配置。請(qǐng)謹(jǐn)慎使用此方法,因?yàn)閣ebpack默認(rèn)配置在大多數(shù)時(shí)候是有效的。默認(rèn)情況下,create-react-app中的選項(xiàng)是:
        //{
          //"chunks": "all",
          //"name": false
        //}
        addBundleVisualizer({}, true),
        
        //useBabelRc()啟用你的.babelrc(或.babelrc.js)
        useBabelRc(),
 
        //adjustWorkbox(fn)調(diào)整Workbox配置。傳遞一個(gè)函數(shù),該函數(shù)將與當(dāng)前Workbox配置一起調(diào)用,其中您可以根據(jù)需要更改配置對(duì)象。請(qǐng)看下面的示例。
        adjustWorkbox(wb =>
            Object.assign(wb, {
                skipWaiting: true,
                exclude: (wb.exclude || []).concat("index.html")
            }),
        );
 
        //addLessLoader(loaderOptions)添加less-loader,
        //npm i -D less less-loader
        addLessLoader({
            strictMath: true,
            noIeCompat: true,
            modifyVars: {
              "@primary-color": "#1DA57A", // 例如, 你使用 Ant Design 改變主題色.
            },
            cssLoaderOptions: {}, // .less 文件被使用在 css-loader 選項(xiàng)
            cssModules: {
                localIdentName: "[path][name]__[local]--[hash:base64:5]", 
            },
        }),
        //如果你在CSS模塊中使用TypeScript (npm init react-app my-app——TypeScript),你應(yīng)該編輯react-app-env.d.ts。
        //declare module "*.module.less" {
          //const classes: { [key: string]: string };
          //export default classes;
        //}
        
        //addPostcssPlugins([plugins])添加postCss插件
         addPostcssPlugins([require("postcss-px2rem")({ remUnit: 37.5 })]),
 
        //disableChunk()防止默認(rèn)的靜態(tài)分塊,并強(qiáng)制整個(gè)構(gòu)建到一個(gè)文件中。
        disableChunk(),
 
        //removeModuleScopePlugin()這將刪除CRA插件,防止從src目錄外導(dǎo)入模塊,如果你使用不同的目錄,這很有用。一個(gè)常見的用例是如果您在monorepo設(shè)置中使用CRA,其中您的包在packages/而不是src/下。
        removeModuleScopePlugin(),
 
        //watchAll()當(dāng)應(yīng)用時(shí),CRA將監(jiān)視項(xiàng)目的所有文件,包括node_modules。要使用它,只需應(yīng)用它并使用yarn start運(yùn)行開發(fā)服務(wù)器——watch-all
        watchAll(),
 
        //adjustStyleLoaders(callback)逐一找到所有樣式加載器和回調(diào)
        adjustStyleLoaders(({ use: [ , css, postcss, resolve, processor ] }) => {
          css.options.sourceMap = true;         // css-loader
          postcss.options.sourceMap = true;     // postcss-loader
          // when enable pre-processor,
          // resolve-url-loader will be enabled too
          if (resolve) {
            resolve.options.sourceMap = true;   // resolve-url-loader
          }
          // pre-processor
          if (processor && processor.loader.includes('sass-loader')) {
            processor.options.sourceMap = true; // sass-loader
          }
        })       
);

customizer整體配置就是這些。

utilities還有兩個(gè)公共方法:

getBabelLoader(config, isOutsideOfApp)

從提供的配置返回babel加載器。

Create-react-app定義了兩個(gè)Babel配置,一個(gè)用于src/目錄下的js文件,另一個(gè)用于該目錄外的任何js文件。

這個(gè)函數(shù)可以使用isOutsideOfApp參數(shù)。

tap(options)

通過在控制臺(tái)中或單獨(dú)的文件中打印配置,使用tap幫助您在某些點(diǎn)上識(shí)別配置。

Tap接受一個(gè)帶有next屬性的可選選項(xiàng)對(duì)象:

  • message:配置前要打印的字符串消息
  • dest:寫日志的目的文件
const { override, tap, addLessLoader } = require("customize-cra");
 
module.exports = override(
  // Print initial config in the console prepending a message
  tap({ message: "Pre - Customizers" }) 
  /* Your customizers: eg. addLessLoader() */
  addLessLoader()
  // Print final config in a separate file
  tap({ dest: 'customize-cra.log'}) 
)

補(bǔ)充:

另外一種拋出對(duì)象的寫法 ,我們想要訪問webpack里一些特定模塊:

const {override, fixBabelImports, addLessLoader, overrideDevServer, watchAll} = require('customize-cra')
const packageName = require('./package.json').name
 
 
module.exports = {
 
  'webpack': override(
    (config) => {
      config.output = config.output || {}
      config.output.library = `${packageName}-[name]`
      config.output.libraryTarget = 'umd'
      config.output.jsonpFunction = `webpackJsonp_${packageName}`
      return config
    },
 
    fixBabelImports('import', {
      libraryName: 'antd',
      libraryDirectory: 'es',
      style: true,
    }),
 
    addLessLoader({
      javascriptEnabled: true,
      modifyVars: {
        'primary-color': '#1DA57A',
        'link-color': '#1DA57A',
        'border-radius-base': '2px',
      },
    })
  ),
 
  'devServer': overrideDevServer(
    (config) => {
      config.headers = config.headers || {}
      config.headers['Access-Control-Allow-Origin'] = '*'
      return config
    },
    watchAll()
  )
 
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • react中實(shí)現(xiàn)拖拽排序react-dnd功能

    react中實(shí)現(xiàn)拖拽排序react-dnd功能

    這篇文章主要介紹了react中實(shí)現(xiàn)拖拽排序react-dnd功能,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-02-02
  • useReducer使用詳解及其應(yīng)用場(chǎng)景

    useReducer使用詳解及其應(yīng)用場(chǎng)景

    這篇文章主要介紹了useReducer使用詳解及其應(yīng)用場(chǎng)景,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • React中映射一個(gè)嵌套數(shù)組實(shí)現(xiàn)demo

    React中映射一個(gè)嵌套數(shù)組實(shí)現(xiàn)demo

    這篇文章主要為大家介紹了React中映射一個(gè)嵌套數(shù)組實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • React虛擬列表的實(shí)現(xiàn)

    React虛擬列表的實(shí)現(xiàn)

    在開發(fā)過程中,總是遇到很多列表的顯示。當(dāng)上數(shù)量級(jí)別的列表渲染于瀏覽器,終會(huì)導(dǎo)致瀏覽器的性能下降,你可以選擇其他方式避免,本文就介紹了虛擬列表來解決這個(gè)問題
    2021-05-05
  • React實(shí)現(xiàn)類似淘寶tab居中切換效果的示例代碼

    React實(shí)現(xiàn)類似淘寶tab居中切換效果的示例代碼

    這篇文章主要介紹了React實(shí)現(xiàn)類似淘寶tab居中切換效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • react?源碼中位運(yùn)算符的使用詳解

    react?源碼中位運(yùn)算符的使用詳解

    這篇文章主要為大家詳細(xì)介紹了react?位運(yùn)算符,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • react-router-domV6嵌套路由實(shí)現(xiàn)詳解

    react-router-domV6嵌套路由實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了react-router-domV6嵌套路由實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • React掌握openapi-typescript-codegen快速生成API客戶端代碼的過程

    React掌握openapi-typescript-codegen快速生成API客戶端代碼的過程

    openapi-typescript-codegen是一個(gè)開源工具,用于根據(jù)OpenAPI規(guī)范自動(dòng)生成TypeScript代碼,包括類型定義和API客戶端代碼,它幫助開發(fā)者節(jié)省手動(dòng)編寫代碼的時(shí)間,提高開發(fā)效率,感興趣的朋友一起看看吧
    2024-11-11
  • react-three/postprocessing庫(kù)的參數(shù)中文含義使用解析

    react-three/postprocessing庫(kù)的參數(shù)中文含義使用解析

    這篇文章主要介紹了react-three/postprocessing庫(kù)的參數(shù)中文含義使用總結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • 關(guān)于react中列表渲染的局部刷新問題

    關(guān)于react中列表渲染的局部刷新問題

    這篇文章主要介紹了關(guān)于react中列表渲染的局部刷新問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評(píng)論