opal / opal-haml

Opal + Haml = <3
MIT License
23 stars 5 forks source link

Require does not work on *.haml files on Windows #7

Open magynhard opened 8 years ago

magynhard commented 8 years ago

Hi there,

I'm playing with ruby at the moment and decided to do with opal.

With *.opalerb templates everything works fine. But I wanted to use haml templates, but it does not work.

Seems that what u call to "add *.haml" to sprockets does not work. Maybe a Windows related problem?

Here is my sample app:

# app/application.rb
require 'opal'
require 'jquery'
require 'opal-jquery'

# require views/example.haml
require 'views/example'

# require views/example2.opalerb - working fine!
require 'views/example2'

Document.ready? do
  template = Template['views/example']
  puts template.render(Example.new)
end

I added opal-haml to the Gemfile and did bundle install. I tried the gem from rubygems as well as the github version. The behaviour is the same.

This is my rakefile

# Rakefile
require 'opal'
require 'opal-jquery'
require 'sass'

desc "Build our app - transpiling rb to js and scss to css and copying some files ..."
task :build do

  Opal.append_path "app"
  Opal.append_path "app/views"
  Opal.append_path "app/class"

  #
  # FRONTEND JS
  #

  js_container = ""

  # RUBY -> JS
  js_container << Opal::Builder.build("application").to_s

  File.binwrite "public/lib/app.js", js_container

  #
  # FRONTEND CSS
  #

  css_container = ""

  # SASS/SCSS -> CSS
  sass_file = File.read "app/assets/stylesheets/application.scss"
  sass_engine = Sass::Engine.new sass_file, { :load_paths => [ "app/assets/stylesheets" ], :syntax => :scss }
  css_container << sass_engine.render {}

  # FONT AWESOME
  fa_file = File.read "app/lib/font-awesome-4.5.0/css/font-awesome.min.css"
  css_container << fa_file
  FileUtils::cp_r "app/lib/font-awesome-4.5.0/fonts/.","public/fonts"

  File.binwrite "public/lib/app.css", css_container

  puts "... finished"
end

When i execute rake build i get the following output:

** Invoke build (first_time)
** Execute build
rake aborted!
Opal::Builder::MissingRequire: A file required by "application" wasn't found.
can't find file: "views/example" in ["C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/opal", "C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/stdlib",
"C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib", "C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-jquery-0.4.1/lib", "D:/project/example/app", "D:/project/example/app/views", "D:/project/example/app/class"]
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:118:in `read'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:127:in `process_require'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:42:in `block in build_str'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:42:in `map'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:42:in `build_str'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:35:in `build'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:30:in `build'
D:/project/example/rakefile.rb:25:in `block in <top (required)>'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:240:in `call'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:235:in `each'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
C:/Ruby22-x64/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:106:in `each'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:75:in `run'
C:/Ruby22-x64/bin/rake:33:in `<main>'
Opal::Builder::MissingRequire: can't find file: "views/example" in ["C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/opal", "C:/Ruby22-x64/lib/ruby/gems/
2.2.0/gems/opal-0.9.0/stdlib", "C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib", "C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-jquery-0.4.1/lib", "
D:/project/example/app", "D:/project/example/app/views", "D:/FIRMA_MGH/PROJEKTE/_INTERN/_webg
ames/rubymaze/app/class"]
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:118:in `read'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:127:in `process_require'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:42:in `block in build_str'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:42:in `map'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:42:in `build_str'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:35:in `build'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:30:in `build'
D:/project/example/rakefile.rb:25:in `block in <top (required)>'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:240:in `call'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:235:in `each'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
C:/Ruby22-x64/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:106:in `each'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:75:in `run'
C:/Ruby22-x64/bin/rake:33:in `<main>'
Tasks: TOP => build

Ruby x64 @ Win7 x64

Any idea?

elia commented 8 years ago

Yes, here's the catch: opal-haml only works with the sprockets-based stack:

https://github.com/opal/opal/blob/master/docs/using_sprockets.md#opal--sprockets

Something more or less like the following:

require 'sprockets'
environment = Sprockets::Environment.new

require 'opal'
require 'opal-haml'

Opal.append_path "app"
Opal.append_path "app/views"
Opal.append_path "app/class"

Opal.paths.each do |path|
  environment.append_path path
end

File.binwrite "public/lib/app.js", environment['application.js'].to_s
magynhard commented 8 years ago

I unfortunately don't get it work.

So i just created a repo for this here: https://github.com/entwanderer/opal-haml-test

There I'm currently using the .opalerb-template and it builds fine with rake build

By renaming "example" to "exmaple2" (on require and @ Template[...]) in app/application.rb it should use the example2.haml, but I still get the same error as posted before when executing rake build

Do you have an idea?