履歴: プログラミング/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/11 23:52:46) → 比較: プログラミング/Riot.js/riot_js-railsの手直し (2016/10/13 7:49:10)
@@ -6,10 +6,30 @@
[[riot_js-rails>https://github.com/bjarosze/riot_js-rails]] というのがあるのですが、
環境によってどうもうまく動かないように思えたため、手直しできないか調べてみました。
-結果、どうにも手に負える物ではなかった感じです。。。
+最終的に、rails 3.1 から 5.0.0.1 までのすべてで動きそうな状況までこぎ着けました。
+「[[それっぽい記事を発見>#l4374caa]]」に原因が、「[[やってみる>#la58b64f]]」 以下に回答らしきものがあります。
+
+この成果をまとめた gem が、https://github.com/osamutake/riot_js-rails こちらにあります。~
+現在、この稿の内容を riot_js-rails の本家へプルリクエストの形で投げ、回答待ちの状況です。
+
+本家に取り込まれるまでの間は Gemfile 中で、
+
+ LANG:ruby
+ gem 'riot_js-rails', :git => 'git://github.com/osamutake/riot_js-rails',
+ :branch => 'support_sprockets_2_3_and_4'
+
+とすれば使えます。
+
+* 目次 [#vb58b525]
+
+#contents
+
* 症状 [#ofe19cd3]
+rails 4.0.13 ではうまく動きませんでした。
+
+ 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 +94,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 +114,9 @@
* Rails のバージョンを上げると [#a1c9dd34]
+rails 4.2.6 では動きました。
+
+ LANG:console
$ rvm use ruby-2.2.1
$ ruby --version
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux]
@@ -138,6 +162,7 @@
* ソースを確認 [#o87bc502]
+ LANG:console
$ cd ..
$ git clone https://github.com/bjarosze/riot_js-rails.git
Cloning into 'riot_js-rails'...
@@ -262,7 +287,7 @@
後者で読まれた v4.1.1 でも変更されていないにも関わらず、
同じことをしてみても、どうもうまくいかず、困ってしまいます。
-* coffee-rails のまねをする [#c2ace7e8]
+** coffee-rails のまねをする [#c2ace7e8]
$ git clone https://github.com/rails/coffee-rails.git
$ mv coffee-rails/ riottag-rails
@@ -299,84 +324,21 @@
それにしても黒魔術すぎて、キビシイなあ
-* それっぽい記事を発見(未検証) [#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 のすべてで動くものを作れる、と。
- 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 を作れる。
+>Sprockets 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.
- LANG:ruby
- require 'sprockets/processing'
- extend Sprockets::Processing
-
- register_preprocessor 'text/css', TheSprocketsExtension
+Sprockets 4 ではさらに大幅な変更が来るみたいですね。
-で拡張機能を登録して、
-
- LANG:ruby
- 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 だから、そういう問題ではないか。
-
- LANG:ruby
- if Sprockets::VERSION.start_with?("4")
- register_sprockets_extention env, 'application/javascript', '.tag.haml', nil,
- Proc.new {|input| TagProcessor.call(HAMLProcessor.call(input)) }
- end
-
-こんな感じかな?
-
** 現行のコードと比べてみる [#i62a3d88]
riot_js-rails/lib/riot_js/rails/processors/sprockets_processor_v2.rb
@@ -484,18 +446,17 @@
** やってみる [#la58b64f]
-あー、development 環境でも assets のキャッシュのクリアはひと手間必要で、
-ちゃんとクリアしないと古い内容が読み込まれてしまうのが第一の問題だったっぽい。
+まず前提として、Sprocket 3 以降では development 環境でも assets のキャッシュのクリアはひと手間必要で、ちゃんとクリアしないと古い内容が読み込まれてしまうのが問題の特定を難しくしていたっぽい。
LANG:console
$ rake tmp:cache:clear
$ rake assets:clean
-でクリアできた。
+でクリアできるとのこと。
-これがないとまったく始まらない(汗
+上でにっちもさっちもいかなかったのはこれのせい(汗
-で、下記コードでいろいろテスト中。
+で、下記コードはかなり良い線行っている気がします。
LANG:ruby
require 'riot_js/rails/processors/compiler'
@@ -505,6 +466,8 @@
# Sprockets 2, 3 & 4 interface
class SprocketsExtensionBase
+ attr_reader :default_mime_type
+
def initialize(filename, &block)
@filename = filename
@source = block.call
@@ -530,23 +493,32 @@
context.metadata.merge(data: result)
end
end
-
+
private
- def self.register_self_helper(config, file_ext, mime_type_from, mime_type_to, charset=nil)
- config.assets.configure do |env|
- # Sprockets 3 and 4
- if env.respond_to?(:register_transformer)
- 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)
- # Sprockets 2 and 3
- if Sprockets::VERSION.start_with?("3")
- env.register_engine file_ext_from, self, { mime_type: mime_type, silence_deprecation: true }
- else
- env.register_engine file_ext_from, self, mime_type: mime_type
+ 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
@@ -558,8 +530,10 @@
::RiotJs::Rails::Compiler.compile(source)
end
- def self.register_self(env)
- register_self_helper(env, '.tag', 'text/riot-tag', 'application/javascript', :html)
+ 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
@@ -580,27 +554,34 @@
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
- Processor.register_self config
-
if defined?(::Haml)
require 'tilt/haml'
Haml::Template.options[:format] = :html5
- config.assets.configure do |env|
- # Sprockets 3 and 4
- if env.respond_to?(:register_transformer)
- 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)
- # Sprockets 2 and 3
- if Sprockets::VERSION.start_with?("3")
- env.register_engine '.haml', ::Tilt::HamlTemplate, { silence_deprecation: true }
- else
- env.register_engine '.haml', ::Tilt::HamlTemplate
+ 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
@@ -634,73 +615,101 @@
end
end
-テストコードは、
+** テスト結果 [#sbab7551]
- LANG:console
- rvm use 2.2.1
- rails _4.0.13_ new riot-test -B
- cd riot-test
- echo "\
- source 'https://rubygems.org'
- gem 'rails', '4.0.13'
- gem 'sqlite3'
- gem 'therubyracer', platforms: :ruby
- gem 'jquery-rails'
- gem 'haml-rails'
- gem 'riot_js-rails', path: '../album/gems/riot_js-rails'
- " >Gemfile
- bundle install
- rails g controller main index
- echo "\
- //= require jquery
- //= require jquery_ujs
- //= require riot
- //= require riot_rails
- //= require_tree .
- " >app/assets/javascripts/application.js
- mkdir app/assets/javascripts/riot-tags
- echo "\
- <test>
- <div><test> 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 '
- RiotTest::Application.routes.draw do
- root "main#index"
- end
- ' >config/routes.rb
- echo '
- <test data-riot></test>
- <haml-test data-riot></haml-test>
- ' >app/views/main/index.html.erb
- rake tmp:assets:clear
- rake assets:clean
- grep "sprockets (" Gemfile.lock
- rails s
-
+|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||
-これで http://localhost:3000/ へアクセスして、
+テストは下記のスクリプトで行った。
+通常の .tag ファイルが正しく読み込めるかどうかと、
+haml を使った .tag.haml ファイルが正しく読み込めるかどうかをテストしている。
+
+うまくいっていれば http://localhost:3000/ をブラウザで見た時に
<test> tag is working.
<haml-test> tag is working.
-と表示されればOK。
+と表示される。
- rvm use 2.2.1
- rails _4.0.13_ new riot-test -B
- cd riot-test
- echo "\
- source 'https://rubygems.org'
- gem 'rails', '4.0.13'
+** テストスクリプト [#zc561111]
-の 2.2.1 や 4.0.13 を変えながら、いろいろ試せばいい。
+Gist に上げた。
-|ruby version|rails version|sprockets version|result|
-|1.9.3|3.0.0|?|?|
-|2.2.1|4.0.13|3.7.0|ok|
-|2.3.1|5.0.0.1|3.7.0|ok|
+https://gist.github.com/osamutake/0ccbdf96a5ac70a36ebce38db0a33137
+
+** github に上げた [#x63554b5]
+
+上記コードを組み入れた riot_js-rails を github に上げました。
+
+https://github.com/osamutake/riot_js-rails
+
+** プルリクエストを書いてみた [#i044a1a0]
+
+作法をよく知らないので、失礼がないと良いのだけれど。
+
+https://github.com/bjarosze/riot_js-rails/pull/18
+
+本家に取り込まれるまでの間は Gemfile 中で、
+
+ LANG:ruby
+ gem 'riot_js-rails', :git => 'git://github.com/osamutake/riot_js-rails',
+ :branch => 'support_sprockets_2_3_and_4'
+
+とすれば上記コードを使えます。
+
+* さらに手直し [#afdb6fc3]
+
+テストを phantomjs で書いてみる。
+
+phantomjs の導入は、バイナリをとってくればすぐできる。
+
+http://phantomjs.org/download.html を見て、
+
+ LANG:console
+ $ wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
+ $ cd /usr/local/src
+ $ sudo tar fxj ~/phantomjs-2.1.1-linux-x86_64.tar.bz2
+ $ sudo ln -s /usr/local/src/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs
+ $ phantomjs -v
+ 2.1.1
+
+あ、npm でも良いみたいですね。こっちの方が簡単だ。
+
+ $ npm install phantomjs-prebuilt
+ $ ~/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs --version
+ 2.1.1
+
+あとは、
+
+test.js
+ LANG:js
+ var page = require('webpage').create();
+
+ page.onConsoleMessage = function(msg) { console.log(msg); };
+
+ page.open('http://localhost:3000/', function(status) {
+ if (status === "success") {
+ page.evaluate(function() {
+ console.log(document.body.innerText.trim());
+ });
+ phantom.exit(0);
+ } else {
+ phantom.exit(1);
+ }
+ });
+
+などというのを用意しておいて、
+
+ $ phantomjs test.js
+
+で実行できる。
+
+* 質問・コメント [#x0da6bd0]
+
+#article_kcaptcha