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

gulp-minify-selectors

v1.1.0

Published

A simple gulp plugin to minify selectors through your entire project

Downloads

10

Readme

gulp-minify-selectors


npm version License: MIT

A simple gulp plugin to minify selectors through your entire project.

Install

Install with npm.

npm install --save-dev gulp-minify-selectors

Or with yarn.

yarn add --dev gulp-minify-selectors

How it work

gulp-minify-selectors track down selectors in your entire project and replace them by a very short lexicographical string. This process drastically reduce your code size.

It will take this code

<div class="-s-Card">
  <div class="-s-Card__Image">
    <img src="assets/images/illustration.jpg">
  </div>
  <div class="-s-Card__Headline">
    <span>Hello world !</span>
  </div>
  <p class="-s-Card__Text">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  </p>
</div>
.-s-Card { display: flex; flex-direction: column; font-family: "Roboto", sans-serif; }
.-s-Card__Image { height: 200px; }
.-s-Card__Headline { font-size: 1.5rem; font-weight: bold; }
.-s-Card__Text { font-size: 1.2rem; }
let card = document.querySelector("-s-Card");

and turn it into this code

<div class="a">
  <div class="b">
    <img src="assets/images/illustration.jpg">
  </div>
  <div class="c">
    <span>Hello world !</span>
  </div>
  <p class="d">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  </p>
</div>
.a { display: flex; flex-direction: column; font-family: "Roboto", sans-serif; }
.b { height: 200px; }
.c { font-size: 1.5rem; font-weight: bold; }
.d { font-size: 1.2rem; }
let card = document.querySelector("a");

Examples

Basic example

var gulp = require('gulp');
var minifySelectors = require('gulp-minify-selectors');

return gulp.src(['src/style.css','src/index.js', 'src/index.html'])
  .pipe(minifySelectors())
  .pipe(gulp.dest('dist'))

You can use gulp-minify-selectors with every file extension since it uses basic regex to operate the substitutions. This example show .css, .js and .html but you can also do it with .jsx, .pug, .scss, .ts or wathever you want.

The only things you have to do is prefixing or suffixing or prefixing and suffixing your actual selectors so that the tool can identify them. By default it expect -s- prefix and no suffix.

You can obviously override default settings by passing an options object like the following example:

Use with options object

var gulp = require('gulp');
var minifySelectors = require('gulp-minify-selectors');

return gulp.src(['src/style.css','src/index.js', 'src/index.html'])
  .pipe(minifySelectors({
    prefix: 'prefix-',
    suffix: '-suffix'
  }))
  .pipe(gulp.dest('dist'))

To fetch your selectors gulp-minify-selectors use some patterns. This patterns can be prefix, suffix or both prefix and suffix. In most cases, a prefix is good enough, but if you face a problem because it is interfering with something in your code, then, it would be more appropriate to use both prefix and suffix.

If you don't provide at least one of them it will throw an error.

⚠️ Warning : If you don't explicitly override the default options, they will be applied. To be clear, if you pass this object: {suffix: '-end'}, the regex will use both -s- prefix and -end suffix. If you only want to use suffix without any prefix, you should pass {prefix: null, suffix: '-end'}.

Error handling

var gulp = require('gulp');
var minifySelectors = require('gulp-minify-selectors');

gulp.task('minify-selectors', function(done) {
return gulp.src(['src/style.css','src/index.js', 'src/index.html'])
  .pipe(
    minifySelectors({
      // This options object will throw an error
      prefix: null,
      suffix: null
    })
  ).on( 'error', (e) => {
    console.error( "[ERROR]: " + e.message)
    done()
  })
  .pipe(gulp.dest('dist'))
});

By default an error will interupt the building process. If you want to get rid of this inconvenience you can handle the error by catching the error event as shown in this example.

API

minifySelectors([options])

minifySelectors({
  prefix: 'prefix-',
  suffix: '-suffix',
  verbose: true
})

Options

Available options are:

  • prefix ( string | null ) : The prefix use to find your selector
  • suffix ( string | null ) : The suffix use to find your selector
  • verbose ( boolean ) : Enable verbose mode (show size reduction)

License

The MIT License (MIT)

Copyright (c) 2020, Julien OPPLIGER

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


0101011000110000001100000100010000110000001100000110010101100100