履歴: プログラミング/Riot.js/riot_js-railsの手直し
各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 18 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます
| 日時 | タイトル | 文字数 | 追加 | 削除 | 内容 | 差分 | 基準 |
|---|---|---|---|---|---|---|---|
| 2017/1/27 17:22:19 | プログラミング/Riot.js/riot_js-railsの手直し 最新版 | 25,291 | +1 | -1 | 表示 | 差分 | — |
| 2017/1/27 17:22:19 | プログラミング/Riot.js/riot_js-railsの手直し | 25,297 | +2 | -1 | 表示 | 差分 | 選択 |
| 2016/10/20 14:32:50 | プログラミング/Riot.js/riot_js-railsの手直し | 25,270 | +2 | -0 | 表示 | 差分 | 選択 |
| 2016/10/20 3:05:00 | プログラミング/Riot.js/riot_js-railsの手直し | 25,217 | +114 | -0 | 表示 | 差分 | 選択 |
| 2016/10/19 7:15:34 | プログラミング/Riot.js/riot_js-railsの手直し | 21,920 | +89 | -14 | 表示 | 差分 | 選択 |
| 2016/10/13 7:49:10 | プログラミング/Riot.js/riot_js-railsの手直し | 19,598 | +47 | -0 | 表示 | 差分 | 選択 |
| 2016/10/13 7:49:10 | プログラミング/Riot.js/riot_js-railsの手直し | 18,578 | +41 | -248 | 表示 | 差分 | 選択 |
| 2016/10/12 14:57:28 | プログラミング/Riot.js/riot_js-railsの手直し | 23,370 | +6 | -0 | 表示 | 差分 | 選択 |
| 2016/10/12 9:36:40 | プログラミング/Riot.js/riot_js-railsの手直し | 23,261 | +299 | -136 | 表示 | 差分 | 選択 |
| 2016/10/11 23:52:46 | プログラミング/Riot.js/riot_js-railsの手直し | 18,982 | +85 | -11 | 表示 | 差分 | 選択 |
| 2016/10/11 15:58:58 | プログラミング/Riot.js/riot_js-railsの手直し | 17,450 | +83 | -19 | 表示 | 差分 | 選択 |
| 2016/10/11 13:50:49 | プログラミング/Riot.js/riot_js-railsの手直し | 15,197 | +193 | -2 | 表示 | 差分 | 選択 |
| 2016/10/4 4:45:38 | プログラミング/Riot.js/riot_js-railsの手直し | 11,053 | +5 | -5 | 表示 | 表示中 | 選択 |
| 2016/10/4 4:45:38 | プログラミング/Riot.js/riot_js-railsの手直し | 11,053 | +13 | -2 | 表示 | 差分 | 選択 |
| 2016/10/4 0:29:17 | プログラミング/Riot.js/riot_js-railsの手直し | 10,750 | +66 | -0 | 表示 | 差分 | 選択 |
| 2016/7/4 14:28:19 | プログラミング/Riot.js/riot_js-railsの手直し | 8,851 | +4 | -1 | 表示 | 差分 | 選択 |
| 2016/7/4 14:28:19 | プログラミング/Riot.js/riot_js-railsの手直し | 8,794 | +189 | -2 | 表示 | 差分 | 選択 |
| 2016/7/4 10:47:00 | プログラミング/Riot.js/riot_js-railsの手直し | 3,926 | — | — | 表示 | — | 基準 |
基準: プログラミング/Riot.js/riot_js-railsの手直し (2016/7/4 10:47:00) → 比較: プログラミング/Riot.js/riot_js-railsの手直し (2016/10/4 4:45:38)
@@ -3,9 +3,11 @@
* 概要 [#ha9b50ed]
rails から riot.js を使うための gem として、
-[[riot_js-rails>>https://github.com/bjarosze/riot_js-rails]] というのがあるのですが、
+[[riot_js-rails>https://github.com/bjarosze/riot_js-rails]] というのがあるのですが、
環境によってどうもうまく動かないように思えたため、手直しできないか調べてみました。
+結果、どうにも手に負える物ではなかった感じです。。。
+
* 症状 [#ofe19cd3]
$ ruby --version
@@ -89,6 +91,51 @@
test-tag.tag ではなく test-tag.js.tag としたところ、読み込もうとする姿勢は見られましたが、
/assets/ ではなく /javascripts/ が指定されており、なおかつ両方とも RoutingError になってしまいます。
+* Rails のバージョンを上げると [#a1c9dd34]
+
+ $ rvm use ruby-2.2.1
+ $ ruby --version
+ ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux]
+ $ rails --version
+ Rails 4.2.6
+ $ rails new riot_js-rails_sample -B
+ $ cd riot_js-rails_sample/
+ $ jed Gemfile
+ gem 'riot_js-rails'
+ $ bundle install
+ $ cat Gemfile.lock
+ DEPENDENCIES
+ byebug
+ coffee-rails (~> 4.1.0)
+ jbuilder (~> 2.0)
+ jquery-rails
+ rails (= 4.2.6)
+ riot_js-rails
+ sass-rails (~> 5.0)
+ sdoc (~> 0.4.0)
+ spring
+ sqlite3
+ turbolinks
+ uglifier (>= 1.3.0)
+ web-console (~> 2.0)
+ $ echo "Sprockets::VERSION" | rails c
+ Loading development environment (Rails 4.2.6)
+ Switch to inspect mode.
+ Sprockets::VERSION
+ "3.6.3"
+
+この環境だと、上と同じ操作で
+
+ <script src="/assets/riot.self-d4789eae993b10e05f4694693fa33efe6afd5ccd866d3f6eee21de793efc7f31.js?body=1" data-turbolinks-track="true"></script>
+ <script src="/assets/riot_rails.self-23d22569edc9e83fe5a74dbf647822c59a2fcd08777f496b38cab2507f2d594d.js?body=1" data-turbolinks-track="true"></script>
+ <script src="/assets/main.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="true"></script>
+ <script src="/assets/test-tag.self-198db509a34ddfb1d978f239761117f57e5680e43ffe43f5524092ecb41ca2b3.js?body=1" data-turbolinks-track="true"></script>
+ <script src="/assets/application.self-7862a8a8b42407b4741a1adeeea35f0d13ddc4f702ec532adb0674491d296495.js?body=1" data-turbolinks-track="true"></script>
+
+のように、"test-tag.self-XXXX.js" が正しくコンパイルされます。
+
+どうやら sprockets のバージョン依存・・・なのでしょうか?
+
* ソースを確認 [#o87bc502]
$ cd ..
@@ -107,4 +154,224 @@
On branch fix-asset-path
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
- $
+ $ cat lib/riot_js/rails/railtie.rb
+ require 'rails/railtie'
+ require 'riot_js/rails/processors/processor'
+ require 'riot_js/rails/helper'
+
+ module RiotJs
+ module Rails
+ class Railtie < ::Rails::Railtie
+
+ initializer :setup_sprockets do |app|
+ Processor.register_self config
+ ...
+ $ cat lib/riot_js/rails/processors/processor.rb
+ require 'riot_js/rails/processors/compiler'
+
+ if Gem::Version.new(Sprockets::VERSION) < Gem::Version.new('3.0.0')
+ require 'riot_js/rails/processors/sprockets_processor_v2'
+ else
+ require 'riot_js/rails/processors/sprockets_processor_v3'
+ end
+
+ module RiotJs
+ module Rails
+ class Processor < SprocketsProcessor
+
+ def process
+ compile_tag
+ end
+
+ private
+
+ def compile_tag
+ ::RiotJs::Rails::Compiler.compile(@data)
+ end
+ end
+ end
+ end
+ $ echo "Sprockets::VERSION" | rails c
+ Loading development environment (Rails 4.0.13)
+ Switch to inspect mode.
+ Sprockets::VERSION
+ "2.12.4"
+ $ cat lib/riot_js/rails/processors/sprockets_rocessor_v2.rb
+ module RiotJs
+ module Rails
+ class SprocketsProcessor < Tilt::Template
+
+ self.default_mime_type = 'application/javascript'
+
+ def self.register_self(app)
+ app.assets.register_engine '.tag', self
+ end
+
+ def evaluate(context, locals, &block)
+ @context = context
+ process
+ end
+
+ def prepare
+ @data = data
+ end
+
+ def process
+ raise 'Not implemented'
+ end
+
+ end
+ end
+ end
+
+これを直すのは難しそうです。。。
+
+* coffee-rails のソースを見る [#h68f93cd]
+
+特定の拡張子を持ったファイルを .js に変換するという動作は coffee-rails と同様なので、
+
+https://github.com/rails/coffee-rails/
+
+これと同じことをやれば良いはずなのですが、、、
+
+.coffee から .js へのコンパイル部分は
+
+https://github.com/rails/coffee-rails/blob/master/lib/coffee/rails/template_handler.rb
+
+ LANG:ruby
+ module Coffee
+ module Rails
+ class TemplateHandler
+ def self.erb_handler
+ @@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
+ end
+
+ def self.call(template)
+ compiled_source = erb_handler.call(template)
+ "CoffeeScript.compile(begin;#{compiled_source};end)"
+ end
+ end
+ end
+ end
+
+ ActiveSupport.on_load(:action_view) do
+ ActionView::Template.register_template_handler :coffee, Coffee::Rails::TemplateHandler
+ end
+
+だけだと思われて、なおかつこの部分は上記前者で読まれた v4.0.1 でも、
+後者で読まれた v4.1.1 でも変更されていないにも関わらず、
+同じことをしてみても、どうもうまくいかず、困ってしまいます。
+
+* coffee-rails のまねをする [#c2ace7e8]
+
+ $ git clone https://github.com/rails/coffee-rails.git
+ $ mv coffee-rails/ riottag-rails
+ $ (いろいろ直す)
+
+主には、
+
+ LANG:ruby
+ module RiotTag
+ module Rails
+ class TemplateHandler
+ def self.erb_handler
+ @@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
+ end
+
+ def self.call(template)
+ compiled_source = erb_handler.call(template)
+ "'Compiled!'.html_safe"
+ end
+ end
+ end
+ end
+
+ ActiveSupport.on_load(:action_view) do
+ ActionView::Template.register_template_handler :tag, RiotTag::Rails::TemplateHandler
+ end
+
+全然うまくいきません(涙
+
+というか、coffee-rails の該当部分 ActionView::Template.register_template_handler
+をコメントアウトしても coffee-rails が動き続けているあたり、見ている場所が全然違うのかもしれません?!
+
+素直に rails のバージョンを上げることを考えるべきなのかも・・・
+
+それにしても黒魔術すぎて、キビシイなあ
+
+* それっぽい記事を発見(未検証) [#l4374caa]
+
+Supporting All Versions of Sprockets in Processors~
+https://github.com/rails/sprockets/blob/master/guides/extending_sprockets.md#supporting-all-versions-of-sprockets-in-processors
+
+これによると、
+
+ LANG:ruby
+ class SprocketsExtensionBase
+ def initialize(filename, &block)
+ @filename = filename
+ @source = block.call
+ end
+
+ def render(context, empty_hash_wtf)
+ self.class.run(@filename, @source, context)
+ end
+
+ def self.run(filename, source, context)
+ /* return the modified source */
+ end
+
+ def self.call(input)
+ filename = input[:filename]
+ source = input[:data]
+ context = input[:environment].context_class.new(input)
+
+ result = run(filename, source, context)
+ context.metadata.merge(data: result)
+ end
+ end
+
+を継承して run を override すれば Extention を作れる。
+
+ require 'sprockets/processing'
+ extend Sprockets::Processing
+
+ register_preprocessor 'text/css', TheSprocketsExtension
+
+で拡張機能を登録して、
+
+ def register_sprockets_extention(env, sprockets_extention, mime_type, file_ext, charset=nil)
+ file_ext = [file_ext] unless file_ext.instance_of?(Array)
+ charset = charset.to_sym
+
+ # Sprockets 2, 3, and 4
+
+ if env.respond_to?(:register_transformer)
+ env.register_mime_type mime_type, extensions: file_ext, charset: charset
+ env.register_preprocessor mime_type, sprockets_extention
+ end
+
+ if env.respond_to?(:register_engine)
+ args = [file_ext, sprockets_extention]
+ args << { mime_type: mime_type, silence_deprecation: true } if Sprockets::VERSION.start_with?("3")
+ env.register_engine(*args)
+ end
+ end
+
+これで良さそう?
+
+あら、ものすごく重要なことがさらっと書いてあった。
+
+>prockets 4 will not chain asset extensions so .coffee.erb is explicitly registered in addition to .coffee. If your application introduces a new mime/extension combo it will be responsible for registering all combinations.
+
+だから rails 5 で file.tag.haml が動かなかったのか?~
+いや、rails 5.0.0.1 でも sprockets は 3.7 だから、そういう問題ではないか。
+
+ if Sprockets::VERSION.start_with?("4")
+ register_sprockets_extention env, 'application/javascript', '.tag.haml', nil,
+ Proc.new {|input| TagProcessor.call(HAMLProcessor.call(input)) }
+ end
+
+こんな感じかな?
+
+後で試さねば。