Skip to content

Commit

Permalink
Add naive bundler
Browse files Browse the repository at this point in the history
  • Loading branch information
swlkr committed Feb 21, 2020
1 parent 588f6f9 commit 7cb45b2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion joy
Expand Up @@ -11,6 +11,7 @@
Actions:
help - Print this usage information
new <project-name> - Create a new folder with the name of your project
bundle [path] - Bundles css and js files
create <db|route|table|migration> <name> - Generate a new thing
migrate - Migrates the database
rollback - Rolls back the most recent migration
Expand All @@ -32,4 +33,5 @@
"rollback" (joy/rollback (joy/env :database-url))
"server" (os/shell "jpm run server")
"watch" (os/shell "jpm run watch")
"version" (print joy/version)))
"version" (print joy/version)
"bundle" (joy/bundle options)))
1 change: 1 addition & 0 deletions src/joy.janet
Expand Up @@ -12,4 +12,5 @@
(import ./joy/cli :prefix "" :export true)
(import ./joy/form-helper :prefix "" :export true)
(import ./joy/http :as http :export true)
(import ./joy/bundler :prefix "" :export true)
(import halo :prefix "" :export true)
27 changes: 27 additions & 0 deletions src/joy/bundler.janet
@@ -0,0 +1,27 @@
(import ./html :as html)


(defn- concat-files [filenames ext]
(var parts @[])
(loop [filename :in filenames]
(let [f (file/open (string "public/" filename) :r)
str (file/read f :all)
parts (array/push parts str)]
(file/close f))
(let [f (file/open (string "public/bundle." ext) :w)]
(file/write f (string/join parts "\n\n"))
(file/close f))))


(def- css? (partial string/has-suffix? ".css"))
(def- js? (partial string/has-suffix? ".js"))
(def- bundle? (partial string/has-prefix? "bundle"))


(defn bundle [[path]]
(let [files (sort (os/dir (or path "public")))
files (filter |(not (bundle? $)) files)
css-files (filter css? files)
js-files (filter js? files)]
(concat-files css-files "css")
(concat-files js-files "js")))
22 changes: 22 additions & 0 deletions src/joy/html.janet
@@ -1,5 +1,6 @@
# html.janet
# parts of the code shamelessly stolen from https://github.com/brandonchartier/html
(import ./env :as env)

(defn- escape [string-arg]
(if (string? string-arg)
Expand Down Expand Up @@ -137,3 +138,24 @@

(defn html [& args]
(string/join (map create args) ""))


(defn link [href]
[:link {:href href :rel "stylesheet"}])


(defn script [src]
[:script {:src src}])


(defn css [filenames]
(if env/development?
(map link filenames)
(link "/bundle.css")))


(defn js [filenames]
(if env/development?
(map script filenames)
(script "/bundle.js")))

0 comments on commit 7cb45b2

Please sign in to comment.