Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
webpack-ant-icon-loader
Advanced tools
用于解决 在引入ant-design
3.x
版本后(全量引入@ant-design/icons
图标文件),导致打包生成的主文件较大的问题。
npm
npm install webpack-ant-icon-loader --save-dev
yarn
yarn add webpack-ant-icon-loader --dev
webpack.config.js
module.exports = {
module:{
rules:[
{
loader:'webpack-ant-icon-loader',
enforce: 'pre',
include:[
require.resolve('@ant-design/icons/lib/dist')
]
}
]
}
}
.umirc.js
export default {
chainWebpack(config, { webpack }) {
// code split @ant-design/icons
config.module
.rule('@ant-design/icons')
.include.add(require.resolve('@ant-design/icons/lib/dist')).end()
.use('ant-icon')
.loader('webpack-ant-icon-loader');
},
}
react-app-rewired
config-overrides.js
const path = require('path');
module.exports = {
// The Webpack config to use when compiling your react app for development or production.
webpack: function (config, env) {
// ...add your webpack config
// add webpack-ant-icon-loader
config.module.rules.push({
loader: 'webpack-ant-icon-loader',
enforce: 'pre',
include: [
require.resolve('@ant-design/icons/lib/dist')
]
})
return config;
},
}
customize-cra
config-overrides.js
const path = require('path');
const { override } = require('customize-cra');
module.exports = override(
// add webpack-ant-icon-loader
(config) => {
config.module.rules.push({
loader: 'webpack-ant-icon-loader',
enforce: 'pre',
include: [
require.resolve('@ant-design/icons/lib/dist')
]
});
return config;
},
);
主要通过webpack
代码拆分(import
)来完成体积优化:
把@antd-design/icons/lib/dist
的图标文件拆成独立的chunk
,且异步加载后自动注册该图标文件图标。
由于ant design
内部组件使用了很多图标,所以不考虑按需加载图标的方案,仅仅是将图标文件拆分出来,减少主文件
的体积,(类似于加载独立的字体文件)。
通过webpack-ant-icon-loader
提供的runtime.js
来完成延迟加载图标的刷新。
由于使用了异步加载图标文件的原因,在首次进入页面后,会导致已渲染的图标组件但是还未注册的图标
会出现空白,解决这个问题,需要触发一次组件刷新, 所以通过runtime.js
在异步图标文件加载完成后,从最小层面(仅刷新已渲染但是未注册的的图标
)进行一次性组件刷新。刷新完毕后,runtime.js
将不再介入操作,一切回归原始流程。
ant design
推出了解决方案,仅需要从webpack.config.js
中移除当前loader
即可。基于 MIT License 开源,使用代码只需说明来源,或者引用 license.txt 即可。
FAQs
ant design icons code split loader
The npm package webpack-ant-icon-loader receives a total of 468 weekly downloads. As such, webpack-ant-icon-loader popularity was classified as not popular.
We found that webpack-ant-icon-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.