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

elm-css-modules-loader

v3.0.4

Published

webpack loader for using CSS modules in Elm code

Downloads

11

Readme

CSS Modules for Elm

A Webpack loader that enables you to reference CSS modules in Elm source files.

Hat tip to NoRedInk and its elm-assets-loader, which formed the technical basis for this package.

Overview

Start with a CSS file that can be imported by Webpack using css-loader:

.something {
  ⋮
}

.anotherThing {
  ⋮
}

In any Elm module, reference this stylesheet and the classes you want to use in it:

module Main exposing (..)

import CssModules exposing (css)


styles =
    css "./stylesheet.css" -- relative to main Elm source directory
        { something = "something" -- string value should match CSS class name 
        , anotherThing = "anotherThing"
        }

Then use the returned functions to use the class names in your view:

view : Html Msg
view =
    div
        [ styles.class .something ]
        [ text "this is a div"]

Note: the .something syntax may be confusing at first. This is just standard Elm syntax for a function that reaches into a record and returns the value of the something key. Because the Elm compiler will only let you reference class names that exist in your CSS Module declaration, you get a bit of type safety to guard against typing mistakes.

Why does this exist?

We wanted to use the same style sheets for the standard components in our application (buttons, form fields, etc.) across two different implementations of these components (React and Elm). We love the namespacing and composition features of CSS Modules; this project seeks to make them usable within Elm views.

Note: elm-css is the de facto standard for writing styles for HTML interfaces written in Elm. If you are working on an all-Elm application, you should probably use that.

How to use

To get this working, you need to set up a combination of a Webpack loader and and Elm package.

Webpack Loader

Add the elm-css-modules-loader NPM package to your project, then configure Webpack to chain it with elm-webpack-loader:

Webpack 2+

module.exports = {
  ⋮
  module: {
    rules: [
      {
        test: /\.elm$/,
        use: [
          {
            loader: 'elm-css-modules-loader',
          },
          {
            loader: 'elm-webpack',
          }
        ],
      },
      ⋮
    ],
  },
};

Webpack 1.x

module.exports = {
  ⋮
  module: {
    loaders: [
      {
        test: /\.elm$/,
        loaders: [
          'elm-css-modules-loader',
          'elm-webpack',
        ],
      },
      ⋮
    ],
  },
};

Note the following configuration options are available for the loader. If you’re using the original version of this package, the defaults should work fine.

package – (default: cultureamp/elm-css-modules-loader) The Elm package in which the CssModule type is defined. If you forked the Elm package for your own development, you’ll need to specify the full package name that you have released it under with this option.

module – (default: CssModules) The name of the Elm module in which the tagger function is defined.

tagger – (default: css) The name of the Elm factory function that is used to declare CssModules in your code.

Note: Don't set noParse on .elm files. Otherwise, the JavaScript requires that this loader adds to your compiled Elm modules won't be processed by Webpack.

Elm Package

Install the cultureamp/elm-css-modules-loader package in your Elm project, then use the CssModule constructor for referencing CSS modules.

Under the hood

Let’s walk through what happens when this Elm code is processed by Webpack:

styles =
    css "./stylesheet.css"
        { something = "something"
        , anotherThing = "anotherThing"
        }

This will be compiled to JavaScript by elm-webpack-loader:

var user$project$Styles$styles = A2(
  cultureamp$elm_css_modules_loader$CssModules$css,
  './stylesheet.css',
  {
    aC: 'something',
    aD: 'anotherThing'
  }
);

elm-css-modules-loader replaces the hard-coded JSON object with a require of your stylesheet:

var user$project$Styles$styles = A2(
  cultureamp$elm_css_modules_loader$CssModules$css,
  './stylesheet.css',
  {
    aC: require('./stylesheet.css')["something"],
    aD: require('./stylesheet.css')["anotherThing"]
  }
);

webpack parses this require call, processes the stylesheet with css-loader, and replaces the require with a reference to the CSS module:

var user$project$Styles$styles = A2(
  cultureamp$elm_css_modules_loader$CssModules$css,
  './stylesheet.css',
  {
    aC: __webpack_require__(42)["something"],
    aD: __webpack_require__(42)["anotherThing"]
  }
);

The CSS module loaded by __webpack_require__(42) contains the actual class names that your Elm app will now consume:

42:
function(module, exports) {
  module.exports = {
    something: 'something-abc123',
    anotherThing: 'anotherThing-abc123'
  };
}

Changelog

Our release history is tracked on the Github Releases page.

Note that the NPM package and the Elm package will have different version numbers, as changes to the Elm API may happen indepently of changes to the NPM API, and Elm does not allow you to bump the version number without changes to the Elm API. When viewing the releases page, NPM releases are tagged with a "v" - v2.1.0. While Elm releases are tagged with no leading "v" - 2.0.3.

For release history prior to 2.0.2, you can view our old CHANGELOG .