Giter Club home page Giter Club logo

grunt-stripmq's Introduction

grunt-stripmq

Mobile-first CSS Fallback

grunt-stripmq is a Grunt task to generate a fallback version of your fancy mobile first stylesheet. Since IE8 and lower dont support media queries, you can use a javascript library like respond.js to enable this, or generate a fallback version on build-time with this task.

Here's the workflow:

  1. Write Mobile-first CSS
  2. Generate Desktop Fallback for IE < 9 with grunt-stripmq
  3. Old IE Users see the desktop version of your mobile-first website
  4. Profit!

Getting Started

This plugin requires Grunt ~0.4.0, and is available on npmjs.org

npm install grunt-stripmq --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-stripmq');

The "stripmq" task

Overview

In your project's Gruntfile, add a section named stripmq to the data object passed into grunt.initConfig().

grunt.initConfig({

    stripmq: {
        //Viewport options
        options: {
            width: 1000,
            type: 'screen'
        },
        all: {
            files: {
                //follows the pattern 'destination': ['source']
                'css/app-old-ie.css': ['css/app.css']
            }
        }
    }
});

Demo

app.css:

Some mobile-first CSS that manipulates the background based on the viewport's width.

body {
    background: url('mobile-background.png');
    margin: 100px;
}

/* Change the background for tablets */
@media screen and (min-width: 640px) {
    body {
        background: url('tablet-background.png');
        margin: 120px;
    }
}

/* Change the background again for desktop and increase the font-size */
@media (min-width: 900px) {
    body {
        background: url('desktop-background.png');
        font-size: 120%;
    }
}

/* If it's a monochrome screen, show a black background */
@media (monochrome) {
    body {
        background: black;
    }
}

app-old-ie.css (generated by grunt-stripmq)

Note how the media queries were removed. The monochrome media query did not match the default options that were passed in, and were therefore discarded.

body {
    margin: 120px;
    background: url(desktop-background.png);
    font-size: 120%;
}

index.html

In your index.html, add in conditional comments to serve app-old-ie.css to old IE browsers, and your mobile-first styles to modern browsers that support media queries.

<!--[if lt IE 9]><link rel="stylesheet" href="app-old-ie.css"><![endif]-->
<!--[if gt IE 8]><!--><link rel="stylesheet" href="app.css"><!--<![endif]-->

How it works

Here's what the stripmq task does under the hood:

  • It parses your source.css files and copies over all default CSS rules to the destination.css file
  • When it encounters a media query, it parses the media query using css-mediaquery
  • It compares the parsed media query to the JavaScript object that you pass into options (more on this below)
    • If the comparison passes, it unwraps all the rules from the media query and adds them to destination.css file in the same place
    • If the comparison fails, it ignores all the rules within the media query
  • Cleans the CSS with clean-css, by merging selectors and properties.
  • It outputs the destination.css file.

Options

The options object is used to specify a "viewport" that you want your old IE users to see. The media queries in your mobile-first stylesheet will be compared against the properties of the options object.

The options follows the W3C Recommendations for CSS3 Media Queries and CSS3 Values and Units. It supports all of the Media Features and will properly convert values to a common unit before comparing them.

options.type

Type: String Default value: "screen"

options.width

Type: Integer Default value: 1024

If you pass in a number, the unit is assumed to be px. The following units are also supported: em, rem, cm, mm, in, pt, and pc.

options['device-width']

Type: Integer Default value: Defaults to options.width if provided, 1024 otherwise

options.height

Type: Integer Default value: 768

options['device-height']

Type: Integer Default value: Defaults to options.height if provided, 768 otherwise

options.resolution

Type: String Default value: "1dppx"

This property also supports other resolution units.

options.orientation

Type: String Default value: "landscape"

options['aspect-ratio']

Type: String Default value: Defaults to options.width/options.height if both are provided, 1024/768 otherwise.

options['color']

Type: Integer Default value: 3

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

grunt-stripmq's People

Contributors

jtangelder avatar oller avatar rvock avatar spacedawwwg avatar tilomitra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grunt-stripmq's Issues

Missing comma in Grunt Task example code

Not sure if this is maintained anymore but there's a missing comma after the closing brace for options in the grunt task example. It should be:

    stripmq: {
        options: {
            width: 640,     // viewport width, default is 1024
            height: 480,    // viewport height, default is 768
            'device-pixel-ratio': 2          // default is 1
        },
        all: {
            files: {
                'desktop.css': ['mobile-first.css']
            }
        }
    }

Compressed/expanded output

As far as I can see, the output of stripmq is always compressed. Shouldn't there be an option to have it expanded, e.g. for checking the generated output in development?

I, for example, usually use Sass's nested style for development and compressed for deployment. (Yes, I am aware that most editors can re-format CSS to that, but that's manually. I am lazy. ;-))

imports getting stripped out

I noticed that stripmq is not including the _@_import statements in the generated files. Is there any way to include _@_imports?

Media queries with `only` should be ignored for IE8

From the Media Query Spec:

The keyword ‘only’ can also be used to hide style sheets from older user agents. User agents must process media queries starting with ‘only’ as if the ‘only’ keyword was not present.

So basically, if a media query contains only, it should be ignored in old IE. I added a related issue in css-mediaquery to reveal only in the AST. Once that happens, I can send a PR that checks for only, and clears up this issue.

Concat of files over a folder does not work

A setup like this works fine with the "contrib-cssmin" task:

{
    "portal":
    {
        "files":
        {
            "../../../httpdocs/module/portal/css/ie_old.css":
                [
                    "../../../httpdocs/module/portal/css/*.css"
                ]
        }
    }
}

But your task fails to concat multiple files into one, it just takes the first one and finally aborts the process.

Note: the css file containts mixed CSS, like:

.box {
    border: 1px solid;
}

@media screen and (min-width: 59.999em) {
    .box{
        display: inline-block;
    }
}

Best regards

Generate a diff File

StripMQ is more or less a blackbox. Some other grunt Plugins like autoprefixer are able to generate diff files or a log to see and control what has been done during the process. would be very nice if stripmq can do something similar

"opacity" IE 8 issue

Hi,

I have installed this plugin in my project, It does not replace "opacity" css.

Thank you

LICENSE?

you need to include the license text for MIT somewhere

Include multiple types

I usually include print styles at the end of my CSS files using @media print {}. If I use

options: {
    width: 1024,
    type: 'screen'
},

the generated output file does not include said print styles. That's logical, because they're obviously not screen styles. But it's not very convenient.

Is there a way to include multiple types? If so, I did not get how to do that from the README. (Which might be possible, I'm not a Grunt expert.) If not, maybe there should be. :)

issue: do not merge CSS rules!

hello some logic in the script merges css rules!
for example if i have that:
.test1 {
margin: 0;
}
.test2 {
margin: 0;
}

it is this after stripmq is done:
.test1,
.test2 {
margin: 0;
}

and i dont want that merging it breaks my website :(
is there an option to stop stripmq doing this? or do you have a fix for it?

edit:
i have seen thats an clean-css option.

Multiple values in width option

Hi,

is it possible to set multiple values in the width options? I need this, because the desktop styles are in different media query sizes.

Cheers Philip

Make it clearer what width and height options do

I had no idea what these did until I started playing with the task. If set to 1024 (the default) it will ignore any media queries wider than that, is that correct?

If so I would recommend updating the README.md to make this clearer. Happy to submit a PR if necessary.

Cheers

can not get stripmq to work for multiple src dest folder

Hi,
it seems there is an issue to get stripmq working for multiple folders.
This works:

        files: {
            expand: true,
            cwd: '.tmp/resources/css/',
            src: '{,*/}*.css',
            dest: '.tmp/resources/css/',
            ext: '.mqless.css'
        }

This one not:

        files: {
            expand: true,
            cwd: '.tmp/',
            src: ['resources/css/{,*/}*.css' , 'resources/css2/{,*/}*.css'],
            dest: '.tmp/resources/css/',
            ext: '.mqless.css'
        }

is there a solution for multiple folders setup?

Getting "path must be a string" error

Trying to run:

stripmq: {
        all: {
            files: {
                'css/all/404.css': ['css/all/test/404.css']
            }
        }
    }

However I keep on getting

Warning: path must be a string Use --force to continue.

I think it's a path issue, but I am not able to resolve it. Any help would be greatly appreciated.

Support IE9/10 media hack?

Trying to use an IE9/10 specific @media screen and (min-width:0\0) hack (see "Moving IE specific hacks into media blocks") but StripMQ (what we use for IE8 support) is exiting with an error:

{ [Error: Stripping media queries failed.] origError: [TypeError: Cannot read property '1' of null] }

Would it be possible to support this weird hack? As a result, we'll have to degrade IE9/10 so that StripMQ can still work, but ideally StripMQ would maybe just ignore media queries it doesn't understand?

I think I lifted this hack from http://wtfforms.com: https://github.com/mdo/wtf-forms/blob/master/wtf-forms.css#L186-L212

Thanks!

Advanced optimisations should be disabled for CleanCss component

Some the optimisations in CleanCss can break stylesheets for IE8, for example the merging of background-size properties into the main background declaration. This sucks, as generating IE8 stylesheets is what I assume this component is mainly used for!

CleanCss also has a "compatibility" mode, which would disable optimisations that would break old IEs, so it might be helpful to use that by default, rather than having to disable CleanCss completely, as it is otherwise quite useful.

StripMQ to separate package

Maybe move the tasks/lib/stripmq file to a separate npm package so it can be used for grunt/gulp/broccoli etc.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.