minifier-css package provides CSS parsing, minification, and transformation capabilities for Meteor applications using PostCSS and cssnano. It handles URL rewriting, source maps, and CSS merging.
Overview
This package exportsCssTools, a comprehensive toolkit for CSS processing. It uses modern PostCSS infrastructure for parsing and cssnano for minification, making it compatible with current CSS standards.
Dependencies
Installation
Included by default in Meteor’s build system. For manual use:API
CssTools.parseCss(cssText, options)
Parse CSS string into a PostCSS AST (Abstract Syntax Tree). Parameters:cssText(string): CSS content to parseoptions(Object): PostCSS parser optionsfrom(string): File path for source maps (formerlysource)
CssTools.stringifyCss(cssAst, options)
Convert PostCSS AST back to CSS string with optional source maps. Parameters:cssAst(Object): PostCSS Root ASToptions(Object): PostCSS stringify optionsmap(Object): Source map configuration (formerlysourcemap)from(string): Source file path
code(string): Generated CSS stringmap(Object|null): Source map if enabled
CssTools.minifyCss(cssText)
Minify CSS using cssnano in safe mode. Parameters:cssText(string): CSS to minify
CssTools.minifyCssAsync(cssText)
Alias forminifyCss(). Both methods are asynchronous.
Example:
CssTools.mergeCssAsts(cssAsts, warnCb)
Merge multiple CSS ASTs into a single AST, handling imports and charset rules. Parameters:cssAsts(Array): Array of PostCSS Root objectswarnCb(Function): Callback for warnings(filename, message) => {}
@importrules are moved to the beginning@charsetrules are removed (UTF-8 assumed)- Warns about mid-file imports
- URL paths are rewritten to absolute
CssTools.rewriteCssUrls(ast)
Rewrite relative URLs in CSS to absolute paths. Parameters:ast(Object): PostCSS Root AST (modified in place)
Configuration
Minification Options
The package uses cssnano in safe mode:- Unsafe selector merging
- Aggressive shorthand optimization
- Removal of potentially needed rules
Source Maps
Enable source maps during stringification:URL Rewriting
How It Works
The package rewrites relative URLs to work correctly when CSS files are merged:URL Patterns
Not rewritten:url(http://example.com/img.png)- HTTP/HTTPSurl(//cdn.example.com/img.png)- Network pathurl(data:image/png;base64,...)- Data URLsurl(#fragment)- Fragments
url(../images/bg.png)- Relative pathsurl(./icon.png)- Current directoryurl(bg.png)- Relative to CSS file
Package Path Handling
Special handling for Meteor packages:- Files in
/packages/*keep their path prefix - Other files are served from root
/
Deployment Prefix Support
URLs are made relative to merged CSS for ROOT_URL support:Charset Handling
UTF-8 Only
The package assumes all CSS is UTF-8:@charset "UTF-8";@charset 'UTF-8';
Import Handling
Import Extraction
Imports are extracted and moved to file beginning:Mid-File Import Warning
Warns about imports after other rules:Performance Optimization
Functions are profiled when available:Backwards Compatibility
Legacy css-parse/css-stringify
The package maintains compatibility with the old API:Complete Example
Testing
Comprehensive test suite:Common Issues
Issue: URLs broken after merge
Solution: Ensureast.filename is set with the source file path before merging:
Issue: Charset warnings
Solution: Remove@charset rules or ensure they specify UTF-8:
Issue: Mid-file imports
Solution: Move all@import statements to the beginning: