【发布时间】:2020-06-05 13:04:45
【问题描述】:
我正在开发一个使用 next.js (^9.1.1) 和 @zeit/next-less (^1.0.1) 的 Web 应用程序。我正在努力提高这个应用程序的性能。我正在使用 LightHouse 来衡量性能。
LightHouse 机会部分正在展示这一点 -
chrome dev tools 的覆盖部分显示 styles.chunk.css 文件中 97.5% 的 CSS 未被使用,我认为这是因为它包含了我的 Next App 几乎所有页面的 CSS -
我有两个问题: 1、这个styles.chunk.css文件是做什么的? 2. 如何减小此文件的大小,使其仅包含特定页面所需的样式?
我尝试过使用 next-purgecss,但是 purgecss 只能在开发模式下工作,不能在生产模式下工作,我的配置文件写在下面 -
module.exports = withPlugins(
[
withLess(withPurgeCss({
purgeCssEnabled: ({ dev, isServer }) => (!dev && !isServer),
cssModules: true,
cssLoaderOptions: {
getLocalIdent: (loaderContext, localIdentName, localName, options) => {
const fileName = path.basename(loaderContext.resourcePath);
const shoudTransform = canBeTransformed(loaderContext.resourcePath);
if (!shoudTransform) {
return localName;
}
const name = fileName.replace(/\.[^/.]+$/, '');
return `${name}___${localName}`;
},
},
})),
// withBundleAnalyzer({}),
],
nextConfig,
);
【问题讨论】:
-
您能否使用 CSS 模块实现这一点?我的样式不适用,因为 purgeCss 模块无法识别 CSS 模块
标签: javascript next.js progressive-web-apps lighthouse