Giter Club home page Giter Club logo

cssloader's Introduction

CssLoader (css!) - RequireJs plugin

RequireJs plugin for reliably loading and waiting for css files.

##Inspiration There is a need for a well tested and reliable plugin for loading css resources using RequireJs.

The main issue with loading and waiting for css files, is actually not “loading” (I found only IE 31 stylesheet limit problematic,described below) but “waiting” for when the stylesheet is downloaded and available in the DOM. This is further described here and here.

Approach

After looking at existing plugins and approaches I came to conclusion that in order to address “waiting” issue the following things needed to be addressed first:

  1. Reliably understand if browser support “load” event on “link” element.
  2. Reliably understand if style sheet is applied on DOM when native “load” event doesn't provided by browser.

The plugin logic also contains the workaround for IE 31 stylesheet limit.

Because there are a lot of caveats in different browsers each approach used in plugin is tested thoroughly. There are Qunit unit tests available which can be found here.

Getting Started

Download the [production version][min] or the [development version][max]. [min]: https://github.com/dimaxweb/CSSLoader/tree/master/dist/css.js [max]: https://github.com/dimaxweb/CSSLoader/tree/master/src/css.js

Usage (example)

You can specify a .css file resource as a requirejs dependency by using below css! syntax. Note that it works completely similar to require.js's 'standard' text! loader plugin. Here is an example:

require.config({
    // baseUrl: 'Scripts',  <-- baseUrl had to be commented out because the .css file is located at 'Content/bootstrap.css' (and not 'Scripts/Content/bootstrap.css').
    paths: {
        "jquery": "Scripts/jquery-1.11.1.min",
        "bootstrap": "Scripts/bootstrap.min",
        "bootbox": "Scripts/bootbox.min",
		...
        "css": "Scripts/css.min"   // <- Step 1: Add the 'css.min.js' (or css.js) to your requirejs config.
    },
    shim: {
        "bootstrap": { "deps": ['jquery'] }
    }
});

require(['jquery', 'bootstrap', 'bootbox', 'css!Content/bootstrap.css'],  // <- Step 2: add the .css URL as a dependency with the 'css!' loader plugin before it.
	function($,bootstrap,bootbox) {
		bootbox.confirm(..  // The confirm is shown with the correct layout!
		...
});

##Compatability Tested on : IE7,IE8,IE9, Firebox: 10.0.2, Chrome:21.0, Opera:12.02, Safari:5.17

##Credits Understanding if an event is supported on an element.

  • Worth to add that I first came to that approach when walking through the jQuery code.

Get identification when css is applied to DOM.

##TODO: Check that stylesheet not only applied to DOM but also that rules are applied on stylesheets loaded from another domain.

Handle the css files with @import directives.

Support for Requirejs optimizer.

License

Licensed under the MIT, GPL licenses.

Contributing

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

Important notes

Please don't edit files in the dist subdirectory as they are generated via grunt. You'll find source code in the src subdirectory!

While grunt can run the included unit tests via PhantomJS, this shouldn't be considered a substitute for the real thing. Please be sure to test the test/*.html unit test file(s) in actual browsers.

Installing grunt

This assumes you have node.js and npm installed already.

  1. Test that grunt is installed globally by running grunt --version at the command-line.
  2. If grunt isn't installed globally, run npm install -g grunt to install the latest version. You may need to run sudo npm install -g grunt.
  3. From the root directory of this project, run npm install to install the project's dependencies.

Installing PhantomJS

In order for the qunit task to work properly, PhantomJS must be installed and in the system PATH (if you can run "phantomjs" at the command line, this task should work).

Unfortunately, PhantomJS cannot be installed automatically via npm or grunt, so you need to install it yourself. There are a number of ways to install PhantomJS.

Note that the phantomjs executable needs to be in the system PATH for grunt to see it.

cssloader's People

Contributors

dimaxweb avatar

Watchers

James Cloos 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.