Skip to main content
The 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 exports CssTools, 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 parse
  • options (Object): PostCSS parser options
    • from (string): File path for source maps (formerly source)
Returns: PostCSS Root AST Example:

CssTools.stringifyCss(cssAst, options)

Convert PostCSS AST back to CSS string with optional source maps. Parameters:
  • cssAst (Object): PostCSS Root AST
  • options (Object): PostCSS stringify options
    • map (Object): Source map configuration (formerly sourcemap)
    • from (string): Source file path
Returns: Object with:
  • code (string): Generated CSS string
  • map (Object|null): Source map if enabled
Example:

CssTools.minifyCss(cssText)

Minify CSS using cssnano in safe mode. Parameters:
  • cssText (string): CSS to minify
Returns: Promise<string[]> - Array containing minified CSS Example:

CssTools.minifyCssAsync(cssText)

Alias for minifyCss(). 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 objects
  • warnCb (Function): Callback for warnings (filename, message) => {}
Returns: PostCSS Root object Example:
Merge behavior:
  • @import rules are moved to the beginning
  • @charset rules 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)
Returns: Undefined (modifies AST in place) Example:

Configuration

Minification Options

The package uses cssnano in safe mode:
Safe mode prevents:
  • 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/HTTPS
  • url(//cdn.example.com/img.png) - Network path
  • url(data:image/png;base64,...) - Data URLs
  • url(#fragment) - Fragments
Rewritten:
  • url(../images/bg.png) - Relative paths
  • url(./icon.png) - Current directory
  • url(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:
Example:

Charset Handling

UTF-8 Only

The package assumes all CSS is UTF-8:
Valid charset rules:
  • @charset "UTF-8";
  • @charset 'UTF-8';
All other charsets: Removed with warning

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:
Run tests:

Common Issues

Issue: URLs broken after merge

Solution: Ensure ast.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: