
Initially, Sass compilation is incredibly fast, especially when used with Browsersync for instant browser updates. However, as your Sass project grows, compilation time inevitably increases, impacting workflow. This article presents a solution, demonstrated in the WordPress CSS editor Microthemer, to address this performance bottleneck.
This is a two-part exploration. Part one focuses on the benefits for Sass users, showcasing core principles, performance improvements, and an interactive demo. Part two delves into the technical details of Microthemer's speed optimization and explores the potential for an npm package to benefit a wider developer community.
Microthemer's Instant Sass Compilation
Microthemer achieves remarkable performance by compiling only the necessary Sass code. It doesn't modify Sass's internal compilation, but rather intelligently selects the minimal code required for recompilation.
This is achieved by tracking Sass entities (variables, mixins, etc.) and their usage. When a selector changes, only that selector and directly related selectors (sharing variables or extending others) are recompiled. This maintains speed even with thousands of selectors.
Performance Benchmarks
With 3000 selectors, compilation time hovers around 0.05 seconds, occasionally reaching 0.1 seconds or as low as 0.01 seconds (10ms). A video demonstration and an online playground are available for hands-on testing.
Online Microthemer Playground
Experiment with Microthemer directly using the online playground:
- Access the Microthemer playground.
- Enable Sass support:
General → Preferences → CSS / SCSS → Enable SCSS. - Open the full code editor:
View → full code editor → on(for global variables, mixins, etc.). - Return to the main UI view:
View → full code editor → off. - Create selectors using the Target button.
- Add Sass code to the editor (left of the Font property group).
- View the compiled code:
View → Generated CSS → Previous SCSS compile. - Import large CSS (not Sass) for scalability testing:
Packs → Import → CSS stylesheet.
An npm Package?
Microthemer's selective compilation could be packaged as an npm module. Is this something you'd find valuable? Would a speed boost to your Sass workflow be beneficial? Let us know in the comments.
The remainder of this article targets developers of community tools and those curious about the solution's implementation.
Microthemer's Approach to Sass Compilation
Before diving into code, let's review the key design goals:
- Minimal Code Compilation: Compile only the changed selector or the minimal set of related selectors.
- Responsiveness: Eliminate perceived lag; minimize processing between user interactions.
- Equivalent Output: Generate identical CSS to a full compilation, but for a subset of the code.
Sass Code Examples
The following code illustrates scenarios handled by the selective compiler: global variables, mixin side effects, and extended selectors.
Variables, Functions, and Mixins
$primary-color: green;
$secondary-color: red;
$dark-color: black;
@function toRem($px, $rootSize: 16){
@return #{$px / $rootSize}rem;
}
@mixin rounded(){
border-radius: 999px;
$secondary-color: blue !global;
}
Selectors
.entry-title {
color: $dark-color;
}
.btn {
display: inline-block;
padding: 1em;
color: white;
text-decoration: none;
}
.btn-success {
@extend .btn;
background-color: $primary-color;
@include rounded;
}
.btn-error {
@extend .btn;
background-color: $secondary-color;
}
@media (min-width: 960px) {
.btn-success {
border:4px solid darken($primary-color, 10%);
&::before {
content: "\2713";
margin-right: .5em;
}
}
}
Microthemer's User Interface
Microthemer uses two main views:
- Full Code View: A standard Sass editor for global elements.
- Visual View: A selector-based interface where each selector is a separate UI element.
This granular selector approach allows for highly efficient analysis. (Note: The blue error button in earlier screenshots is due to a mixin side effect changing $secondary-color.)
Third-Party Libraries
Microthemer leverages:
- Gonzales PE: For Sass-to-AST conversion.
- Sass.js: For browser-based Sass-to-CSS compilation (using web workers).
Data Structures
The core data structures are crucial for efficient processing. Microthemer uses four main JavaScript objects:
-
projectCode: Stores all project code, segmented for individual selectors. -
projectEntities: Tracks variables, functions, mixins, extends, imports, and their locations. -
connectedEntities: Maps code dependencies on Sass entities. -
compileResources: Holds data for selective compilation.
The detailed explanation of these objects and their properties (e.g., projectCode, itemData, projectEntities, itemDeps, connectedEntities, compileResources) is extensive and would significantly increase the length of this already long response. However, the core concept is that these structures allow for efficient tracking of dependencies and the selection of only the necessary code for recompilation.
The gatherCompileResources function, using recursion, intelligently identifies and gathers all dependent code segments for compilation. The application process then compiles this minimal code set and updates the styles.
Considerations for an npm Package
Adapting this for an npm package requires addressing:
- Code Segmentation: Efficiently handling larger files and potential virtual segmentation techniques.
- Sass Imports: Tracking Sass usage across all project files, rather than including all imports indiscriminately.
Conclusion
This approach offers a novel method for selective Sass compilation. The near-instant compilation is crucial for Microthemer's live editing, but could also benefit other environments. The decision to create an npm package depends on community demand. Feedback and questions are welcome.
The above is the detailed content of A Proof of Concept for Making Sass Faster. For more information, please follow other related articles on the PHP Chinese website!
How much specificity do @rules have, like @keyframes and @media?Apr 18, 2025 am 11:34 AMI got this question the other day. My first thought is: weird question! Specificity is about selectors, and at-rules are not selectors, so... irrelevant?
Can you nest @media and @support queries?Apr 18, 2025 am 11:32 AMYes, you can, and it doesn't really matter in what order. A CSS preprocessor is not required. It works in regular CSS.
Quick Gulp Cache BustingApr 18, 2025 am 11:23 AMYou should for sure be setting far-out cache headers on your assets like CSS and JavaScript (and images and fonts and whatever else). That tells the browser
In Search of a Stack That Monitors the Quality and Complexity of CSSApr 18, 2025 am 11:22 AMMany developers write about how to maintain a CSS codebase, yet not a lot of them write about how they measure the quality of that codebase. Sure, we have
Datalist is for suggesting values without enforcing valuesApr 18, 2025 am 11:08 AMHave you ever had a form that needed to accept a short, arbitrary bit of text? Like a name or whatever. That's exactly what is for. There are lots of
Front Conference in ZürichApr 18, 2025 am 11:03 AMI'm so excited to be heading to Zürich, Switzerland for Front Conference (Love that name and URL!). I've never been to Switzerland before, so I'm excited
Building a Full-Stack Serverless Application with Cloudflare WorkersApr 18, 2025 am 10:58 AMOne of my favorite developments in software development has been the advent of serverless. As a developer who has a tendency to get bogged down in the details
Creating Dynamic Routes in a Nuxt ApplicationApr 18, 2025 am 10:53 AMIn this post, we’ll be using an ecommerce store demo I built and deployed to Netlify to show how we can make dynamic routes for incoming data. It’s a fairly


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools







