npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

css-media-splitter

v0.2.4

Published

Utils that split css by media queries into dedicated files. Supports: plain html files, Vite and Nuxt.

Downloads

95

Readme

css-media-splitter

The tool extracts all @media at-rules into dedicated files and download them only when the user device matches the media query.

This technique is the most valuable for mobile-first applications. It reduces the size of the CSS downloaded and increases the coverage ratio, so makes page loading faster and prevents a "Reduce unused CSS code" issue in the Lighthouse report.

The package exposes:

  • css-media-splitter/plain - handler for plain HTML & CSS projects. (maintainer: @levchak0910)
  • css-media-splitter/vite-plugin - plugin for Vite based projects. (maintainer: @levchak0910)
  • css-media-splitter/nuxt-module - module for Nuxt based projects. (maintainer: @levchak0910)
  • css-media-splitter/api - helper functions for writing custom solutions. (maintainer: @levchak0910)

Your favorite framework is not supported yet? - please refer to CONTRIBUTION.md.

How it works

The tool is modifying the application by transforming HTML and CSS files. It was developed to be used on the final bundle after any build process was finished.

The algorithm:

  1. run through all CSS source files and extract all @media at-rules.
  2. remove all @media at-rules from CSS source files.
  3. write dedicated media files with all CSS rules grouped under one @media at-rule.
  4. generate manifest with relations between source files and media files.
  5. include the loader into all HTML files.

source file in scope of this tool equals to a compiled CSS or HTML file from your final bundle. Please, don't confuse with the file you actually work with.

Usage

Install the package

npm i -D css-media-splitter
yarn add -D css-media-splitter
pnpm add -D css-media-splitter

Plain app

import processCssMediaSplitter from "css-media-splitter/plain"

const result = await processCssMediaSplitter({ distDir: "dist" })

Use result which contain manifest, loader and report depending on the project setup.

Example could be found in playground.

Vite

// vite.config.ts
import { defineConfig } from "vite"
import VitePluginCssMediaSplitter from "css-media-splitter/vite-plugin"

export default defineConfig({
  plugins: [
    VitePluginCssMediaSplitter(),
  ],
})

Examples could be found for:

Nuxt

// nuxt.config.ts
export default defineNuxtConfig({
  modules: ["css-media-splitter/nuxt-module"],
})

Example could be found in playground

Options

distDir

Available in: plain

Path to dist folder. Should be an absolute fs path or the folder name relative to the root folder.

In vite-plugin and nuxt-module is set based on vite/nuxt config respectively.

mediaFileMinSize

Available in: plain, vite-plugin, nuxt-module

Use this option to define how large media file content should be, so it will be extract into a dedicated file.

This option is useful when media file content is small and it will take more space in the loader manifest than having it in the CSS source file.

By default equals to 100 (experimentally calculated, not recommended to set a smaller size).

In Vite project: vite.config.ts

{
  plugins: [ VitePluginCssMediaSplitter({ mediaFileMinSize: 250 }) ],
}

In Nuxt project: nuxt.config.ts

{
  modules: [ ["css-media-splitter/nuxt-module", { mediaFileMinSize: 250 }] ],
}

Internals

Loader

By default, the loader will be included before the first link/style/script with the src path ends with .css or .js extension.

In some scenarios it is not relevant, then you can put comment <!-- css-media-splitter:loader --> in place before any link/style/script is defined and the comment will be replaced automatically.

Example in vue-ssr playground - comment on line 7.

When the comment is not the case too, the loader can be inserted programmatically using functions from css-media-splitter/api.

Example in nuxt-module - nuxt.hook "close" on line 42.

Credits