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

mobile-first-media-queries-splitter-webpack-plugin

v1.0.2

Published

A Webpack plugin that splits your CSS main file into several CSS files based on media queries sizes, and automatically inject them into your index.html by the current media queries Mobile-First loading order approach.

Downloads

11

Readme

⚠ Please note, the plugin checks for Media Queries in px unit only.

What the plugin solves?

Let's assume your final CSS file looks like this:

main.css

body {
  background-color: green;
}

@media (min-width:768px) and (max-width:991px) {
    body {
        background-color:red
    }
}

@media (min-width:992px) and (max-width:1199px) {
    body {
        background-color:blue;
    }
}

Now, after you will run a build command, you will get an index.html file that looks like this:

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Index.html file example</title>
    <link href="/assets/css/main.css" rel="stylesheet" />
  </head>
  <body>
    <div id="root"></div>
    <script src="/assets/js/runtime.bundle.js"></script>
    <script src="/assets/js/main.bundle.js"></script>
  </body>
</html>

And what's the problem here? The problem is that you are loading a single CSS file (main.css), which contains settings that will not always need to be loaded. e.g If the user is browsing from a mobile device whose width is 480px, this user should not experience a browsing slowdown only because the CSS file is too inflated. Instead, what is right to do is to divide the CSS file into separate files, which will load asynchronously and contain only the settings that the user really needs.

main.css

body {
  background-color: green;
}

main.768.css

@media (min-width:768px) and (max-width:991px) {
    body {
        background-color:red
    }
}

main.992.css

@media (min-width:992px) and (max-width:1199px) {
    body {
        background-color:blue;
    }
}

And your new index.html file should look like this (and yes, the plugin takes care of that too!):

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Index.html file example</title>
    <link href="/assets/css/main.css" rel="stylesheet" />
    <link href="/assets/css/main.768.css" rel="stylesheet" media="(min-width: 768px)" />
    <link href="/assets/css/main.992.css" rel="stylesheet" media="(min-width: 992px)" />
  </head>
  <body>
    <div id="root"></div>
    <script src="/assets/js/runtime.bundle.js"></script>
    <script src="/assets/js/main.bundle.js"></script>
  </body>
</html>

Let's get started

To begin, you'll need to install mobile-first-media-queries-splitter-webpack-plugin, html-webpack-plugin, and mini-css-extract-plugin.

npm install --save-dev mobile-first-media-queries-splitter-webpack-plugin html-webpack-plugin mini-css-extract-plugin

⚠ Please note, the plugin will not work properly if you do not use one of the plugins mentioned above, it is only meant to work with them.

Now, add the plugin to your webpack production config file. For example:

webpack.production.js

const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const MobileFirstMediaQueriesSplitterPlugin = require('mobile-first-media-queries-splitter-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  mode: 'production',
  module: {
    rules: [
        {
            test: /\.css$/i,
            use: [MiniCssExtractPlugin.loader, "css-loader"],
        }
    ],
  },
  plugins: [
      new MiniCssExtractPlugin(),
      new MobileFirstMediaQueriesSplitterPlugin([768, 992]),
      new HtmlWebpackPlugin()
  ]
};

Parameters

Plugin Parameters

| Name | Type | Default | Description | | :---------------------------------------------------------------: | :------------------: | :-----------------------------------: | :---------------------------------------------------------------------------- | | mediaQueries | Array[]<INT> | [] | Determines media queries sizes. |

mediaQueries

Type: Array[]<INT> Default: []

Determines media queries sizes.

E.G:

const mediaQueries = [768, 992];
const plugins = [ new MobileFirstMediaQueriesSplitterPlugin(mediaQueries) ];
...

Dependence

Please note, this plugin meant to be simple as possible, there is no fancy options in it, does not know how to handle grandiose Media Query rules, or different units from PX, just splits CSS Media Queries by the mobile first approach, KEEP IT SIMPLE!

More Media Query Plugins

Credits

Parts of the plugin were written using code snippets written by others.

License

MIT