This article is a mirror article of machine translation, please click here to jump to the original article.

View: 2664|Reply: 2

[Tips] ASP.NET Core (27) Performance optimization PurgeCSS to remove unused code

[Copy link]
Posted on 5/8/2024 7:13:56 PM | | | |
Requirements: In a project, almost all front-end frameworks are referenced, such as: TailwindCSS, Bootstrap, MaterializeCSS, Foundation, etc., we may only use some of the CSS styles in it, and when publishing, we can delete unused CSS code, thusSpeed up website access, save bandwidth and browser memoryWait.

PurgeCSS is a tool for removing unused CSS. It can be part of your development workflow.
When you build a website, you may decide to use a CSS framework like TailwindCSS, Bootstrap, MaterializeCSS, Foundation, etc...... But you'll only use a small portion of the framework and will contain a lot of unused CSS styles.

This is where PurgeCSS comes into play. PurgeCSS analyzes your content and CSS files. It then matches the selectors used in the file with those in the content file. It removes unused selectors from CSS, resulting in smaller CSS files.

Website:The hyperlink login is visible.
GitHub address:The hyperlink login is visible.

First, we can install the PurgeCSS package globally using npm with the following command:


Create a new ASP.NET Core MVC project using VS, and the project will use the Bootstrap 5 style by default and load in the browserbootstrap.min.cssFile size163kb, the start project is as follows:



Start optimizing with PurgeCSS, creating a new one in the project rootpurgecss.config.jsThe configuration file reads as follows:

The PurgeCSS configuration file reference is as follows:

interface UserDefinedOptions {
  content: Array<string | RawContent>;
  css: Array<string | RawCSS>;
  defaultExtractor?: ExtractorFunction;
  extractors?: Array<Extractors>;
  fontFace?: boolean;
  keyframes?: boolean;
  output?: string;
  rejected?: boolean;
  rejectedCss?: boolean;
  stdin?: boolean;
  stdout?: boolean;
  variables?: boolean;
  safelist?: UserDefinedSafelist;
  blocklist?: StringRegExpArray;
}

interface RawContent {
  extension: string
  raw: string
}

interface RawCSS {
  raw: string
}

type StringRegExpArray = Array<RegExp | string>;

type ComplexSafelist = {
  standard?: StringRegExpArray;
  deep?: RegExp[];
  greedy?: RegExp[];
  variables?: StringRegExpArray;
  keyframes?: StringRegExpArray;
};

type UserDefinedSafelist = StringRegExpArray | ComplexSafelist;
Publish the ASP.NET Core to a folder and use PurgeCSS to remove unused CSS styles via the command line, the code is as follows:

As shown below:



Start the published project and view it in your browserbootstrap.min.cssThe optimized size is20.8 kb, as shown in the figure below:



This means that the bootstrap.min.css source file size is 163kb, and we only use the 20.8kb class style inside, and PurgeCSS helps us142 kb of content was removed

(End)




Previous:ASP.NET MVC uses Autofac to add global filters
Next:ASP.NET Core (twenty-eight) stores application secrets in development
 Landlord| Posted on 5/8/2024 9:07:25 PM |
You can also automate the execution of commands after the .csproj release is complete, as shown in the figure below:




The configuration is as follows:


The default priority for this Message task is Normal, but the default log level for VS is Minimal. If you want the message to be visible at the default log level, use Importance to set to high.
 Landlord| Posted on 5/16/2024 10:47:52 AM |
Command line, without using a configuration file:

Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com