ruby-requirejs 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +25 -0
  3. data/Gemfile +6 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +85 -0
  6. data/Rakefile +1 -0
  7. data/lib/requirejs/action_view/tag_helper.rb +15 -0
  8. data/lib/requirejs/builds/build.js.erb +8 -0
  9. data/lib/requirejs/builds/build.rb +18 -0
  10. data/lib/requirejs/builds/build_config.rb +59 -0
  11. data/lib/requirejs/builds/optimized_build.rb +57 -0
  12. data/lib/requirejs/compiler.rb +21 -0
  13. data/lib/requirejs/config.rb +72 -0
  14. data/lib/requirejs/engine.rb +40 -0
  15. data/lib/requirejs/manifest.rb +56 -0
  16. data/lib/requirejs/runtime/node_runner.js +21 -0
  17. data/lib/requirejs/runtime/r.js +27617 -0
  18. data/lib/requirejs/runtime/runtime.rb +59 -0
  19. data/lib/requirejs/tilt/directive_processor.rb +52 -0
  20. data/lib/requirejs/tilt/template.rb +37 -0
  21. data/lib/requirejs/version.rb +5 -0
  22. data/lib/ruby-requirejs.rb +26 -0
  23. data/ruby-requirejs.gemspec +24 -0
  24. data/spec/cases/basic/in/app.js +10 -0
  25. data/spec/cases/basic/in/application.js +7 -0
  26. data/spec/cases/basic/out/application.js +6 -0
  27. data/spec/cases/basic_almond/in/app.js +10 -0
  28. data/spec/cases/basic_almond/in/application.js +7 -0
  29. data/spec/cases/basic_almond/out/application.js +447 -0
  30. data/spec/cases/basic_digested/in/app.js +10 -0
  31. data/spec/cases/basic_digested/in/application.js +7 -0
  32. data/spec/cases/basic_digested/out/application.js +14 -0
  33. data/spec/cases/basic_minimized_almond/in/app.js +10 -0
  34. data/spec/cases/basic_minimized_almond/in/application.js +7 -0
  35. data/spec/cases/basic_minimized_almond/out/application.js +7 -0
  36. data/spec/cases/basic_optimized/in/app.js +10 -0
  37. data/spec/cases/basic_optimized/in/application.js +7 -0
  38. data/spec/cases/basic_optimized/out/application.js +21 -0
  39. data/spec/cases/simple/in/app.js +10 -0
  40. data/spec/cases/simple/in/application.js +35 -0
  41. data/spec/rails_spec.rb +80 -0
  42. data/spec/spec_helper.rb +22 -0
  43. data/spec/support/helpers.rb +40 -0
  44. data/vendor/assets/javascripts/almond.js +422 -0
  45. data/vendor/assets/javascripts/require.js +2072 -0
  46. metadata +136 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 58f4f4f4aba74d633bcdc94f249b0df14fef27a5
4
+ data.tar.gz: e90156c95ecbab9d56ccf7706aeeb108dc1bd687
5
+ SHA512:
6
+ metadata.gz: b86323d78c0657d60b0df4382e102dff8375424d3db85ef167efe621a13b08fb527ff4103091655b1078e8d1740e59c696f3f5ec3fa1487db2db9835fe602939
7
+ data.tar.gz: 3e7b818d2c1caf66c1837ef2e183cc129fad30c73074a3fe545b650c5efbcc29fc7217f5adcc847756e99c4a7e49e818ea3e6e2b5fce46c4ae61b50886b2d749
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
19
+ Guardfile
20
+ atlassian-ide-plugin.xml
21
+ .rspec
22
+ spec/database.yml
23
+ .ruby-gemset
24
+ .ruby-version
25
+ log
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby-requirejs.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec', '~> 2.14.0'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alex Krasinsky
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Ruby Requirejs
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'ruby-requirejs'
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install ruby-requirejs
16
+
17
+
18
+ ## Configuration
19
+
20
+ ### With Rails and the Asset Pipeline.
21
+
22
+ #### Development
23
+ You have several options. For development default configuration should be enough for you.
24
+ By default requirejs will not perform any optimization or compression.
25
+
26
+ #### Production
27
+ For production setup you would probably want to use [almond](https://github.com/jrburke/almond) as an alternative loader.
28
+ To enable `almond` you need to configure your `config/environments/{environment}.rb` or `config/environment.rb` if you want to use `almond` all the time (I would not recommend it)
29
+
30
+ config.requirejs.loader = :almond
31
+
32
+ `ruby-requirejs` currently supports js_compression only with `uglifier`. To enable:
33
+
34
+ config.requirejs.js_compressor = :uglifier
35
+
36
+ If you don't want to use `almond` remove `config.requirejs.loader = :almond` and instead add `config.requirejs.optimize = true`
37
+
38
+ ### Standalone Sprockets usage
39
+
40
+ #### TODO: Coming soon
41
+
42
+ ## Usage
43
+
44
+ In your `application.js` add directive `rjs` so `ruby-requirejs` will know that this file should be processed as an entry point
45
+ Example of `application.js` can look like this:
46
+
47
+ //= rjs
48
+
49
+ require.config({
50
+ shim:{
51
+ "jquery":{
52
+ exports:"$"
53
+ },
54
+ "turbolinks":{
55
+ exports:"Turbolinks"
56
+ },
57
+ "jquery_ujs":["jquery"],
58
+ "app":{
59
+ deps:[
60
+ "jquery",
61
+ "jquery_ujs",
62
+ "turbolinks"
63
+ ]
64
+ }
65
+ },
66
+
67
+ waitSeconds: 10,
68
+ catchError:false
69
+ });
70
+
71
+ require(["jquery", "turbolinks", "jquery_ujs", "app"], function ($, Turbolinks, uJS, App) {
72
+ App.initialize();
73
+ });
74
+
75
+ During `r.js` optimization `ruby-requirejs` will automatically add fingerprints to assets if this is set in configuration.
76
+
77
+ ## Contributing
78
+
79
+ Want to add some examples? Document code? Implement new feature? Fix bug?
80
+
81
+ 1. Fork it ( http://github.com/<my-github-username>/ruby-requirejs/fork )
82
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
83
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
84
+ 4. Push to the branch (`git push origin my-new-feature`)
85
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,15 @@
1
+ module Requirejs
2
+ module ActionView
3
+ module TagHelper
4
+ def requirejs_include_tag(*sources)
5
+ if Requirejs.config.almond?
6
+ javascript_include_tag(*sources)
7
+ else
8
+ sources.uniq.map do |source|
9
+ javascript_include_tag('require', data: { main: path_to_javascript(source).gsub!(/(^.*)\.js$/, '\1') })
10
+ end.join("\n").html_safe
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ //Load the requirejs optimizer
2
+ var requirejs = require("<%= Requirejs.config.runtime_location %>");
3
+
4
+ //Define optimize function so it can be called from within ExecJS
5
+ var optimize = function () {
6
+ requirejs.optimize(<%= config.as_json %>);
7
+ };
8
+
@@ -0,0 +1,18 @@
1
+ module Requirejs
2
+ class Build
3
+ def initialize(scope, file, original_data)
4
+ @scope, @file = scope, file
5
+ @original_data = original_data
6
+ end
7
+
8
+ def data
9
+ <<-JS
10
+ require.config({
11
+ paths: #{ Manifest.new(@scope, @file).paths_as_json }
12
+ });
13
+ #{@original_data}
14
+ JS
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,59 @@
1
+ module Requirejs
2
+ # Class handles storing and retrieving build config for each rjs-manifest file
3
+ class BuildConfig
4
+
5
+ def initialize(file)
6
+ @file = file
7
+ end
8
+
9
+ def data
10
+ @data ||= begin
11
+ data = {
12
+ wrap: true,
13
+ baseUrl: Requirejs.config.cache_assets_location,
14
+ optimize: Requirejs.config.js_compressor,
15
+ out: File.join(Requirejs.config.cache_builds_location, basename)
16
+ }.merge(config_from_file)
17
+ data[:name] = 'almond' if Requirejs.config.almond?
18
+ data
19
+ end
20
+ end
21
+
22
+ def as_json
23
+ JSON.dump(data)
24
+ end
25
+
26
+ # Dumps hash with build config to yaml file
27
+ def save(hash)
28
+ File.open(file_path, 'w') do |f|
29
+ f.write(YAML.dump(hash))
30
+ end
31
+ end
32
+
33
+ def exists?
34
+ File.exists?(file_path)
35
+ end
36
+
37
+ private
38
+
39
+ def file_path
40
+ File.join(Requirejs.config.cache_build_scripts_location, "#{name}.yaml")
41
+ end
42
+
43
+ # Read config from file
44
+ def config_from_file
45
+ YAML.load(File.read(file_path))
46
+ end
47
+
48
+ # The basename of the template file.
49
+ def basename(suffix='')
50
+ File.basename(@file, suffix)
51
+ end
52
+
53
+ # The template file's basename with all extensions chomped off.
54
+ def name
55
+ basename.split('.', 2).first if basename
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,57 @@
1
+ module Requirejs
2
+ class OptimizedBuild
3
+ def initialize(scope, file, original_data)
4
+ @scope, @file = scope, file
5
+ @original_data = original_data
6
+ end
7
+
8
+ def prepare
9
+ store_data_to_asset
10
+ copy_assets
11
+ end
12
+
13
+ def data
14
+ prepare
15
+ ::Requirejs::Runtime.new(build_script).exec
16
+ File.read(config.data[:out])
17
+ end
18
+
19
+ private
20
+
21
+ def build_script
22
+ build_script_path = File.join(Requirejs.config.gem_root_path, 'lib', 'requirejs', 'builds', 'build.js.erb')
23
+ ERB.new(File.read(build_script_path)).result(binding)
24
+ end
25
+
26
+ # Store data in assets cache dir so it will be used in compilation
27
+ def store_data_to_asset
28
+ File.open(File.join(Requirejs.config.cache_assets_location, File.basename(@file)), 'w') do |f|
29
+ f.write @original_data
30
+ end
31
+ end
32
+
33
+ # Store assets in assets cache dir so we can use them during compilation
34
+ def copy_assets
35
+ @scope.environment.each_logical_path do |logical_path|
36
+ copy_asset(logical_path) if copy_asset?(logical_path)
37
+ end
38
+ end
39
+
40
+ def copy_asset(logical_path)
41
+ @scope.environment.find_asset(logical_path).write_to(asset_path(logical_path))
42
+ end
43
+
44
+ def asset_path(logical_path)
45
+ File.join(Requirejs.config.cache_assets_location, logical_path).to_s
46
+ end
47
+
48
+ def copy_asset?(logical_path)
49
+ !File.exists?(asset_path(logical_path)) && (logical_path =~/.*\.js$/) && (File.basename(logical_path) != File.basename(@file))
50
+ end
51
+
52
+ def config
53
+ @config ||= ::Requirejs::BuildConfig.new(@file)
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,21 @@
1
+ module Requirejs
2
+ class Compiler
3
+ attr_accessor :scope, :data, :file, :build_script_path
4
+
5
+ def initialize(options ={})
6
+ Requirejs.config.ensure_cache_location_exists
7
+ @scope, @data, @file = options[:scope], options[:data], options[:file]
8
+ end
9
+
10
+ def exec
11
+ if Requirejs.config.optimize?
12
+ OptimizedBuild.new(@scope, @file, @data).data
13
+ elsif Requirejs.config.digest?
14
+ Build.new(@scope, @file, @data).data
15
+ else
16
+ @data
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,72 @@
1
+ module Requirejs
2
+ class Config < ::ActiveSupport::OrderedOptions
3
+ def initialize
4
+ super
5
+
6
+ self.gem_root_path = Gem::Specification.find_by_name('ruby-requirejs').gem_dir
7
+ self.runtime_location = File.join(gem_root_path, 'lib', 'requirejs', 'runtime', 'r.js')
8
+ self.cache_location = nil
9
+ self.js_compressor = :none # :uglify
10
+
11
+ self.loader = :requirejs # :almond
12
+ self.optimize = false # :almond
13
+ self.digest = false # :almond
14
+ end
15
+
16
+ def cache_assets_location
17
+ check_cache_location
18
+ @cache_assets_location ||= File.join(cache_location, 'assets')
19
+ end
20
+
21
+ def cache_build_scripts_location
22
+ check_cache_location
23
+ @cache_build_scripts_location ||= File.join(cache_location, 'build_scripts')
24
+ end
25
+
26
+ def cache_builds_location
27
+ check_cache_location
28
+ @cache_builds_location ||= File.join(cache_location, 'builds')
29
+ end
30
+
31
+ def check_cache_location
32
+ raise 'cache location is not set. Set cache_location. Ex.: Require.config.cache_location = File.dirname(__FILE__)' if cache_location.blank?
33
+ end
34
+
35
+ def setup_directories
36
+ cleanup_cache_dir
37
+ ensure_cache_location_exists
38
+ end
39
+
40
+ def ensure_cache_location_exists
41
+ [cache_location, cache_assets_location, cache_build_scripts_location, cache_builds_location].each do |path|
42
+ Pathname.new(path).mkpath
43
+ end
44
+ end
45
+
46
+ def cleanup_cache_dir
47
+ FileUtils.remove_entry_secure(self.cache_location) rescue nil
48
+ end
49
+
50
+ def almond?
51
+ self.loader == :almond
52
+ end
53
+
54
+ def optimize?
55
+ almond? || self.optimize
56
+ end
57
+
58
+ def digest?
59
+ self.digest
60
+ end
61
+
62
+ end
63
+
64
+ # Config accessors
65
+ def self.config=(config)
66
+ @config = config
67
+ end
68
+
69
+ def self.config
70
+ @config
71
+ end
72
+ end
@@ -0,0 +1,40 @@
1
+ module Requirejs
2
+ class Engine < ::Rails::Engine
3
+
4
+ config.before_configuration do
5
+ config.requirejs = Requirejs.config = Requirejs::Config.new
6
+ Requirejs.config.cache_location = ::Rails.root.join('tmp', 'ruby-requirejs')
7
+ end
8
+
9
+ config.assets.configure do |env|
10
+ # Switching to rjs javascript compressor
11
+ if env.js_compressor && env.js_compressor.name == 'Sprockets::UglifierCompressor'
12
+ config.requirejs.js_compressor = :uglify
13
+ else
14
+ :none
15
+ end
16
+
17
+ # Disable default assets pipeline compression
18
+ env.js_compressor = nil
19
+
20
+ config.requirejs.digest = config.assets.digest
21
+
22
+ # Changing Processors
23
+ require 'requirejs/tilt/template'
24
+ require 'requirejs/tilt/directive_processor'
25
+ env.unregister_processor('application/javascript', Sprockets::DirectiveProcessor)
26
+ env.register_preprocessor('application/javascript', Requirejs::Tilt::DirectiveProcessor)
27
+
28
+ if Requirejs.config.optimize?
29
+ config.assets.precompile << 'require.js' unless Requirejs.config.almond?
30
+ else
31
+ # Ready to precompile everything
32
+ config.assets.precompile += env.paths.map { |path| Dir.glob(File.join(path, '**', '*.{js,coffee}')) }.flatten.uniq
33
+ config.assets.uniq!
34
+ end
35
+
36
+ env.register_bundle_processor('application/javascript', Requirejs::Tilt::Template)
37
+ end
38
+
39
+ end
40
+ end