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

using figwheel in translation #70

Open
keerts opened this issue Jan 21, 2016 · 3 comments
Open

using figwheel in translation #70

keerts opened this issue Jan 21, 2016 · 3 comments

Comments

@keerts
Copy link

keerts commented Jan 21, 2016

We are used to the rapid feedback cycle that figwheel gives us in development. So we tried to make translation fit in nicely, but can't seem to get it to work. Any idea what could cause this?

We are trying to achieve this by adding figwheel-always to the metadata (see below). What we expect is that the dictionary reloads for every cljs file that we save. But it doesn't. When I eval the complete namespace (emacs cider-eval-buffer) the dictionary does reload. Especially that last fact puzzles me.

(ns ^:figwheel-always relations-for-jira.localization
  (:require [taoensso.tower :as tower :include-macros true]))

(def ^:private tconfig
  {:fallback-locale :en-US
   :dev-mode? true
   :compiled-dictionary (tower/dict-compile* "i18n/dictionary.clj")})

(def t (tower/make-t tconfig))
@maciejka
Copy link

maciejka commented Aug 2, 2016

@keerts: Have you managed to solve the problem?

@keerts
Copy link
Author

keerts commented Aug 2, 2016

Unfortunately not. We have moved to boot in the meanwhile.

@ai212983
Copy link

ai212983 commented Sep 13, 2016

In case someone is still interested.

To use tower with figwheel, you have to trigger rebuilding of namespace containing tconfig each time your dictionary.clj changes. It can be done via figwheel scripting.

Here's my figwheel.clj:

(import org.apache.commons.io.filefilter.WildcardFileFilter)
(require
  '[figwheel-sidecar.config :as conf]
  '[leiningen.core.project :as lein]

  '[figwheel-sidecar.components.file-system-watcher :as fsw]
  '[figwheel-sidecar.system :as sys]
  '[com.stuartsierra.component :as component])

; read lein project and extracting config with figwheel internal machinery
(def lein-config (conf/->lein-project-config-source (lein/read)))
(def figwheel-config (-> lein-config
                         conf/config-source->prepped-figwheel-internal
                         (get :data)))

(defn touch [file-path]
  (-> file-path clojure.java.io/file (.setLastModified(System/currentTimeMillis))))

(defn create-notification-handler [file-pattern touch-file]
  (let [f (WildcardFileFilter. (str file-pattern))]
    (fn [_watcher files]
      (when (not-empty (filter #(.accept f nil %) (map str files)))
        (touch touch-file)))))


(def system
  (atom
    (component/system-map
      :figwheel-server (sys/figwheel-system figwheel-config)
      ; sidecar do not have css watcher by default, so we have to add it manually
      ; see https://github.com/bhauman/lein-figwheel/blob/4a34663097cae43865e6008ada062500ebe65102/sidecar/README.md
      :css-watcher (component/using
                     (sys/css-watcher {:watch-paths (get-in figwheel-config [:figwheel-options :css-dirs])})
                     [:figwheel-server])
      :custom-watcher (let [{:keys [dir file-pattern touch-file]} (get-in lein-config [:data :figwheel-watch])]
                        (component/using
                         (fsw/file-system-watcher {:watcher-name         "Custom Watcher"
                                                   :watch-paths          [dir]
                                                   :notification-handler (create-notification-handler file-pattern touch-file)})
                         [:figwheel-server])))))

(defn start []
  (println (prn-str (get-in lein-config [:data :figwheel-watch])))
  (swap! system component/start))

(defn stop []
  (swap! system component/stop))

(defn reload []
  (stop)
  (start))

(defn repl []
  (sys/cljs-repl (:figwheel-server @system)))

;; Start the components and the repl
(start)
(repl)

Figwheel is started with rlwrap lein run -m clojure.main --init script/figwheel.clj -r, and project.clj contains an entry in the root:

 :figwheel-watch {:dir          "resources"
                  :file-pattern "*.clj"
                  :touch-file   "src/cljs/my.project/dictionary.cljs"}

Whenever file matching *.clj is edited in resources directory, dictionary.cljs will be touched and project rebuilt.

You can go fancy, add multiple files/directories support for watching, or rebuild that particular namespace only and notify your application whenever change occur, but that will require some tinkering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants