Table of Contents
Open Table of Contents
Introduction
In this post you’ll learn how to use the astro-renameastro integration to hash all CSS class names from your astro project.
What is astro-rename
The package astro-rename is an Astrointegration that uses postcss-renameto rename all CSS class names. This integration will also rename all JS/HTML files.
By default the postcss-rename only supports none
, debug
and minimal
strategies. So, we’ll use the strategy
option with a custom hash generator.
Create a new Astro project
Let’s get started creating a new Astro project:
All you have to do is follow the step-by-step instructions.
Running the Astro project
Once you have created your Astro project, you can start the dev server using:
Now, the Astro project is running on localhost using the default port :4321
:
Let’s take a look at the DevTools from this default project:
As you can see, all CSS class names are “exposed”.
Installing dependencies
First of all, let’s install the astro-renamedependency:
The astro-rename package require node >= 18.18
. You don’t actually need this version and can use the --ignore-engines
flag to bypass this error.
Set up astro integration
After installing the package, let’s import and include the integration:
Import dependency:
Add to integrations option:
The integration will only work when output is set to static
If you are using other integration like astro-compress, include the astro-rename before this integration
Testing with default options
At this point, you already can build the project using:
Now, run the output file using the VSCode Live Server extensionand take a look at the DevTools:
Note that all class names have been replaced with letters, by default the integration will use the minimal1 strategy.
The next step is to create our custom hash generator.
Generating hash names
We can take a look at the minimal renamermethod from postcss-rename plugin and uses as template for creating our custom hash generator.
First, create a new file named hash-renamer.ts
, with a HashRenamer
class:
- The
maxSize
will define the max length of the hashed name; - The
renames
will hold our old and new class names; - The
values
will hold ONLY the new class names;
The postcss-rename plugin will not keep the files context so, if the output has multiple CSS files, it will rename all renamed class names. That’s the reason we use the values
array to validate if some hashed name is used as key.
See more: Support multiple CSS files
Hash method
Now, let’s create the getHash
method. This function will loop from 0
to maxSize
and take a random letter from CHARS
constant.
Set up custom strategy
Open the astro.config.cjs
file and add the custom strategy:
When using this custom strategy, the prefix option from astro-rename will not work properly.
See my hash-renamer.ts fileto understand how i implemented CSS prefix on this website.
Testing with custom strategy option
Build the project:
Run the output file using the VSCode Live Server extensionand take a look at the DevTools:
All class names have been replaced with hash names.
Two notes before using this astro-rename integration:
1 - Avoid naming your classes with html tags. E.g: body, title, link, img, etc…
2 - Avoid repeating class names on differents pages/components.
Using any method above will replace and/or confuse the replacement of the names since the plugin don’t save the CSS context
Conclusion
There you have it. A quick and simple integration to hash all CSS class names from your final Astro website.
Here’s the final project repository:
Resources and References
- github.com/astro-rename
- github.com/postcss-rename
- docs.astro.build/getting-started
- github.com/astro-rename-example
Footnotes
Use the shortest possible names, in order of appearance: the first class is renamed to .a, the second to .b, and so on. ↩
Comments