LoginSignup
3
0

More than 5 years have passed since last update.

figwheel-sidecar

Last updated at Posted at 2017-07-11

Figwheel はGitHubのプロジェクト名(lein-figweel)から察するとleiningenプラグインとしてしか使えないような印象を受けるが、実際はfigwheel-sidecarという単体で利用可能なライブラリの上に作られており、自由度が高い物だったという話

ライブラリとして使いたくなる理由

  • leiningen以外のビルドツールから使いたい
  • 起動、停止のタイミングをコントロールしたい
    • leiningenプラグインだと、figwheelの設定を変更した場合leinごと再起動する必要がある
  • figwheelの起動のタイミングで他の処理を行いたい
    • 例:css watcherの起動
  • 自分のフレームワーク/ワークフローに組み込みたい

使い方

figwheel-sidecarプロジェクトのREADMEに詳しい応用を含めた例が載っているが、ここでは起動と停止に絞ってreplセッションに注釈を付けてみる。(後からやる気が出て来れば他の応用に触れてみたい)

(require '[figwheel-sidecar.system :as sys])
(require '[com.stuartsierra.component :as c])

;;fetch-configはfigwheel.ednファイル,もしくはproject.cljの :cljsbuild と :figwheel の内容からfigwheelの設定値を作って返す
(def config (sys/fetch-config))

;;figwheelのStuart Sierraシステム(必要なコンポーネントの依存関係の定義)をvarに束縛する
;;https://github.com/stuartsierra/component
(def system
      (c/system-map
       :figwheel-system
       (sys/figwheel-system config)))

;;varに束縛されているオブジェクトにc/start関数を適用して起動する
(alter-var-root #'system c/start)
;Figwheel: Starting server at http://0.0.0.0:3449
;Figwheel: Watching build - example
;Figwheel: Cleaning build - example
;Compiling "out/fiddles.js" from ["src"]...
;Successfully compiled "out/fiddles.js" in 4.024 seconds.

;;varに束縛されているオブジェクトにc/stop関数を適用して停止する
(alter-var-root #'system c/stop)
;Figwheel: Stopped watching build - example
;Figwheel: Stopping Websocket Server

余談:leiningenプラグインの実装方法の話

最初に記述したような理由からreplから単体で使えるライブラリを作り、その上にleiningenのプラグインとしての薄い実装を提供することが推奨されている。
leiningenからしか使えない有用なpluginを作ってしまうとClojure界の重鎮がmavenしか使えない顧客のためにIssueを立ててくるみたいなので今後のlein plugin開発の参考にすると良いかも知れない。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0