Skip to content

Commit

Permalink
feat: move to only supporting dart-sass
Browse files Browse the repository at this point in the history
  • Loading branch information
hackeryarn committed Oct 3, 2023
1 parent 3e2320b commit e5579a5
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 309 deletions.
33 changes: 11 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# lein-sass [![Build Status](https://travis-ci.org/101loops/lein-sass.svg)](https://travis-ci.org/101loops/lein-sass)

Leiningen plugin to compile SASS/SCSS files with [SassC](https://github.com/sass/sassc) or
[Sass](http://sass-lang.com).
Leiningen plugin to compile SASS/SCSS files with [Dart Sass](https://sass-lang.com/dart-sass/)

> If you are looking for compatibility with [SassC](https://github.com/sass/sassc) or
older versions of [Sass](http://sass-lang.com), look at the pre 1.0 versions.

[![Clojars Project](http://clojars.org/lein-sass/latest-version.svg)](http://clojars.org/lein-sass)

Expand All @@ -15,33 +17,20 @@ You can install the plugin by adding lein-sass to your `project.clj` file in the
:plugins [[lein-sass "0.4.0"]])
```

[SassC](https://github.com/sass/sassc) needs to be installed manually.
For example on OSX run `brew install sassc`.

To install SassC on Ubuntu (should similarly apply to other Unixes) I usually follow this recipe:
http://askubuntu.com/questions/566675/how-to-install-node-sass-gulp-sass-on-ubuntu-14-04-or-linux-mint-17/566681#566681

Alternatively you can use sass to compile your sass or scss
files. This needs to be installed as well, but the process might be straight forward. Please see http://sass-lang.com/install for install instructions.

On a Linux system you can just run `sudo gem install sass` (provided a decent Ruby runtime is installed).

You will also need to install `sass`. Please see https://sass-lang.com/install/ for full installation instructions.

## Configuration

Here is an example of `project.clj` with all the possible definitions.

You must provide a `:targets` configuration, which only supports the [Many-to-Many Mode](https://sass-lang.com/documentation/cli/dart-sass/#many-to-many-mode). All other options are optional.

```clj
(defproject example-project "1.2.3"

:sass {:src "resources/sass"
:output-directory "resources/public/css"

:sass {:targets ["resources/sass:resources/public/css"]
;; other options (provided are default values):
;; :delete-output-dir true
;; :source-maps true
;; :style :nested
;; :command :sassc (:sass, :sassc, or :dart-sass are recognized values)
;; :source-maps false
;; :style :compressed
}
```

Expand All @@ -57,7 +46,7 @@ $ lein sass once
To keep the compiler running and watch for changes:

```sh
$ lein sass auto
$ lein sass watch
```

To delete all the files generated by lein-sass
Expand Down
22 changes: 7 additions & 15 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
(defproject lein-sass "0.6.0"
(defproject lein-sass "1.0.0"
:description "SASS autobuilder plugin"
:url "https://github.com/hackeryarn/lein-sass"
:license {:name "Eclipse Public License - v 1.0"
:url "http://www.eclipse.org/legal/epl-v10.html"
:distribution :repo}

:dependencies [[org.clojure/clojure "1.6.0"]
[com.cemerick/pomegranate "0.2.0"]
[clojure-watch "LATEST"]
[me.raynes/fs "1.4.6"]]
:dependencies [[org.clojure/clojure "1.11.1"]]

:profiles {:dev {:dependencies [[speclj "2.5.0"]]
:plugins [[speclj "2.5.0"]]
:profiles {:dev {:dependencies [[speclj "3.4.3"]]
:plugins [[speclj "3.4.3"]]
:test-paths ["spec/"]}

:spec {:sass {:src "spec/files"
:output-directory "spec/out"}}

:spec-map {:sass {:src "spec/files"
:source-maps true
:output-directory "spec/out/map"}}}
:spec {:sass {:targets ["spec/files:spec/out"]}}
:spec-map {:sass {:targets ["spec/files:spec/out/map"]
:source-maps true}}}

:hooks [leiningen.sass]

:eval-in-leiningen true
:min-lein-version "2.0.0")
111 changes: 42 additions & 69 deletions spec/leiningen/integration_spec.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns leiningen.integration-spec
(:use [speclj.core]
[clojure.java.shell :only [sh]])
(:require [leiningen.utils :as futils]
(:require [leiningen.utils :as utils]
[clojure.java.io :as io]))

(describe "integration tests on tasks"
Expand All @@ -10,84 +10,57 @@
;; the tests) especially: we are relying on the project.clj file
;; (which can't be changed from here)

(before (with-out-str (futils/delete-directory-recursively! "spec/out")))
(before (with-out-str
(utils/delete-file! (io/file "spec/out"))))

(defn sass [profile sub-task] (sh "lein" "with-profile" profile "sass" sub-task))
(defn sass [profile sub-task] (sh "lein" "with-profile" profile "sass" sub-task))

(context "without source maps"

(context "without source maps"
(context "once"
(it "compiles the files in the correct directory"
(sass "spec" "once")

(context "once"
(it "compiles the files in the correct directory"
(sass "spec" "once")
(let [out-files (file-seq (io/file "spec/out"))]
(should= 3 (count out-files)))

(let [out-files (file-seq (io/file "spec/out"))]
(should= 3 (count out-files)))
(let [file-content (slurp "spec/out/foo.css")
expected-content ".wide{width:100%}.foo{display:block}\n"]
(should= expected-content file-content))

(let [file-content (slurp "spec/out/foo.css")
expected-content ".wide {\n width: 100%; }\n\n.foo {\n display: block; }\n"]
(should= expected-content file-content))
(let [file-content (slurp "spec/out/bar.css")
expected-content ".bar{display:none}\n"]
(should= expected-content file-content))))

(let [file-content (slurp "spec/out/bar.css")
expected-content ".bar {\n display: none; }\n"]
(should= expected-content file-content))))
(context "clean"
(it "removes all artifacts that were created by sass task"
(sass "spec" "once")
(should (.exists (io/file "spec/out")))

(context "clean"
(it "removes all artifacts that were created by sass task"
(sass "spec" "once")
(should (.exists (io/file "spec/out")))
(sass "spec" "clean")
(should-not (.exists (io/file "spec/out"))))))
(context "with source maps"
(context "once"
(it "compiles the files in the correct directory"
(sass "spec-map" "once")

(sass "spec" "clean")
(should-not (.exists (io/file "spec/out"))))
(let [out-files (file-seq (io/file "spec/out/map"))]
(should= 5 (count out-files)))

(it "only deletes the artifacts that were created by sass task"
(sass "spec" "once")
(should (.exists (io/file "spec/out")))
(let [file-content (slurp "spec/out/map/foo.css")
expected-content ".wide{width:100%}.foo{display:block}/*# sourceMappingURL=foo.css.map */\n"]
(should= expected-content file-content))
(should (.exists (io/file "spec/out/map/foo.css.map")))

(spit "spec/out/not-generated" "a non generated content")
(let [file-content (slurp "spec/out/map/bar.css")
expected-content ".bar{display:none}/*# sourceMappingURL=bar.css.map */\n"]
(should= expected-content file-content))
(should (.exists (io/file "spec/out/map/bar.css.map")))))

(sass "spec" "clean")
(should (.exists (io/file "spec/out/not-generated")))
(should-not (.exists (io/file "spec/out/bar.css")))
(should-not (.exists (io/file "spec/out/foo.css"))))))
(context "clean"
(it "removes all artifacts that were created by sass task"
(sass "spec-map" "once")
(should (.exists (io/file "spec/out/map")))


(context "with source maps"

(context "once"
(it "compiles the files in the correct directory"
(sass "spec-map" "once")

(let [out-files (file-seq (io/file "spec/out/map"))]
(should= 5 (count out-files)))

(let [file-content (slurp "spec/out/map/foo.css")
expected-content ".wide {\n width: 100%; }\n\n.foo {\n display: block; }\n\n/*# sourceMappingURL=foo.css.map */"]
(should= expected-content file-content))
(should (.exists (io/file "spec/out/map/foo.css.map")))

(let [file-content (slurp "spec/out/map/bar.css")
expected-content ".bar {\n display: none; }\n\n/*# sourceMappingURL=bar.css.map */"]
(should= expected-content file-content))
(should (.exists (io/file "spec/out/map/bar.css.map")))))

(context "clean"
(it "removes all artifacts that were created by sass task"
(sass "spec-map" "once")
(should (.exists (io/file "spec/out/map")))

(sass "spec-map" "clean")
(should-not (.exists (io/file "spec/out/map"))))

(it "only deletes the artifacts that were created by sass task"
(sass "spec-map" "once")
(should (.exists (io/file "spec/out/map")))

(spit "spec/out/map/not-generated" "a non generated content")

(sass "spec-map" "clean")
(should (.exists (io/file "spec/out/map/not-generated")))
(should-not (.exists (io/file "spec/out/map/bar.css")))
(should-not (.exists (io/file "spec/out/map/bar.css.map")))
(should-not (.exists (io/file "spec/out/map/foo.css")))
(should-not (.exists (io/file "spec/out/map/foo.css.map")))))))
(sass "spec-map" "clean")
(should-not (.exists (io/file "spec/out/map")))))))
53 changes: 0 additions & 53 deletions spec/leiningen/utils_spec.clj

This file was deleted.

23 changes: 13 additions & 10 deletions src/leiningen/cleaner.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
(ns leiningen.cleaner
(:use leiningen.utils)
(:require [clojure.java.io :as io]))
(:require [leiningen.utils :as utils]
[clojure.java.io :as io]
[clojure.string :as str]))

(defn clean-all!
[{:keys [output-directory delete-output-dir] :as options}]
(doseq [[_ dest-file] (files-from options)]
(delete-file! (io/file dest-file))
(delete-file! (io/file (str (.getPath dest-file) ".map"))))
(defn target->dest-file [target]
(-> target
(str/split #":" 2)
last
io/file))

(when (and delete-output-dir (exists output-directory) (dir-empty? output-directory))
(println (str "Destination folder " output-directory " is empty - Deleting it"))
(delete-directory-recursively! output-directory)))
(defn clean-all!
[{:keys [targets]}]
(doseq [dest-file (map target->dest-file targets)]
(utils/delete-file! dest-file)
(utils/delete-file! (io/file (str (.getPath dest-file) ".map")))))
69 changes: 11 additions & 58 deletions src/leiningen/render.clj
Original file line number Diff line number Diff line change
@@ -1,64 +1,17 @@
(ns leiningen.render
(:use leiningen.utils)
(:require [clojure-watch.core :refer [start-watch]]
[clojure.java.shell :as shell]
[clojure.java.io :as io]
[clojure.string :as string])
(:import java.lang.Thread))
(:require [leiningen.utils :as utils]
[clojure.java.shell :as shell]))

(defn- sassc-version* []
(->> (shell/sh "sassc" "-v")
:out
(re-find #"sassc: (.*)")
second))

(defn- sassc-version []
(map #(Integer/parseInt %) (string/split (sassc-version*) #"\.")))

(defn- source-map-args []
(let [[major minor patch] (sassc-version)]
(if (and (>= major 3) (>= minor 4) (>= patch 6))
["--sourcemap=auto"]
["--sourcemap"])))

(defn build-command-vec [src-file dest-file {:keys [command style source-maps]}]
(let [src-path (.getPath src-file)
dest-path (.getPath dest-file)
sass-style (name style)]
(case command
:sassc (let [opts [ "-t" sass-style src-path dest-path]
add-opts (if source-maps (source-map-args) [])]
(concat ["sassc"] add-opts opts))

:sass (let [opts [ "--update" "--force" "-t" sass-style (str src-path ":" dest-path)]
add-opts (if source-maps (source-map-args) [])]
(concat ["sass"] add-opts opts))

:dart-sass (let [opts [ "--update" "-s" sass-style (str src-path ":" dest-path)]
add-opts (if source-maps ["--source-map"] [])]
(concat ["sass"] add-opts opts)))))
(defn build-command-vec [{:keys [targets style source-maps watch]}]
(let [sass-style (name style)
opts (cond-> ["--update" "--style" sass-style]
(not source-maps) (conj "--no-source-map")
watch (conj "--watch"))]
(concat ["sass"] opts targets)))

(defn render
[src-file dest-file options]
(when (and (not (is-partial? src-file)) (name-matches? src-file options))
(io/make-parents dest-file)
(let [opts-vec (build-command-vec src-file dest-file options)]
(println (str " [sass] - " (.getName src-file)))
(println (:err (apply shell/sh opts-vec))))))

(defn render-once!
[options]
(let [descriptors (files-from options)]
(doseq [[src-file dest-file] descriptors]
(render src-file dest-file options))))

(defn render-loop!
([options]
(render-once! options)
(start-watch [{:path (:src options)
:event-types [:create :modify :delete]
:callback (fn [_ _] (render-once! options))
:options {:recursive true}}])
(let [t (Thread/currentThread)]
(locking t
(.wait t)))))
(let [opts-vec (build-command-vec options)]
(println (str " [sass] - " (utils/targets->str (:targets options))))
(println (:err (apply shell/sh opts-vec)))))

0 comments on commit e5579a5

Please sign in to comment.