웹 프론트엔드 CSS 튜토리얼 Knip 및 PurgeCSS를 사용하여 사용하지 않는 파일 및 종속성을 제거하여 성능 향상

Knip 및 PurgeCSS를 사용하여 사용하지 않는 파일 및 종속성을 제거하여 성능 향상

Oct 06, 2024 am 06:10 AM

As developers, we're constantly striving for better performance. Whether it's faster load times, more responsive UIs, or smaller bundle sizes, performance is a key metric that directly impacts user experience. One overlooked area that can significantly improve performance is cleaning up unused files and dependencies. In this article, I’ll explore how identifying and removing dead code, unused packages, and unnecessary CSS can lead to a leaner, faster project. We'll also take a look at how tools like Knip and PurgeCSS can help automate this process.

Why Cleaning Up Unused Code Matters

Over time, it's easy for projects to accumulate unnecessary files, dependencies, and CSS rules that are no longer in use. These leftovers bloat your project, making it slower to load, harder to maintain, and potentially impacting performance in ways you don’t immediately realize.

By regularly removing unused code, you not only shrink the size of your JavaScript bundles and CSS files but also improve load times, reduce memory usage, and decrease the overall footprint of your application. This practice is especially critical for front-end performance, where every kilobyte counts.

Tools for the Job: Knip and PurgeCSS

Now that we understand the importance of keeping a project tidy, let’s briefly discuss the tools we can use to automate this process. Knip is a tool designed to analyze your JavaScript project and identify unused dependencies and files, while PurgeCSS focuses on cleaning up unused CSS classes from your stylesheets. Together, these tools help ensure that only the code you're actually using makes it to production.

Improving Performance: The Workflow

  1. Audit Your Dependencies with Knip The first step to cleaning up your project is auditing your dependencies and modules. Knip scans your project, analyzes the imports, and provides a report of what's in use and what can be safely removed.

Instalation and Setup


npm init @knip/config


로그인 후 복사

Running


npm run knip


로그인 후 복사

Knip generates a report highlighting unused dependencies and files. This allows you to quickly identify which parts of your project are no longer necessary, helping you reduce bundle size and improve performance.

Example output
Boosting Performance by Removing Unused Files and Dependencies with Knip and PurgeCSS

With this summary, you can safely review your files.

2. Optimize Your CSS with PurgeCSS
CSS, just like JavaScript, can become bloated over time. PurgeCSS is a tool that removes unused CSS selectors from your stylesheets, ensuring that your project only ships the necessary styles.

Instalation


npm i -D @fullhuman/postcss-purgecss postcss


로그인 후 복사

Run PurgeCSS CLI in postbuild

I prefer using it as a post-build script, but you can configure the command for any file you need. Simply specify your build folder and the path to the script.


"scripts": {
      "postbuild": "purgecss --css dist/assets/*.css --content dist/index.html dist/assets/*.js --output dist/assets"
},


로그인 후 복사

With this setup, PurgeCSS will automatically strip away unused CSS classes, leaving you with a much smaller and optimized stylesheet.
Checkout PurgeCSS docs to more details.

3. Regularly Review and Refactor

While tools like Knip and PurgeCSS automate a large part of the cleanup process, regular reviews of your codebase are still essential. Make it a habit to refactor old code, remove unused components, and ensure that your project remains lightweight and easy to maintain. Automating this process with CI/CD pipelines can also help you keep your codebase lean and performant as you continue developing.

Conclusion

Improving the performance of your application isn’t just about writing efficient code; it’s also about keeping your project clean and free of unnecessary files and dependencies. Regularly removing unused code not only reduces the size of your project but also improves load times, simplifies maintenance, and enhances the user experience. Tools like Knip and PurgeCSS make it easier to automate this cleanup process, but the key takeaway is the habit of constantly auditing and optimizing your code.

By adopting this mindset and utilizing these tools, you can ensure your project remains fast, lean, and optimized for the best performance possible.

위 내용은 Knip 및 PurgeCSS를 사용하여 사용하지 않는 파일 및 종속성을 제거하여 성능 향상의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

<gum> : Bubble Gum Simulator Infinity- 로얄 키를 얻고 사용하는 방법
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
Nordhold : Fusion System, 설명
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora : 마녀 트리의 속삭임 - Grappling Hook 잠금 해제 방법
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

정적 양식 공급자의 비교 정적 양식 공급자의 비교 Apr 16, 2025 am 11:20 AM

"정적 양식 공급자"라는 용어를 동전하려고합시다. 당신은 당신의 HTML을 가져옵니다

Sass를 더 빨리 만들기위한 개념 증명 Sass를 더 빨리 만들기위한 개념 증명 Apr 16, 2025 am 10:38 AM

새로운 프로젝트가 시작될 때, Sass 컴파일은 눈을 깜박이게합니다. 특히 BrowserSync와 짝을 이루는 경우 기분이 좋습니다.

주간 플랫폼 뉴스 : HTML로드 속성, 주요 ARIA 사양 및 iframe에서 Shadow Dom으로 이동 주간 플랫폼 뉴스 : HTML로드 속성, 주요 ARIA 사양 및 iframe에서 Shadow Dom으로 이동 Apr 17, 2025 am 10:55 AM

이번 주에 플랫폼 뉴스 라운드 업 RONDUP, Chrome은로드에 대한 새로운 속성, 웹 개발자를위한 접근성 사양 및 BBC Move를 소개합니다.

HTML 대화 요소와 함께 일부 실습 HTML 대화 요소와 함께 일부 실습 Apr 16, 2025 am 11:33 AM

이것은 처음으로 HTML 요소를보고 있습니다. 나는 그것을 잠시 동안 알고 있었지만 아직 스핀을 위해 그것을 가져 갔다. 그것은 꽤 시원하고 있습니다

PaperForm PaperForm Apr 16, 2025 am 11:24 AM

구매 또는 빌드는 기술 분야의 고전적인 논쟁입니다. 신용 카드 청구서에 라인 항목이 없기 때문에 물건을 구축하는 것이 저렴할 수 있지만

주간 플랫폼 뉴스 : 텍스트 간격 북마크, 최상위 차단, 새로운 앰프 로딩 표시기 주간 플랫폼 뉴스 : 텍스트 간격 북마크, 최상위 차단, 새로운 앰프 로딩 표시기 Apr 17, 2025 am 11:26 AM

이번 주에 타이포그래피를 검사하기위한 편리한 북마크 인 Roundup, JavaScript 모듈과 Facebook의 Facebook 등을 어떻게 가져 오는지 땜질하기 위해 대기하는 편리한 북마크 인 Roundup과 Facebook의

직접 비자 스크립트 기반 분석을 호스팅하는 옵션 직접 비자 스크립트 기반 분석을 호스팅하는 옵션 Apr 15, 2025 am 11:09 AM

사이트에서 방문자 및 사용 데이터를 추적하는 데 도움이되는 분석 플랫폼이 많이 있습니다. 아마도 널리 사용되는 Google 웹 로그 분석

'Podcast 구독'링크는 어디에서 링크해야합니까? 'Podcast 구독'링크는 어디에서 링크해야합니까? Apr 16, 2025 pm 12:04 PM

한동안 iTunes는 팟 캐스팅에서 큰 개 였으므로 "Podcast 구독"을 링크 한 경우 다음과 같습니다.

See all articles