共计 字 • 阅读约
min
nuxt.js 是服务端渲染的框架,性能优化非常重要。默认在项目中使用 ant-design 是全量引入的,这其中是有很多用不到的组件的,无形中增加的文件大小,从而拉低了页面加载速度,所以说,最简单的性能优化,就是按需引入
1. 安装 ant-design
npm i ant-design-vue -S
或者 cnpm i ant-design-vue -S
已安装的跳过这一步
2. 引入 ant-design 中自己需要的组件
在项目文件夹 plugin
下面新建一个 antd-ui.js
的文件
import Vue from "vue";
import {
Button,
Anchor,
Skeleton,
Icon,
Card,
BackTop,
Tag,
Tooltip,
Avatar,
Rate,
Affix,
Alert,
message
} from "ant-design-vue";
Vue.use(Button)
.use(Anchor)
.use(Skeleton)
.use(Icon)
.use(Card)
.use(BackTop)
.use(Tag)
.use(Tooltip)
.use(Avatar)
.use(Rate)
.use(Affix)
.use(Alert)
.use(message);
Vue.prototype.$message = message;
message.config({
top: `70px`,
duration: 2,
maxCount: 3
});
然后,在 nuxt.config.js
中使用,加在配置对象的 plugins 中
export default {
plugins: [
"@/plugins/antd-ui"
]
}
现在只是 js 文件按需加载了,还有这些组件的样式我们需要载入
3. 引入组件的样式
同样的也是找到配置对象 的根节点的 CSS
export default {
css: [
"ant-design-vue/lib/button/style/index.css",
"ant-design-vue/lib/anchor/style/index.css",
"ant-design-vue/lib/skeleton/style/index.css",
"ant-design-vue/lib/icon/style/index.css",
"ant-design-vue/lib/card/style/index.css",
"ant-design-vue/lib/back-top/style/index.css",
"ant-design-vue/lib/tag/style/index.css",
"ant-design-vue/lib/tooltip/style/index.css",
"ant-design-vue/lib/avatar/style/index.css",
"ant-design-vue/lib/rate/style/index.css",
"ant-design-vue/lib/affix/style/index.css",
"ant-design-vue/lib/alert/style/index.css",
"ant-design-vue/lib/message/style/index.css"
]
}
到这,还没有结束,如果只是这样的话,可能你就会发现,样式和原本的样式有一点点不一样。我们还需要引入 ant-design
的基础样式,大概是以下这些:
html,
body {
width: 100%;
height: 100%;
}
input::-ms-clear,
input::-ms-reveal {
display: none;
}
*,
*::before,
*::after {
box-sizing: border-box;
}
html {
font-family: sans-serif;
line-height: 1.15;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
-ms-overflow-style: scrollbar;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
@-ms-viewport {
width: device-width;
}
article,
aside,
dialog,
figcaption,
figure,
footer,
header,
hgroup,
main,
nav,
section {
display: block;
}
body {
margin: 0;
color: rgba(0, 0, 0, 0.65);
font-size: 14px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe1 UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
font-variant: tabular-nums;
line-height: 1.5;
background-color: #fff;
font-feature-settings: 'tnum';
}
[tabindex='-1']:focus {
outline: none !important;
}
hr {
box-sizing: content-box;
height: 0;
overflow: visible;
}
h1,
h2,
h3,
h4,
h5,
h6 {
margin-top: 0;
margin-bottom: 0.5em;
color: rgba(0, 0, 0, 0.85);
font-weight: 500;
}
p {
margin-top: 0;
margin-bottom: 1em;
}
abbr[title],
abbr[data-original-title] {
text-decoration: underline;
-webkit-text-decoration: underline dotted;
text-decoration: underline dotted;
border-bottom: 0;
cursor: help;
}
address {
margin-bottom: 1em;
font-style: normal;
line-height: inherit;
}
input[type='text'],
input[type='password'],
input[type='number'],
textarea {
-webkit-appearance: none;
}
ol,
ul,
dl {
margin-top: 0;
margin-bottom: 1em;
}
ol ol,
ul ul,
ol ul,
ul ol {
margin-bottom: 0;
}
dt {
font-weight: 500;
}
dd {
margin-bottom: 0.5em;
margin-left: 0;
}
blockquote {
margin: 0 0 1em;
}
dfn {
font-style: italic;
}
b,
strong {
font-weight: bolder;
}
small {
font-size: 80%;
}
sub,
sup {
position: relative;
font-size: 75%;
line-height: 0;
vertical-align: baseline;
}
sub {
bottom: -0.25em;
}
sup {
top: -0.5em;
}
a {
/* color: #1890ff; */
text-decoration: none;
background-color: transparent;
outline: none;
cursor: pointer;
transition: color 0.3s;
-webkit-text-decoration-skip: objects;
}
a:hover {
color: #40a9ff;
}
a:active {
color: #096dd9;
}
a:active,
a:hover {
text-decoration: none;
outline: 0;
}
a[disabled] {
color: rgba(0, 0, 0, 0.25);
cursor: not-allowed;
pointer-events: none;
}
pre,
code,
kbd,
samp {
font-size: 1em;
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;
}
pre {
margin-top: 0;
margin-bottom: 1em;
overflow: auto;
}
figure {
margin: 0 0 1em;
}
img {
vertical-align: middle;
border-style: none;
}
svg:not(:root) {
overflow: hidden;
}
a,
area,
button,
[role='button'],
input:not([type='range']),
label,
select,
summary,
textarea {
touch-action: manipulation;
}
table {
border-collapse: collapse;
}
caption {
padding-top: 0.75em;
padding-bottom: 0.3em;
color: rgba(0, 0, 0, 0.45);
text-align: left;
caption-side: bottom;
}
th {
text-align: inherit;
}
input,
button,
select,
optgroup,
textarea {
margin: 0;
color: inherit;
font-size: inherit;
font-family: inherit;
line-height: inherit;
}
button,
input {
overflow: visible;
}
button,
select {
text-transform: none;
}
button,
html [type="button"],
[type="reset"],
[type="submit"] {
-webkit-appearance: button;
}
button::-moz-focus-inner,
[type='button']::-moz-focus-inner,
[type='reset']::-moz-focus-inner,
[type='submit']::-moz-focus-inner {
padding: 0;
border-style: none;
}
input[type='radio'],
input[type='checkbox'] {
box-sizing: border-box;
padding: 0;
}
input[type='date'],
input[type='time'],
input[type='datetime-local'],
input[type='month'] {
-webkit-appearance: listbox;
}
textarea {
overflow: auto;
resize: vertical;
}
fieldset {
min-width: 0;
margin: 0;
padding: 0;
border: 0;
}
legend {
display: block;
width: 100%;
max-width: 100%;
margin-bottom: 0.5em;
padding: 0;
color: inherit;
font-size: 1.5em;
line-height: inherit;
white-space: normal;
}
progress {
vertical-align: baseline;
}
[type='number']::-webkit-inner-spin-button,
[type='number']::-webkit-outer-spin-button {
height: auto;
}
[type='search'] {
outline-offset: -2px;
-webkit-appearance: none;
}
[type='search']::-webkit-search-cancel-button,
[type='search']::-webkit-search-decoration {
-webkit-appearance: none;
}
::-webkit-file-upload-button {
font: inherit;
-webkit-appearance: button;
}
output {
display: inline-block;
}
summary {
display: list-item;
}
template {
display: none;
}
[hidden] {
display: none !important;
}
mark {
padding: 0.2em;
background-color: #feffe6;
}
::-moz-selection {
color: #fff;
background: #1890ff;
}
::selection {
color: #fff;
background: #1890ff;
}
.clearfix {
zoom: 1;
}
.clearfix::before,
.clearfix::after {
display: table;
content: '';
}
.clearfix::after {
clear: both;
}
最后来看一下按需引入后文件大小的变化
😰 按需引入之前:
🎉 按需引入之后:
效果还是很明显的 ✨ ,由原本的 2.19M 降到了 357.49kb(这只是我这个项目,不同项目按需引入的组件数量不用,最后的大小都不同)
如何生成自己项目的文件报告
上图的视图的生成,我们只需要在 nuxt.config.js 中修改一项配置就可以了
export default {
build: {
analyze: true
}
}
启用了后,运行打包命令 npm run build
后 就会自动生成报告,并在浏览器中打开
文章更新于: 2021-4-23 1:50:35