Git Product home page Git Product logo

jspm-loader-css's Introduction

JSPM Loader: CSS

Join the chat at https://gitter.im/geelen/jspm-loader-css

Description

This is a CSS loader for jspm. It uses PostCSS plugins to parse your CSS files. By default, it only applies the CSS Modules plugin suite to your CSS, but it can be extended to suit your needs. This loader has been built for jspm >0.17. You may experience difficulties, or it may not work at all, if you are using jspm v0.16.

That's pretty cool, but what the heck is CSS Modules?

Here's some reading material:

Essentially, CSS Modules automatically namespaces all of your classes to guarantee uniqueness. It returns to you an object of key/value pairs (in JavaScript) which you use to apply the generated class names to your elements.

Installation

To installl, run the following command:

jspm install github:MeoMix/jspm-loader-css@master"

and in jspm.config.js you'll need to add the following:

SystemJS.config({
  ...
  meta: {
    "*.css": {
      "loader": "jspm-loader-css",
      "cssOptions": {}
    }
  },
  ...
});

Load the styles by referencing them in your JS:

import styles from './foo.css'
elem.innerHTML = `<div class="${styles.fooComponent}"></div>`

Loading additional PostCSS plugins

It's likely that you will want to load additional, non-default PostCSS plugins for additional processing of your CSS. That is a supported scenario with this plugin, but requires minor adjustments to jspm.config.js

You should re-write your meta configuration such that it looks similar to:

SystemJS.config({
  ...
  meta: {
    "*.css": {
      "loader": "css.js"
    }
  },
  ...
});

where css.js is a file you create yourself. You can place css.js in a directory if you'd like, but you'll need to provide the full path to it inside jspm.config.js.

You'll need to import jspm-loader-css from within css.js like so:

import Plugins from 'jspm-loader-css/src/plugins.js'
import Loader from 'jspm-loader-css/src/loader.js'

const plugins = [
  Plugins.values,
  Plugins.localByDefault,
  Plugins.extractImports,
  Plugins.scope
];

const { fetch, bundle } = new Loader(plugins);
export { fetch, bundle };

The above code will perform identically to the default behavior of jspm-loader-css. You are then free to add additional plugins to the mixture, such as:

import Plugins from 'jspm-loader-css/src/plugins.js'
import Loader from 'jspm-loader-css/src/loader.js'
import autoprefixer from 'autoprefixer';

const plugins = [
  Plugins.values,
  Plugins.localByDefault,
  Plugins.extractImports,
  Plugins.scope,
  autoprefixer()
];

const { fetch, bundle } = new Loader(plugins);
export { fetch, bundle };

Keep in mind that jspm-loader-css runs both in the browser (during development) and in node (during production builds). Many PostCSS plugins are written with only node in mind. Your mileage may vary on being able to successfully use PostCSS plugins without modification.

For a working example of jspm-loader-css in css.js, see here: https://github.com/MeoMix/StreamusWebsite/blob/development/jspm/css.js

Passing cssOptions to jspm-loader-css

To specify options to jspm-loader-css, provide a cssOptions configuration:

SystemJS.config({
  meta: {
    "*.css": {
      "loader": "jspm-loader-css",
      "cssOptions": {
        "bundledStyleTagId": "id-of-style-element"
      }
    }
  },
});

The options supported include:

  • bundledStyleTagId: a string that will be the id of the <style> element that contains the CSS for a bundled project. If not provided, the name of the first entry point being bundled will be used. When css files are not being bundled, this configuration option does not apply.

Misc. Notes

I'm going to say this once and only once. THERE IS NO GUARANTEE ON LOAD ORDER OF YOUR CSS. Do not write CSS which only works because it comes after another CSS file. You will be very sad.

...but how do I load something like normalize.css?

You should load it via a separate <link> tag which points to its CDN.

...but I REALLY, REALLY, REALLY need one of my CSS files to come after another

Consider using PostCSS-Import to prepend one file above the other. Note that you should not do this for N files as you'll drastically increase the size of your CSS output. You'll need to provide custom load and resolve methods as options. See the example css.js file above for more information.

..do I have other options?

You can also use additional, explicit <link> tags and, as part of your build process, inline and minify as needed.

It sucks that I can't use composes from within pseudo-selectors and/or media queries

Agreed. Consider using:

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.