在create-react-app使用less與antd按需加載

使用antd按需加載

使用react-app-rewired對 create-react-app 的默認配置進行自定義

  1. yarn add react-app-rewired --dev
/* 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", } 

2.而後在項目根目錄建立一個 config-overrides.js 用於修改默認配置。javascript

module.exports = function override(config, env) { // do stuff with the webpack config... return config; }; 

使用 babel-plugin-import

babel-plugin-import 是一個用於按需加載組件代碼和樣式babel 插件(原理),如今咱們嘗試安裝它並修改config-overrides.js 文件css

1.yarn add babel-plugin-import --devjava

+ const { injectBabelPlugin } = require('react-app-rewired'); module.exports = function override(config, env) { + config = injectBabelPlugin(['import', { libraryName: 'antd', libraryDirectory: 'es', style: 'css' }], config); return config; }; 

或者也可修改node

{
            test: /\.(js|jsx|mjs)$/, include: paths.appSrc, loader: require.resolve('babel-loader'), options: { plugins: [ ['import', { libraryName: 'antd', style: true }], ], // This is a feature of `babel-loader` for webpack (not Babel itself). // It enables caching results in ./node_modules/.cache/babel-loader/ // directory for faster rebuilds. cacheDirectory: true, }, }, 

自定義主題

  • yarn add react-app-rewire-less --dev
const { injectBabelPlugin } = require('react-app-rewired'); + const rewireLess = require('react-app-rewire-less'); module.exports = function override(config, env) { - config = injectBabelPlugin(['import', { libraryName: 'antd', style: 'css' }], config); + config = injectBabelPlugin(['import', { libraryName: 'antd', style: true }], config); + config = rewireLess.withLoaderOptions({ + modifyVars: { "@primary-color": "#1DA57A" }, + })(config, env); return config; }; 

參考連接:https://ant.design/docs/react/use-with-create-react-app-cnreact

使用less

若是已經配置react-app-rewire-less,則無需再進行此操做

1.npm install less-loader less --save-dev
2.修改node_modules/react_script/config下的webpack.config.dev.js 和 webpack.config-prod.js 配置文件webpack

  • test: /.css$/ 改成 /.(css|less)$/
  • test: /.css$/ 的 use 數組配置增長 less-loader
{
  test: /\.(css|less)$/, use: [ require.resolve('style-loader'), { loader: require.resolve('css-loader'), options: { importLoaders: 1, }, }, { loader: require.resolve('postcss-loader'), options: { // Necessary for external CSS imports to work // https://github.com/facebookincubator/create-react-app/issues/2677 ident: 'postcss', plugins: () => [ require('postcss-flexbugs-fixes'), autoprefixer({ browsers: [ '>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 9', // React doesn't support IE8 anyway ], flexbox: 'no-2009', }), ], }, }, { loader: require.resolve('less-loader') // compiles Less to CSS } ], }, 

使用css Module

antd 和 css modules 不能混用,針對antd的css 單獨寫一條loader的規則,不開啓 css modules。

  1. 使用 exclude 和 include 配置(https://segmentfault.com/q/1010000011223900),修改webpack.config.dev.js 和 webpack.config-prod.js 配置文件 (儘可能不要使用less-loader 來處理css文件,在與antd一塊兒使用時可能出現錯誤,單獨寫一條規則)
{
            test: /\.css$/, exclude: /node_modules|antd\.css/, use: [ require.resolve('style-loader'), { loader: require.resolve('css-loader'), options: { importLoaders: 1, modules: true, // 新增對css modules的支持 localIdentName: '[name]__[local]__[hash:base64:5]', }, }, { loader: require.resolve('postcss-loader'), options: { // Necessary for external CSS imports to work // https://github.com/facebookincubator/create-react-app/issues/2677 ident: 'postcss', plugins: () => [ require('postcss-flexbugs-fixes'), autoprefixer({ browsers: [ '>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 9', // React doesn't support IE8 anyway ], flexbox: 'no-2009', }), ], }, }, ], }, { test: /\.less$/, exclude: /node_modules|antd\.less/, use: [ require.resolve('style-loader'), { loader: require.resolve('css-loader'), options: { importLoaders: 1, modules: true, // 新增對css modules的支持 localIdentName: '[name]__[local]__[hash:base64:5]', }, }, { loader: require.resolve('postcss-loader'), options: { // Necessary for external CSS imports to work // https://github.com/facebookincubator/create-react-app/issues/2677 ident: 'postcss', plugins: () => [ require('postcss-flexbugs-fixes'), autoprefixer({ browsers: [ '>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 9', // React doesn't support IE8 anyway ], flexbox: 'no-2009', }), ], }, }, { loader: require.resolve('less-loader') // compiles Less to CSS }, ], }, { test: /\.(css)$/, include: /node_modules|antd\.css/, use: [ require.resolve('style-loader'), { loader: require.resolve('css-loader'), options: { importLoaders: 1, }, }, { loader: require.resolve('postcss-loader'), options: { // Necessary for external CSS imports to work // https://github.com/facebookincubator/create-react-app/issues/2677 ident: 'postcss', plugins: () => [ require('postcss-flexbugs-fixes'), autoprefixer({ browsers: [ '>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 9', // React doesn't support IE8 anyway ], flexbox: 'no-2009', }), ], }, }, ], }, { test: /\.(less)$/, include: /node_modules|antd\.less/, use: [ require.resolve('style-loader'), { loader: require.resolve('css-loader'), options: { importLoaders: 1, }, }, { loader: require.resolve('postcss-loader'), options: { // Necessary for external CSS imports to work // https://github.com/facebookincubator/create-react-app/issues/2677 ident: 'postcss', plugins: () => [ require('postcss-flexbugs-fixes'), autoprefixer({ browsers: [ '>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 9', // React doesn't support IE8 anyway ], flexbox: 'no-2009', }), ], }, }, { loader: require.resolve('less-loader') // compiles Less to CSS }, ], }, 
做者:summer_味道製造 連接:https://www.jianshu.com/p/3550f919c3a6 來源:簡書 簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。
相關文章
相關標籤/搜索