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

@himanoa/class-name-extractor

v0.0.4

Published

ClassNameExtractor solves these problems by providing FFI setup that can extract class names by parsing CSS for safe style resolution, and by providing functionality to place CSS in the `output` directory.

Downloads

223

Readme

ClassNameExtractor

License: MIT npm version Test

When using Vite's CSS Modules feature with PureScript, there are two main issues:

  1. Since PureScript cannot directly import CSS, you need to prepare JavaScript code using FFI to import CSS and retrieve styles based on class name information, making it accessible from PureScript.
  2. While spago build can output the JavaScript file that imports CSS and the compiled PureScript results to output/, the CSS files imported by JavaScript are not placed in output. As a result, Vite cannot resolve the CSS import part, causing build errors.

ClassNameExtractor solves these problems by providing FFI setup that can extract class names by parsing CSS for safe style resolution, and by providing functionality to place CSS in the output directory.

Installation

If you want to use the internal implementation as a library, please install it from spago:

spago install class-name-extractor

If you want to use this tool as a CLI tool, install it from the NPM registry with the following command:

npm install -D @himanoa/class-name-extractor

Usage

Basic Usage

First, prepare your CSS module file. The file should use the .module.css extension:

/* src/components/Button/styles.module.css */
.foo {
  display: flex
}
.bar {
  background-color: black;
}

Then, run the class-name-extractor command with the following arguments:

class-name-extractor <css-file-path> <purescript-module-name>
  • <css-file-path>: Path to your CSS module file (e.g., src/components/Button/styles.module.css)
  • <purescript-module-name>: The PureScript module name where you want to use these styles (e.g., YourProject.Components.Button)

Example:

class-name-extractor src/components/Button/styles.module.css YourProject.Components.Button.Styles

Generated Files

The command will generate three files:

  1. JavaScript FFI file:
/* src/components/Button/Styles.js */

import s from './styles.module.css'
export const _styles = (name) => {
  return s[name]
}

This file imports the CSS module and provides a function to access the class names.

  1. PureScript module file:
-- src/components/Button/Styles.purs
module YourProject.Components.Button.Styles (foo, bar) where

foreign import _styles :: String -> String

foo :: String
foo = _styles "foo"
bar :: String
bar = _styles "bar"

This file exports the class names as PureScript functions that can be imported in your components.

  1. CSS file in output directory:
/* output/YourProject.Components.Button.Styles/styles.module.css */

.foo {
  display: flex
}
.bar {
  background-color: black;
}

This is a copy of your CSS file placed in the output directory for Vite to properly resolve the imports.

Using in Your Components

You can use the generated styles in your PureScript components like this:

module YourProject.Components.Button.Component where

import YourProject.Components.Button.Styles as Styles

button :: JSX
button = element "div" { className: Styles.foo } [ text "Button" ]

This ensures that:

  • The class names are available as PureScript functions
  • The CSS is properly imported and bundled by Vite
  • You don't need to manually manage the FFI setup for CSS modules

Feedback and Contributions

If you find any bugs or issues while using the tool, please create an Issue.

LICENSE

MIT