Stylify CSS: Automated utility-first CSS packages splitting right into CSS layers in Astro.build
Utility-first CSS packages can be really tiny. However suppose, we could make them also smaller sized? Divide them for each and every page/layout for instance? Some web pages may not designs from an additional web page. Discover exactly how to divide CSS packages in Astro. construct instantly making use of Stylify CSS.
Video Clip Overview
Stylify CSS intro
Stylify is a collection that utilizes CSS-like selectors to produce enhanced utility-first CSS based upon what you compose.
- ✨ CSS-like selectors
- No structure to research study
- Much less time invested in docs
- Mangled & & Very tiny CSS
- No cleanup required
- Elements, Variables, Customized selectors
- It can produce numerous CSS packages
Additionally we have a web page regarding what issues Stylify CSS addresses and also why you must offer it a shot!
Inspiration
Each package can consist of just required CSS. This suggests practically absolutely no extra CSS and also in manufacturing, it can be mangled and also minified, so the CSS is also smaller sized (even more details listed below).
The code
Ok, initially the code, after that the description. Despite the fact that the code can look difficult, it is in fact rather basic:
import stylify from ' @stylify/ astro';
import { hooks } from ' @stylify/ bundler';
import fastGlob from ' fast-glob';
import course from ' course';
import fs from ' fs';
import { fileURLToPath } from ' link';
const pagesDir = ' src/pages';
const layoutsDir = ' src/layouts';
const stylesDir = ' src/styles';
const stylifyBundles = [];
const layoutCssLayerName = ' format';
const pageCssLayerName = ' web page';
const getFileCssLayerName = ( filePath) =>>
filePath consists of('/ web pages/') ? pageCssLayerName : layoutCssLayerName;
const getOutputFileName = ( documents) =>> {
const parsedFile = course parse( documents);
const fileName = parsedFile name toLowerCase();
const dirNameCleanupRegExp = brand-new RegExp('$ { pagesDir} |$ { layoutsDir} | W', ' g'); console log ( parsedFile dir);
const dir = parsedFile
dir change( dirNameCleanupRegExp, " ); console log( dir );
return ' ${ dir size? '$ { dir}-' : "}$ { fileName} css ';
} ;
const createBundle = ( documents ) = > {
const fileName = course parse( documents ) name;
const> fileCssLayerName = getFileCssLayerName( documents )
;
return {
outputFile: '$ { stylesDir }/$ { fileCssLayerName} / ${ getOutputFileName( documents)} ',
documents: [file],
cssLayer: fileCssLayerName ,
} ;
} ;
const createBundles = ( documents) = > {
for ( const documents of documents) {
stylifyBundles press( createBundle( documents)) ;
}
};
createBundles( fastGlob sync('$ { pagesDir}/ **/ *. astro ') );
createBundles( fastGlob sync (' $ { layoutsDir } / **/ *. astro'));
const stylifyIntegration = stylify( {
bundler: {
id: ' astro',
cssLayersOrder: {
order : [layoutCssLayerName, pageCssLayerName] sign up with(','),
exportLayer :
[layoutCssLayerName],
},
} ,
packages: stylifyBundles,
}); hooks addListener(' bundler: fileToProcessOpened' ,
( information) = > {
allow { material, filePath} = information ;
if ( filePath consists of ( '/ web pages/' ) || filePath consists of('/ formats/' )) {
const cssFileName = course . parse( filePath) name;
const cssFilePathImport = ' import'/ $ { stylesDir }/ $ { getFileCssLayerName( filePath.)}/ $ { getOutputFileName( filePath)} '
;';
if (! material consists of( cssFilePathImport) ) {
if
(/ import S + from (?:' |") S +(/ layouts/S +)(?: '|
");/ examination( material)) {. material = material change(
/ import S + from(?:' |") S +/ layouts/S + (?:' |");/,
' $ &
n $ { cssFilePathImport}'
);
} else if (/ ^ s *-- n/ examination( material ) ) {. material = material change (/ ^ (s *)-- n/, ' $ & $ { cssFilePathImport } n ') ;
} else {(* ). material = ' -- n $ {(* )cssFilePathImport(* )} n-- n $ { material(* )}';} fs(* ). writeFileSync(* )( filePath
, material)(* );}} const regExp(* )= brand-new RegExp (
' (* )import S + from(?:' |") S +( / parts / S +) (?:' |" );
'
, ' g' ) ;(*
) allow importedComponent;
const importedComponentFiles =;(*
) const
rootDir (* )=
course
dirname( fileURLToPath
((* )import meta []
link(* ))(* )) ; while(* )( ( importedComponent = regExp director( material )(* )) ) { importedComponentFiles press
( course (* ). sign up with(* )( rootDir , (* )' src ',(* )importedComponent) )(* );} information (* )contentOptions(* ). documents = importedComponentFiles;}(* )) ; hooks addListener (* )(' bundler: booted up', ( { bundler}(* ))(* )= > {[1] const(* )dirsToWatchForNewBundles
=;(* )for
( const dir of dirsToWatchForNewBundles) { fs(* ). watch
((* )dir,(* )( eventType, fileName ) = > { const fileFullPath = course (* ). sign up with ( dir ,(* )fileName
)(* ); if((* )eventType [layoutsDir, pagesDir]! = =
' relabel ' ||! fs (* ). existsSync( fileFullPath )) { return;} bundler package(* )();} ) ;(* )}
}(* )); export default(* ){ combinations : }; Exactly how it functions The code over is divided right into 5 actions: It discovers all web pages in src/pages
and also all formats in(* )src/layouts and also calls the createBundles to develop packages setup for us with the appropriate layer name and also outcome documents. The Stylify assimilation is booted up and also CSS layers order is set up so it will certainly produce the order just right into a data, that has the format CSS layer name. bundler: fileToProcessOpened. hook is included. This hook has 2 components. One component is done, when this documents is a design or web page and also the an additional for each opened up documents. When a design or a web page documents is opened up, it checks, whether it consists of a course to CSS documents and also otherwise, it includes it to the head of the documents. For all various other documents, it attempts to look for imports If any type of part import is discovered, it maps it as a reliance. In this manner it can map an entire parts reliance tree instantly so you simply maintain adding/removing them and also the CSS is produced properly When Bundler is initialized we can begin expecting freshly included documents within the format
and also web pages
directory site. Whenever a brand-new documents is included, we develop a brand-new Package. The Stylify Assimilation is contributed to the Astro config. When we run the dev command, Stylify will certainly do the following: Map layout/page documents Create CSS right into appropriate documents, cover the material right into the appropriate CSS layer and also includes web links to these documents right into Astro themes.[createBundle(fileFullPath)] If a brand-new documents in layout/pages is developed a brand-new package is instantly included As Well As exactly how does the outcome tries to find manufacturing?
Format (src/layouts/Layout. astro): @layer format
,
web page; @layer
format { b
{ font-size [stylifyIntegration]
: 16px
}
- Web page (src/pages/index. astro):
@layer
web page{
. a{
shade - :
darkblue
} - } Assimilation instance
- Look into the interactive
- instance on Heap Strike
- The instances over do not consist of every little thing Stylify can do:
Do not hesitate to
take a look at the docsfor more information.
.