A sample ClojureScript project showcasing a workflow with live-reloadable CSS Modules using Gulp & Figwheel.
The basic premise is as follows:
- A Gulp task watches and compiles your CSS Modules to JS.
- Note: CSS Module namespaces to be exported are defined in
src/clojurescript_css_modules_demo/css/settings.js
.
- Note: CSS Module namespaces to be exported are defined in
- This JS file is hooked into your CLJS app through a
foreign-lib
namedcssModules
. - Figwheel reloads your project whenever it detects changes to
cssModules
. - This module is exposed to your CLJS project, allowing you to easily access the class names of your CSS Modules.
- Note: a helper macro is provided for use with Reagent's hiccup-style syntax, but you can use this flow with any CLJS project.
Clone, npm install
and gulp watch
in one REPL.
lein figwheel
in another REPL.
Change your .css modules within src/css/
and notice how Gulp/Figwheel live reload your project.
Note: Remember to change css/settings.js
if you add additional CSS Module namespaces.
At first - import CSS modules to project with foreign-libs. After it you can use aget
to lookup and replace classes in Reagent templates with the correct css classes from your files.
For example: in filename.css
we get CSS rule with selector .logo
:
(defn my-component[]
[:div { :class-name (aget js/cssModules "filename" "logo") }])
There is a pretty syntax sugar inside demo:
(defn my-component[]
(get-css
[:div.CSS>filename>logo]))
Which is equivalent to code above. Or you can use macro defcomponent
which wraps your component function with get-css
.
(defcomponent my-component[]
[:div.CSS>filename>logo])
- Expand on this documentation!
- Structure CSS Modules, add advanced examples.
- Explore making this a standalone Clojure plugin (thereby removing the NodeJS/Gulp dependency).
Contributions welcome!