履歴: プログラミング/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/10/4 4:45:38) → 比較: プログラミング/Riot.js/riot_js-railsの手直し (2016/10/12 14:57:28)

@@ -1,15 +1,18 @@
[[公開メモ]]
+#contents
+
* 概要 [#ha9b50ed]
rails から riot.js を使うための gem として、
[[riot_js-rails>https://github.com/bjarosze/riot_js-rails]] というのがあるのですが、
環境によってどうもうまく動かないように思えたため、手直しできないか調べてみました。
-結果、どうにも手に負える物ではなかった感じです。。。
+「[[それっぽい記事を発見>#l4374caa]]」に原因が、「[[やってみる>#la58b64f]]」 以下に回答らしきものがあります。
* 症状 [#ofe19cd3]
+ LANG:console
$ ruby --version
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-linux]
$ rails new riot_js-rails_sample -B
@@ -74,6 +77,7 @@
require_tree で test-tag.tag が読み込まれていません。
+ LANG:console
$ mv app/assets/javascripts/test-tag.tag app/assets/javascripts/test-tag.js.tag
$ wget -q -O - http://localhost:3000/
<script data-turbolinks-track="true" src="/assets/riot.js?body=1"></script>
@@ -93,6 +97,7 @@
* Rails のバージョンを上げると [#a1c9dd34]
+ LANG:console
$ rvm use ruby-2.2.1
$ ruby --version
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux]
@@ -138,6 +143,7 @@
* ソースを確認 [#o87bc502]
+ LANG:console
$ cd ..
$ git clone https://github.com/bjarosze/riot_js-rails.git
Cloning into 'riot_js-rails'...
@@ -299,79 +305,571 @@
それにしても黒魔術すぎて、キビシイなあ
-* それっぽい記事を発見(未検証) [#l4374caa]
+* それっぽい記事を発見 [#l4374caa]
+Sprockets のバージョン間の非互換性が原因ということで調べたところ以下の記事を発見しました。
+
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
-これによると、
+この通りの手順を踏めば、Sprockets 2, 3 & 4 のすべてで動くものを作れる、と。
+さらに、ものすごく重要なことがさらっと書いてあった。
+
+>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.
+
+Sprockets 4 ではさらに大幅な変更が来るみたいですね。
+
+** 現行のコードと比べてみる [#i62a3d88]
+
+riot_js-rails/lib/riot_js/rails/processors/sprockets_processor_v2.rb
LANG:ruby
- class SprocketsExtensionBase
- def initialize(filename, &block)
- @filename = filename
- @source = block.call
- end
+ module RiotJs
+ module Rails
+ class SprocketsProcessor < Tilt::Template
- def render(context, empty_hash_wtf)
- self.class.run(@filename, @source, context)
- end
+ self.default_mime_type = 'application/javascript'
- def self.run(filename, source, context)
- /* return the modified source */
+ 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
+
+~
+
+riot_js-rails/lib/riot_js/rails/processors/sprockets_processor_v3.rb
+ LANG:ruby
+ module RiotJs
+ module Rails
+ class SprocketsProcessor
+ def self.instance
+ @instance ||= new
+ end
- def self.call(input)
- filename = input[:filename]
- source = input[:data]
- context = input[:environment].context_class.new(input)
+ def self.call(input)
+ instance.call(input)
+ end
+
+ def call(input)
+ prepare(input)
+ data = process
+
+ @context.metadata.merge(data: data)
+ end
+
+ def self.register_self(config)
+ config.assets.configure do |env|
+ env.register_engine '.tag', self, mime_type: 'application/javascript'
+ end
+ end
- result = run(filename, source, context)
- context.metadata.merge(data: result)
+ private
+
+ def process
+ raise 'Not implemented'
+ end
+
+ def prepare(input)
+ @context = input[:environment].context_class.new(input)
+ @data = input[:data]
+ end
+
+ end
end
end
-を継承して run を override すれば Extention を作れる。
+~
- require 'sprockets/processing'
- extend Sprockets::Processing
+riot_js-rails/lib/riot_js/rails/processors/processor.rb
+ LANG:ruby
+ require 'riot_js/rails/processors/compiler'
- register_preprocessor 'text/css', TheSprocketsExtension
+ 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
-で拡張機能を登録して、
+いろいろ面倒な手順は踏んでいるけれど、やろうとしていることは同じように見える?
- 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
+** やってみる [#la58b64f]
+
+まず前提として、Sprocket 3 以降では development 環境でも assets のキャッシュのクリアはひと手間必要で、ちゃんとクリアしないと古い内容が読み込まれてしまうのが問題の特定を難しくしていたっぽい。
+
+ LANG:console
+ $ rake tmp:cache:clear
+ $ rake assets:clean
+
+でクリアできるとのこと。
+
+これがないとまったく始まらない(汗
+
+で、下記コードはかなり良い線行っている気がします。
+
+ LANG:ruby
+ require 'riot_js/rails/processors/compiler'
+
+ module RiotJs
+ module Rails
+
+ # Sprockets 2, 3 & 4 interface
+ class SprocketsExtensionBase
+ attr_reader :default_mime_type
+
+ 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)
+ raise 'Not implemented'
+ end
+
+ def self.call(input)
+ if input.is_a?(String)
+ run("", input, nil)
+ else
+ 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
+
+ private
+
+ def self.register_self_helper(app, config, file_ext, mime_type_from, mime_type_to, charset=nil)
+
+ if config.respond_to?(:assets)
+ config.assets.configure do |env|
+ if env.respond_to?(:register_transformer)
+ # Sprockets 3 and 4
+ env.register_mime_type mime_type_from, extensions: [file_ext], charset: charset
+ env.register_transformer mime_type_from, mime_type_to, self
+ elsif env.respond_to?(:register_engine)
+ if Sprockets::VERSION.start_with?("3")
+ # Sprockets 3 ... is this needed?
+ env.register_engine file_ext, self, { mime_type: mime_type_to, silence_deprecation: true }
+ else
+ # Sprockets 2.12.4
+ @default_mime_type = mime_type_to
+ env.register_engine file_ext, self
+ end
+ end
+ end
+ else
+ # Sprockets 2.2.3
+ @default_mime_type = mime_type_to
+ app.assets.register_engine file_ext, self
+ end
+ end
+
+ end
+
+ class Processor < SprocketsExtensionBase
+
+ def self.run(filename, source, context)
+ ::RiotJs::Rails::Compiler.compile(source)
+ end
+
+ def self.register_self(app, config)
+ # app is a YourApp::Application
+ # config is Rails::Railtie::Configuration that belongs to RiotJs::Rails::Railtie
+ register_self_helper(app, config, '.tag', 'text/riot-tag', 'application/javascript', :html)
+ end
+
+ end
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
+
+~
+
+ LANG:ruby
+ require 'rails/railtie'
+ require 'riot_js/rails/processors/processor'
+ require 'riot_js/rails/helper'
+
+ module RiotJs
+ module Rails
+ class Railtie < ::Rails::Railtie
+ config.riot = ActiveSupport::OrderedOptions.new
+ config.riot.node_paths = []
+
+ initializer :setup_sprockets do |app|
+ # app is a YourApp::Application
+ # config is Rails::Railtie::Configuration that belongs to RiotJs::Rails::Railtie
+ Processor.register_self app, config
+
+ if defined?(::Haml)
+ require 'tilt/haml'
+ Haml::Template.options[:format] = :html5
+
+ if config.respond_to?(:assets)
+ config.assets.configure do |env|
+ if env.respond_to?(:register_transformer)
+ # Sprockets 3 and 4
+ env.register_mime_type 'text/riot-tag+haml', extensions: ['.tag.haml'], charset: :html
+ env.register_transformer 'text/riot-tag+haml', 'application/javascript',
+ Proc.new{ |input| Processor.call(::Haml::Engine.new(input[:data]).render) }
+ elsif env.respond_to?(:register_engine)
+ if Sprockets::VERSION.start_with?("3")
+ # Sprockets 3 ... is this needed?
+ env.register_engine '.haml', ::Tilt::HamlTemplate, { silence_deprecation: true }
+ else
+ # Sprockets 2.12.4
+ env.register_engine '.haml', ::Tilt::HamlTemplate
+ end
+ end
+ end
+ else
+ # Sprockets 2
+ app.assets.register_engine '.haml', ::Tilt::HamlTemplate
+ end
+ end
+ end
+
+ initializer :add_helpers do |app|
+ helpers = %q{ include ::RiotJs::Rails::Helper }
+ ::ActionView::Base.module_eval(helpers)
+ ::Rails.application.config.assets.context_class.class_eval(helpers)
+ end
+
+ config.after_initialize do |app|
+ node_paths = ENV['NODE_PATH'].to_s.split(':')
+ node_paths += app.config.riot.node_paths
+ node_global_path = detect_node_global_path
+ node_paths << node_global_path if node_global_path
+
+ ENV['NODE_PATH'] = node_paths.join(':')
+ end
+
+ def detect_node_global_path
+ prefix = `npm config get prefix`.to_s.chomp("\n")
+ possible_paths = [ "#{prefix}/lib/node", "#{prefix}/lib/node_modules" ]
+
+ possible_paths.each do |path|
+ return path if File.directory?(path)
+ end
+ return
+ end
+
+ end
end
end
-これで良さそう?
+** テスト結果 [#sbab7551]
-あら、ものすごく重要なことがさらっと書いてあった。
+|ruby version|rails version|sprockets version|result|comment|
+|1.9.3|3.0.20|--|NG|sprockets is not available|
+|1.9.3|3.1.12|2.0.5|ok|extension must be .js.tag instead of .tag|
+|1.9.3|3.2.11|2.2.3|ok|extension must be .js.tag instead of .tag|
+|1.9.3|4.0.0|2.12.4|ok|extension must be .js.tag instead of .tag|
+|1.9.3|4.1.16|2.12.4|ok|extension must be .js.tag instead of .tag|
+|2.2.1|4.2.0|3.7.0|ok||
+|2.3.1|5.0.0.1|3.7.0|ok||
->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 だから、そういう問題ではないか。
+通常の .tag ファイルが正しく読み込めるかどうかと、
+haml を使った .tag.haml ファイルが正しく読み込めるかどうかをテストしている。
- if Sprockets::VERSION.start_with?("4")
- register_sprockets_extention env, 'application/javascript', '.tag.haml', nil,
- Proc.new {|input| TagProcessor.call(HAMLProcessor.call(input)) }
+うまくいっていれば http://localhost:3000/ をブラウザで見た時に
+ <test> tag is working.
+ <haml-test> tag is working.
+
+と表示される。
+
+*** sprockets 2.2.3 / 2.0.5 のテストスクリプト [#e11ce618]
+
+ LANG:sh
+ #!/bin/sh
+ rvm use 1.9.3-p551
+ rvm gemset create riot-test
+ rvm use 1.9.3-p551@riot-test
+ gem uninstall --all --force
+ gem install rails -N -v 3.2.11
+ rails new riot-test
+ cd riot-test
+ rm public/index.html
+ echo "\
+ RiotTest::Application.routes.draw do
+ root :to => 'main#index'
end
+ " >config/routes.rb
+ rails g controller main index
+ # rails s # to test sprockets works well
+ echo "
+ gem 'therubyracer', platforms: :ruby
+ gem 'haml-rails'
+ gem 'riot_js-rails', path: 'path/to/riot_js-rails'
+ " >>Gemfile
+ bundle install
+ mkdir app/assets/javascripts/riot-tags
+ echo "\
+ <test>
+ <div>&lt;test&gt; tag is working.</div>
+ </test>
+ " >app/assets/javascripts/riot-tags/test.js.tag
+ echo '
+ %haml-test
+ %div
+ &="<haml-test> tag is working."
+ ' >app/assets/javascripts/riot-tags/haml-test.js.tag.haml
+ echo '
+ <test data-riot></test>
+ <haml-test data-riot></haml-test>
+ ' >app/views/main/index.html.erb
+ echo "\
+ //= require jquery
+ //= require jquery_ujs
+ //= require riot
+ //= require riot_rails
+ //= require_tree .
+ " >app/assets/javascripts/application.js
+ grep sprockets Gemfile.lock
+ rails s
+
-こんな感じかな?
+後始末は
-後で試さねば。
+ LANG:sh
+ #!/bin/sh
+ cd ..
+ rm -r riot-test
+ rvm gemset delete riot-test
+
+rails 3.1.12 = sprockets 2.0.5 も、上記の 3.2.11 を 3.1.12 とするだけで動いた。
+
+
+*** sprockets 2.12.4 のテストスクリプト: [#c777ace6]
+
+ LANG:sh
+ #!/bin/sh
+ rvm use 1.9.3
+ rvm gemset create riot-test
+ rvm use 1.9.3@riot-test
+ gem uninstall --all --force
+ gem install rails -N -v 4.0.0
+ rails new riot-test
+ cd riot-test
+ echo "\
+ RiotTest::Application.routes.draw do
+ root :to => 'main#index'
+ end
+ " >config/routes.rb
+ rails g controller main index
+ # rails s # to test sprockets works well
+ echo "
+ source 'https://rubygems.org'
+ gem 'rails', '4.0.0'
+ gem 'sqlite3'
+ gem 'sass-rails', '~> 4.0.0'
+ gem 'coffee-rails', '~> 4.0.0'
+ gem 'jquery-rails'
+ gem 'therubyracer', platforms: :ruby
+ gem 'haml-rails'
+ gem 'riot_js-rails', path: 'path/to/riot_js-rails'
+ " >Gemfile
+ bundle install
+ mkdir app/assets/javascripts/riot-tags
+ echo "\
+ <test>
+ <div>&lt;test&gt; tag is working.</div>
+ </test>
+ " >app/assets/javascripts/riot-tags/test.js.tag
+ echo '
+ %haml-test
+ %div
+ &="<haml-test> tag is working."
+ ' >app/assets/javascripts/riot-tags/haml-test.js.tag.haml
+ echo '
+ <test data-riot></test>
+ <haml-test data-riot></haml-test>
+ ' >app/views/main/index.html.erb
+ echo "\
+ //= require jquery
+ //= require jquery_ujs
+ //= require riot
+ //= require riot_rails
+ //= require_tree .
+ " >app/assets/javascripts/application.js
+ grep sprockets Gemfile.lock
+ rails s
+
+rails 4.1.16 = sprockets 2.12.4 も同様にして動いた。
+
+このとき Gemfile は、
+
+ LANG:sh
+ #!/bin/sh
+ echo "
+ source 'https://rubygems.org'
+ gem 'rails', '4.1.16'
+ gem 'sqlite3'
+ gem 'sass-rails', '~> 4.0.3'
+ gem 'coffee-rails', '~> 4.0.0'
+ gem 'jquery-rails'
+ gem 'therubyracer', platforms: :ruby
+ gem 'haml-rails'
+ gem 'riot_js-rails', path: 'path/to/riot_js-rails'
+ " >Gemfile
+
+とし、rails g controller main index の前に Gemfile を書き換えないと
+spring が邪魔をして正しく controller を作成できなかった~
+(恐らく spring にバージョン指定が必要)
+
+*** sprockets 3.7.0 (rails 4.2) のテストスクリプト: [#c777ace6]
+
+ LANG:sh
+ #!/bin/sh
+ rvm use 2.2.1
+ rvm gemset create riot-test
+ rvm use 2.2.1@riot-test
+ gem uninstall --all --force
+ gem install rails -N -v 4.2
+ rails _4.2.0_ new riot-test
+ cd riot-test
+ echo "\
+ RiotTest::Application.routes.draw do
+ root :to => 'main#index'
+ end
+ " >config/routes.rb
+ rails g controller main index
+ # rails s # to test sprockets works well
+ echo "
+ source 'https://rubygems.org'
+ gem 'rails', '4.2.0'
+ gem 'sqlite3'
+ gem 'sass-rails', '~> 5.0'
+ gem 'coffee-rails', '~> 4.1.0'
+ gem 'jquery-rails'
+ gem 'therubyracer', platforms: :ruby
+ gem 'haml-rails'
+ gem 'riot_js-rails', path: 'path/to/riot_js-rails'
+ " >Gemfile
+ bundle install
+ mkdir app/assets/javascripts/riot-tags
+ echo "\
+ <test>
+ <div>&lt;test&gt; tag is working.</div>
+ </test>
+ " >app/assets/javascripts/riot-tags/test.tag
+ echo '
+ %haml-test
+ %div
+ &="<haml-test> tag is working."
+ ' >app/assets/javascripts/riot-tags/haml-test.tag.haml
+ echo '
+ <test data-riot></test>
+ <haml-test data-riot></haml-test>
+ ' >app/views/main/index.html.erb
+ echo "\
+ //= require jquery
+ //= require jquery_ujs
+ //= require riot
+ //= require riot_rails
+ //= require_tree .
+ " >app/assets/javascripts/application.js
+ grep sprockets Gemfile.lock
+ rails s
+
+*** sprockets 3.7.0 (rails 5.0.0.1) のテストスクリプト [#u426f453]
+
+ LANG:sh
+ #!/bin/sh
+ rvm use 2.3.1
+ rvm gemset create riot-test
+ rvm use 2.3.1@riot-test
+ gem uninstall --all --force
+ gem install rails -N -v 5.0.0.1
+ rails _5.0.0.1_ new riot-test
+ cd riot-test
+ echo "\
+ RiotTest::Application.routes.draw do
+ root :to => 'main#index'
+ end
+ " >config/routes.rb
+ rails g controller main index
+ # rails s # to test sprockets works well
+ echo "
+ source 'https://rubygems.org'
+ gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
+ gem 'sqlite3'
+ gem 'sass-rails', '~> 5.0'
+ gem 'coffee-rails', '~> 4.2'
+ gem 'jquery-rails'
+ gem 'listen', '~> 3.0.5'
+ gem 'therubyracer', platforms: :ruby
+ gem 'haml-rails'
+ gem 'riot_js-rails', path: 'path/to/riot_js-rails'
+ " >Gemfile
+ bundle install
+ mkdir app/assets/javascripts/riot-tags
+ echo "\
+ <test>
+ <div>&lt;test&gt; tag is working.</div>
+ </test>
+ " >app/assets/javascripts/riot-tags/test.tag
+ echo '
+ %haml-test
+ %div
+ &="<haml-test> tag is working."
+ ' >app/assets/javascripts/riot-tags/haml-test.tag.haml
+ echo '
+ <test data-riot></test>
+ <haml-test data-riot></haml-test>
+ ' >app/views/main/index.html.erb
+ echo "\
+ //= require jquery
+ //= require jquery_ujs
+ //= require riot
+ //= require riot_rails
+ //= require_tree .
+ " >app/assets/javascripts/application.js
+ grep sprockets Gemfile.lock
+ rails s
+
+** プルリクエストを書いてみた [#i044a1a0]
+
+作法をよく知らないので、失礼がないと良いのだけれど。
+
+https://github.com/bjarosze/riot_js-rails/pull/18