Giter Site home page Giter Site logo

gss2css's Introduction

GSS to CSS precompiler Build Status

This project provides both a Node.js library and a Grunt plugin for precompiling constraint-driven GSS layouts to plain CSS.

gss2css utilizes PhantomJS for rendering the existing GSS layout in various screen sizes and producing the appropriate CSS rules and media queries for those.

Node.js module

It is possible to run GSS-to-CSS precompilation as a Node.js library in your custom tooling. Example:

// Load the NPM module
var precompiler = require('gss-to-css');

// Sizes configuration
var options = {
  ranges: {
    width: {
      from: 400,
      to: 1000,
      step: 100
    },
    height: 600
  }
};

// Prepare a headless browser for the URL you're interested in
precompiler.open('http://example.net', function (err, page, phantom) {

  // Create a version of the page with GSS converted to CSS media queries
  precompiler.gss2css(page, options, function (err, html) {
    // Serve or save the HTML string

    // Then close down the headless browser
    phantom.exit();
  });

});

See the grunt sizes and ranges documentation on the sizing options to provide to the gss2css function.

Grunt plugin

Getting Started

This plugin requires Grunt ~0.4.1

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install gss-to-css --save-dev

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

grunt.loadNpmTasks('gss-to-css');

The gss_to_css task

Overview

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

grunt.initConfig({
  gss_to_css: {
    options: {
      // Task-specific options go here
    },
    precompile: {
      // Target-specific file lists and/or options go here.
    }
  },
});

Options

options.baseUrl

Type: String Default value: http://localhost:8002/

Base URL to use for rendering the GSS-enabled pages. Must be a URL where both the HTML files and their assets, GSS included, are available.

When working with local files the easiest option is to run the gss_to_css task together with a local web server provided by grunt-contrib-connect.

options.sizes

Type: Array Default value:

[
  {
    width: 1024,
    height: 768
  }
]

A list of sizes to render the page in and generate media queries. Useful when the page is targeting a known set of display resolutions, as is often the case when building mobile web apps.

options.ranges

Type: Object Default value: none

Ranges for width and height to utilize for producing the media queries. Allows compiling GSS into a set of responsive media queries. Overrides options.sizes when set.

For example, to generate media queries for each screen size between 400x600 and 1400x600 in 20 pixel intervals, one could configure ranges with:

ranges: {
  width: {
    from: 400,
    to: 1400,
    step: 20
  },
  height: 600
}

Note that it is possible to configure ranges for both width and height, in which case all the size combinations will appear in the media queries.

Usage examples

In this example we'll build some local GSS-enabled HTML files into the equivalent CSS-powered ones. GSS and other dependencies are available in the local directory structure and the HTTP server is provided via grunt-contrib-connect. The files are stored in the _site folder:

grunt.initConfig({
  connect: {
    server: {
      options: {
        port: 8002
      }
    }
  },

  gss_to_css: {
    pages: {
      options: {
        baseUrl: 'http://localhost:8002/',
        sizes: [
          {
            width: 800,
            height: 600
          },
          {
            width: 1024,
            height: 768
          },
          {
            width: 1900,
            height: 1080
          }
        ]
      },
      files: [
        {
          expand: true,
          cwd: '',
          src: ['src/*.html']
          dest: '_site'
        }
      ]
    }
  }
});

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.

gss2css's People

Contributors

bergie avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.