Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Service Worker support to module.cljs #8

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ pom.xml.asc
/.nrepl-port
.hgignore
.hg/
/.idea/
*.idea
.idea
*.iml
.iml
6 changes: 3 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(defproject duct/module.cljs "0.3.2"
(defproject duct/module.cljs "0.4.0"
:description "Duct module for developing and compiling ClojureScript"
:url "https://github.com/duct-framework/module.cljs"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.9.0-RC2"]
[org.clojure/clojurescript "1.9.946"]
:dependencies [[org.clojure/clojure "1.9.0"]
[org.clojure/clojurescript "1.10.238"]
[binaryage/devtools "0.9.7"]
[duct/core "0.6.1"]
[duct/compiler.cljs "0.2.0"]
Expand Down
63 changes: 48 additions & 15 deletions src/duct/module/cljs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,58 @@
(defn- target-public-path [config options]
(str core/target-path "/resources/" (project-dirs config options) "/public"))

(defn- compiler-config [path main]
(defn- compiler-config [path main sw-main]
{:duct.compiler/cljs
{:builds ^:displace
[{:source-paths ["src"]
:build-options
{:main main
:output-to (str path "/js/main.js")
:output-dir (str path "/js")
:asset-path "/js"
:closure-defines {'goog.DEBUG false}
:verbose true
:optimizations :advanced}}]}})
[(when sw-main
{:id "sw"
:source-paths ["src"]
:build-options
{:main sw-main
:output-to (str path "/sw.js")
:output-dir (str path "/sw")
:asset-path "/sw"
:closure-defines {'goog.DEBUG false}
:verbose true
:infer-externs true
:language-in :es6
:rewrite-polyfills true
:optimizations :advanced
:target :webworker}})
{:source-paths ["src"]
:build-options
{:main main
:output-to (str path "/js/main.js")
:output-dir (str path "/js")
:asset-path "/js"
:closure-defines {'goog.DEBUG false}
:verbose true
:optimizations :advanced}}]}})

(defn- figwheel-config [path main]
(defn- figwheel-config [path main sw-main]
{:duct.server/figwheel
{:css-dirs ^:displace ["resources" "dev/resources"]
:builds ^:displace
[{:id "dev"
[(when sw-main
{:id "sw-dev"
:figwheel false
:source-paths ["dev/src" "src"]
:build-options
{:main sw-main
:output-to (str path "/sw.js")
:output-dir (str path "/sw")
:asset-path "/sw"
:closure-defines {'goog.DEBUG true}
:verbose false
:infer-externs true
:language-in :es6
:rewrite-polyfills true
:preloads '[devtools.preload]
:optimizations :none
:target :webworker}})
{:id "dev"
:figwheel true
:source-paths ["dev/src" "src" ]
:source-paths ["dev/src" "src"]
:build-options
{:main main
:output-to (str path "/js/main.js")
Expand All @@ -51,7 +83,8 @@
(defmethod ig/init-key :duct.module/cljs [_ options]
{:fn (fn [config]
(let [path (target-public-path config options)
sw-main (:sw-main options)
main (:main options)]
(case (get-environment config options)
:production (core/merge-configs config (compiler-config path main))
:development (core/merge-configs config (figwheel-config path main)))))})
:production (core/merge-configs config (compiler-config path main sw-main))
:development (core/merge-configs config (figwheel-config path main sw-main)))))})
89 changes: 81 additions & 8 deletions test/duct/module/cljs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,39 @@

(core/load-hierarchy)

(def service-worker-base-config
{:duct.core/project-ns 'foo
:duct.module/cljs {:main 'foo.client
:sw-main 'foo.sw}})

(def base-config
{:duct.core/project-ns 'foo
:duct.module/cljs {:main 'foo.client}})

(defn- absolute-path [relative-path]
(.getAbsolutePath (io/file relative-path)))

(deftest module-test
(testing "production config"
(is (= (core/prep base-config)
(merge base-config
(deftest service-worker-module-test
(testing "production config with service worker"
(is (= (core/prep service-worker-base-config)
(merge service-worker-base-config
{:duct.compiler/cljs
{:builds
[{:source-paths ["src"]
[{:id "sw"
:source-paths ["src"]
:build-options
{:main 'foo.sw
:output-to (absolute-path "target/resources/foo/public/sw.js")
:output-dir (absolute-path "target/resources/foo/public/sw")
:asset-path "/sw"
:closure-defines {'goog.DEBUG false}
:verbose true
:infer-externs true
:language-in :es6
:rewrite-polyfills true
:optimizations :advanced
:target :webworker}}
{:source-paths ["src"]
:build-options
{:main 'foo.client
:output-to (absolute-path "target/resources/foo/public/js/main.js")
Expand All @@ -30,14 +49,30 @@
:verbose true
:optimizations :advanced}}]}}))))

(testing "development config"
(let [config (assoc base-config ::core/environment :development)]
(testing "development config with service worker"
(let [config (assoc service-worker-base-config ::core/environment :development)]
(is (= (core/prep config)
(merge config
{:duct.server/figwheel
{:css-dirs ["resources" "dev/resources"]
:builds
[{:id "dev"
[{:id "sw-dev"
:figwheel false
:source-paths ["dev/src" "src"]
:build-options
{:main 'foo.sw
:output-to (absolute-path "target/resources/foo/public/sw.js")
:output-dir (absolute-path "target/resources/foo/public/sw")
:asset-path "/sw"
:closure-defines {'goog.DEBUG true}
:verbose false
:infer-externs true
:language-in :es6
:rewrite-polyfills true
:preloads '[devtools.preload]
:optimizations :none
:target :webworker}}
{:id "dev"
:figwheel true
:source-paths ["dev/src" "src"]
:build-options
Expand All @@ -49,3 +84,41 @@
:verbose false
:preloads ['devtools.preload]
:optimizations :none}}]}}))))))

(deftest module-test
(testing "production config without service worker"
(is (= (core/prep base-config)
(merge base-config
{:duct.compiler/cljs
{:builds
[nil
{:source-paths ["src"]
:build-options
{:main 'foo.client
:output-to (absolute-path "target/resources/foo/public/js/main.js")
:output-dir (absolute-path "target/resources/foo/public/js")
:asset-path "/js"
:closure-defines {'goog.DEBUG false}
:verbose true
:optimizations :advanced}}]}}))))

(testing "development config without service worker"
(let [config (assoc base-config ::core/environment :development)]
(is (= (core/prep config)
(merge config
{:duct.server/figwheel
{:css-dirs ["resources" "dev/resources"]
:builds
[nil
{:id "dev"
:figwheel true
:source-paths ["dev/src" "src"]
:build-options
{:main 'foo.client
:output-to (absolute-path "target/resources/foo/public/js/main.js")
:output-dir (absolute-path "target/resources/foo/public/js")
:asset-path "/js"
:closure-defines {'goog.DEBUG true}
:verbose false
:preloads ['devtools.preload]
:optimizations :none}}]}}))))))