履歴: プログラミング/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/11 23:52:46)
@@ -318,7 +318,7 @@
end
def self.run(filename, source, context)
- /* return the modified source */
+ # return the modified source
end
def self.call(input)
@@ -333,13 +333,15 @@
を継承して run を override すれば Extention を作れる。
- require 'sprockets/processing'
- extend Sprockets::Processing
-
- register_preprocessor 'text/css', TheSprocketsExtension
+ LANG:ruby
+ require 'sprockets/processing'
+ extend Sprockets::Processing
+
+ register_preprocessor 'text/css', TheSprocketsExtension
で拡張機能を登録して、
+ 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
@@ -367,11 +369,338 @@
だから rails 5 で file.tag.haml が動かなかったのか?~
いや、rails 5.0.0.1 でも sprockets は 3.7 だから、そういう問題ではないか。
- if Sprockets::VERSION.start_with?("5")
+ 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
+ LANG:ruby
+ 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
+
+~
+
+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)
+ 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
+
+ private
+
+ def process
+ raise 'Not implemented'
+ end
+
+ def prepare(input)
+ @context = input[:environment].context_class.new(input)
+ @data = input[:data]
+ end
+
+ end
+ end
+ end
+
+~
+
+riot_js-rails/lib/riot_js/rails/processors/processor.rb
+ LANG:ruby
+ 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
+
+いろいろ面倒な手順は踏んでいるけれど、やろうとしていることは同じように見える?
+
+** やってみる [#la58b64f]
+
+あー、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
+ 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(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
+ end
+ end
+ end
+ end
+
+ end
+
+ class Processor < SprocketsExtensionBase
+
+ def self.run(filename, source, context)
+ ::RiotJs::Rails::Compiler.compile(source)
+ end
+
+ def self.register_self(env)
+ register_self_helper(env, '.tag', 'text/riot-tag', 'application/javascript', :html)
+ end
+
+ end
+ end
+ 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|
+
+ 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
+ end
+ end
+ 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
+
+テストコードは、
+
+ 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
+
+
+これで 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'
+
+の 2.2.1 や 4.0.13 を変えながら、いろいろ試せばいい。
+
+|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|