requirejs-rails 0.9.5 → 0.9.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- require 'requirejs/rails'
1
+ require "requirejs/rails"
@@ -1,5 +1,5 @@
1
1
  module Requirejs
2
2
  module Rails
3
- require 'requirejs/rails/engine'
3
+ require "requirejs/rails/engine"
4
4
  end
5
5
  end
@@ -1,32 +1,24 @@
1
- require 'requirejs/rails'
1
+ require "ostruct"
2
+ require "pathname"
2
3
 
3
- require 'pathname'
4
- require 'ostruct'
4
+ require "requirejs/rails"
5
5
 
6
- module Requirejs::Rails
7
- class Builder
8
- # config should be an instance of Requirejs::Rails::Config
9
-
10
- def initialize(config)
11
- @config = config
12
- end
13
-
14
- def build
15
- @config.tmp_dir
16
- end
6
+ module Requirejs
7
+ module Rails
8
+ class Builder
9
+ def initialize(config)
10
+ @config = config
11
+ end
17
12
 
18
- def digest_for(path)
19
- if !Rails.application.assets.file_digest(path).nil?
20
- Rails.application.assets.file_digest(path).hexdigest
21
- else
22
- raise Requirejs::BuildError, "Cannot compute digest for missing asset: #{path}"
13
+ def build
14
+ @config.tmp_dir
23
15
  end
24
- end
25
16
 
26
- def generate_rjs_driver
27
- templ = Erubis::Eruby.new(@config.driver_template_path.read)
28
- @config.driver_path.open('w') do |f|
29
- f.write(templ.result(@config.get_binding))
17
+ def generate_rjs_driver
18
+ templ = Erubis::Eruby.new(@config.driver_template_path.read)
19
+ @config.driver_path.open('w') do |f|
20
+ f.write(templ.result(@config.get_binding))
21
+ end
30
22
  end
31
23
  end
32
24
  end
@@ -1,35 +1,47 @@
1
- require 'requirejs/rails'
2
- require 'requirejs/error'
1
+ require "pathname"
3
2
 
4
- require 'active_support/ordered_options'
5
- require 'erubis'
6
- require 'pathname'
3
+ require "active_support/ordered_options"
4
+ require "erubis"
7
5
 
8
- module Requirejs::Rails
9
- class Config < ::ActiveSupport::OrderedOptions
10
- LOADERS = [ :requirejs, :almond ]
6
+ require "requirejs/error"
7
+ require "requirejs/rails"
11
8
 
12
- def initialize(application)
13
- super
14
- self.manifest = nil
9
+ module Requirejs
10
+ module Rails
11
+ class Config < ::ActiveSupport::OrderedOptions
12
+ LOADERS = [:requirejs, :almond]
15
13
 
16
- self.logical_asset_filter = [/\.js$/,/\.html$/,/\.txt$/]
17
- self.tmp_dir = application.root + 'tmp'
18
- self.bin_dir = Pathname.new(__FILE__+'/../../../../bin').cleanpath
14
+ BOWER_PATH_PATTERN = Regexp.new("\\A(.*)/(?:\\.bower|bower|component)\\.json\\z")
19
15
 
20
- self.source_dir = self.tmp_dir.join("requirejs/src")
21
- self.build_dir = self.tmp_dir.join("requirejs/dst")
22
- self.target_dir = application.root + 'public/assets'
23
- self.rjs_path = self.bin_dir+'r.js'
16
+ LOGICAL_PATH_PATTERNS = [
17
+ Regexp.new("\\.html\\z"),
18
+ Regexp.new("\\.js\\z"),
19
+ Regexp.new("\\.txt\\z"),
20
+ BOWER_PATH_PATTERN
21
+ ]
24
22
 
25
- self.loader = :requirejs
23
+ def initialize(application)
24
+ super
26
25
 
27
- self.driver_template_path = Pathname.new(__FILE__+'/../rjs_driver.js.erb').cleanpath
28
- self.driver_path = self.tmp_dir.join("requirejs/rjs_driver.js")
26
+ self.manifest = nil
27
+ self.logical_path_patterns = LOGICAL_PATH_PATTERNS
29
28
 
30
- self.user_config = {}
29
+ self.tmp_dir = application.root + 'tmp'
30
+ self.bin_dir = Pathname.new(__FILE__+'/../../../../bin').cleanpath
31
31
 
32
- self.run_config_whitelist = %w{
32
+ self.source_dir = self.tmp_dir.join("requirejs/src")
33
+ self.build_dir = self.tmp_dir.join("requirejs/dst")
34
+ self.target_dir = application.root + 'public/assets'
35
+ self.rjs_path = self.bin_dir+'r.js'
36
+
37
+ self.loader = :requirejs
38
+
39
+ self.driver_template_path = Pathname.new(__FILE__+'/../rjs_driver.js.erb').cleanpath
40
+ self.driver_path = self.tmp_dir.join("requirejs/rjs_driver.js")
41
+
42
+ self.user_config = {}
43
+
44
+ self.run_config_whitelist = %w{
33
45
  baseUrl
34
46
  callback
35
47
  catchError
@@ -49,7 +61,7 @@ module Requirejs::Rails
49
61
  xhtml
50
62
  }
51
63
 
52
- self.build_config_whitelist = %w{
64
+ self.build_config_whitelist = %w{
53
65
  appDir
54
66
  baseUrl
55
67
  closure
@@ -88,69 +100,70 @@ module Requirejs::Rails
88
100
  useStrict
89
101
  wrap
90
102
  }
91
- end
103
+ end
92
104
 
93
- def loader=(sym)
94
- unless LOADERS.include?(sym)
95
- raise Requirejs::ConfigError, "Attempt to set unknown loader: #{sym}"
105
+ def loader=(sym)
106
+ unless LOADERS.include?(sym)
107
+ raise Requirejs::ConfigError, "Attempt to set unknown loader: #{sym}"
108
+ end
109
+ self[:loader] = sym
96
110
  end
97
- self[:loader] = sym
98
- end
99
111
 
100
- def build_config
101
- unless self.has_key?(:build_config)
102
- self[:build_config] = self.run_config.merge "baseUrl" => source_dir.to_s,
103
- "modules" => [ { 'name' => 'application' } ]
104
- self[:build_config].merge!(self.user_config).slice!(*self.build_config_whitelist)
105
- case self.loader
106
- when :requirejs
107
- # nothing to do
108
- when :almond
109
- mods = self[:build_config]['modules']
110
- unless mods.length == 1
111
- raise Requirejs::ConfigError, "Almond build requires exactly one module, config has #{mods.length}."
112
+ def build_config
113
+ unless self.has_key?(:build_config)
114
+ self[:build_config] = self.run_config.merge "baseUrl" => source_dir.to_s,
115
+ "modules" => [{'name' => 'application'}]
116
+ self[:build_config].merge!(self.user_config).slice!(*self.build_config_whitelist)
117
+ case self.loader
118
+ when :requirejs
119
+ # nothing to do
120
+ when :almond
121
+ mods = self[:build_config]['modules']
122
+ unless mods.length == 1
123
+ raise Requirejs::ConfigError, "Almond build requires exactly one module, config has #{mods.length}."
124
+ end
125
+ mod = mods[0]
126
+ name = mod['name']
127
+ mod['name'] = 'almond'
128
+ mod['include'] = name
112
129
  end
113
- mod = mods[0]
114
- name = mod['name']
115
- mod['name'] = 'almond'
116
- mod['include'] = name
117
130
  end
131
+ self[:build_config]
118
132
  end
119
- self[:build_config]
120
- end
121
133
 
122
- def run_config
123
- unless self.has_key?(:run_config)
124
- self[:run_config] = { "baseUrl" => "/assets" }
125
- self[:run_config].merge!(self.user_config).slice!(*self.run_config_whitelist)
134
+ def run_config
135
+ unless self.has_key?(:run_config)
136
+ self[:run_config] = {"baseUrl" => "/assets"}
137
+ self[:run_config].merge!(self.user_config).slice!(*self.run_config_whitelist)
138
+ end
139
+ self[:run_config]
126
140
  end
127
- self[:run_config]
128
- end
129
141
 
130
- def user_config=(cfg)
131
- if url = cfg.delete('baseUrl')
132
- raise Requirejs::ConfigError, "baseUrl is not needed or permitted in the configuration"
142
+ def user_config=(cfg)
143
+ if url = cfg.delete('baseUrl')
144
+ raise Requirejs::ConfigError, "baseUrl is not needed or permitted in the configuration"
145
+ end
146
+ self[:user_config] = cfg
133
147
  end
134
- self[:user_config] = cfg
135
- end
136
148
 
137
- def module_name_for(mod)
138
- case self.loader
139
- when :almond
140
- return mod['include']
141
- when :requirejs
142
- return mod['name']
149
+ def module_name_for(mod)
150
+ case self.loader
151
+ when :almond
152
+ return mod['include']
153
+ when :requirejs
154
+ return mod['name']
155
+ end
143
156
  end
144
- end
145
157
 
146
- def get_binding
147
- return binding()
148
- end
158
+ def get_binding
159
+ return binding()
160
+ end
149
161
 
150
- def asset_allowed?(asset)
151
- self.logical_asset_filter.reduce(false) do |accum, matcher|
152
- accum || (matcher =~ asset)
153
- end ? true : false
162
+ def asset_allowed?(logical_path)
163
+ logical_path_patterns.reduce(false) do |accum, pattern|
164
+ accum || !!(pattern.match(logical_path))
165
+ end
166
+ end
154
167
  end
155
168
  end
156
169
  end
@@ -1,6 +1,6 @@
1
- require 'requirejs/rails/config'
1
+ require "pathname"
2
2
 
3
- require 'pathname'
3
+ require "requirejs/rails/config"
4
4
 
5
5
  module Requirejs
6
6
  module Rails
@@ -54,7 +54,7 @@ module Requirejs
54
54
  config.after_initialize do |app|
55
55
  config = app.config
56
56
  rails_manifest_path = File.join(app.root, 'public', config.assets.prefix)
57
- rails_manifest = Sprockets::Manifest.new(app.assets, rails_manifest_path)
57
+ rails_manifest = ::Sprockets::Manifest.new(app.assets, rails_manifest_path)
58
58
  if config.requirejs.manifest_path.exist? && rails_manifest
59
59
  rjs_digests = YAML.load(ERB.new(File.new(config.requirejs.manifest_path).read).result)
60
60
  rails_manifest.assets.merge!(rjs_digests)
@@ -1,6 +1,6 @@
1
1
  module Requirejs
2
2
  module Rails
3
- Version = "0.9.5"
4
- LibVersion = "2.1.15"
3
+ Version = "0.9.6"
4
+ LibVersion = "2.1.17"
5
5
  end
6
6
  end
@@ -0,0 +1,9 @@
1
+ module Requirejs
2
+ module Rails
3
+ class ViewProxy
4
+ include ActionView::Context
5
+ include ActionView::Helpers::AssetUrlHelper
6
+ include ActionView::Helpers::TagHelper
7
+ end
8
+ end
9
+ end
@@ -1,13 +1,12 @@
1
- require 'requirejs/rails/builder'
2
- require 'requirejs/rails/config'
1
+ require "fileutils"
2
+ require "pathname"
3
+ require "tempfile"
3
4
 
4
- require 'fileutils'
5
- require 'pathname'
5
+ require "active_support/ordered_options"
6
+ require "sprockets"
6
7
 
7
- require 'sprockets'
8
- require 'tempfile'
9
-
10
- require 'active_support/ordered_options'
8
+ require "requirejs/rails/builder"
9
+ require "requirejs/rails/config"
11
10
 
12
11
  namespace :requirejs do
13
12
  # This method was backported from an earlier version of Sprockets.
@@ -40,7 +39,7 @@ namespace :requirejs do
40
39
  end
41
40
 
42
41
  task setup: ["assets:environment"] do
43
- unless defined?(Sprockets)
42
+ unless defined?(::Sprockets)
44
43
  warn "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true"
45
44
  exit
46
45
  end
@@ -89,9 +88,6 @@ OS X Homebrew users can use 'brew install node'.
89
88
  # Copy all assets to the temporary staging directory.
90
89
  task prepare_source: ["requirejs:setup",
91
90
  "requirejs:clean"] do
92
- bower_json_pattern = Regexp.new("\\A(.*)/bower\\.json\\z")
93
- js_ext = requirejs.env.extension_for_mime_type("application/javascript")
94
-
95
91
  requirejs.config.source_dir.mkpath
96
92
 
97
93
  # Save the original JS compressor and cache, which will be restored later.
@@ -102,20 +98,31 @@ OS X Homebrew users can use 'brew install node'.
102
98
  original_cache = requirejs.env.cache
103
99
  requirejs.env.cache = nil
104
100
 
105
- requirejs.env.each_logical_path do |logical_path|
106
- m = bower_json_pattern.match(logical_path)
107
- bower_logical_path = m && "#{m[1]}#{js_ext}"
101
+ js_ext = requirejs.env.mime_types["application/javascript"][:extensions].first
108
102
 
103
+ requirejs.env.logical_paths do |logical_path, physical_path|
109
104
  next \
110
- if !(requirejs.config.asset_allowed?(logical_path) || bower_logical_path)
111
-
112
- asset = requirejs.env.find_asset(logical_path)
113
-
114
- if asset
115
- # If a `bower.json` was found, then substitute the logical path with the parsed module name.
116
- filename = requirejs.config.source_dir.join(bower_logical_path || asset.logical_path)
117
- filename.dirname.mkpath
118
- asset.write_to(filename)
105
+ if !requirejs.config.asset_allowed?(logical_path)
106
+
107
+ m = ::Requirejs::Rails::Config::BOWER_PATH_PATTERN.match(logical_path)
108
+
109
+ if !m
110
+ asset = requirejs.env.find_asset(logical_path)
111
+
112
+ if asset
113
+ file = requirejs.config.source_dir.join(asset.logical_path)
114
+ file.dirname.mkpath
115
+ asset.write_to(file)
116
+ end
117
+ else
118
+ bower_logical_path = Pathname.new(logical_path).dirname.sub_ext(js_ext).to_s
119
+ asset = requirejs.env.find_asset(bower_logical_path)
120
+
121
+ if asset
122
+ file = requirejs.config.source_dir.join(bower_logical_path)
123
+ file.dirname.mkpath
124
+ asset.write_to(file)
125
+ end
119
126
  end
120
127
  end
121
128
 
@@ -145,8 +152,10 @@ OS X Homebrew users can use 'brew install node'.
145
152
  task digestify_and_compress: ["requirejs:setup"] do
146
153
  requirejs.config.build_config['modules'].each do |m|
147
154
  asset_name = "#{requirejs.config.module_name_for(m)}.js"
155
+ asset = requirejs.env.find_asset(asset_name)
156
+
148
157
  built_asset_path = requirejs.config.build_dir.join(asset_name)
149
- digest_name = asset_name.sub(/\.(\w+)$/) { |ext| "-#{requirejs.builder.digest_for(built_asset_path)}#{ext}" }
158
+ digest_name = asset.digest_path
150
159
  digest_asset_path = requirejs.config.target_dir + digest_name
151
160
 
152
161
  # Ensure that the parent directory `a/b` for modules with names like `a/b/c` exist.
@@ -17,7 +17,7 @@ class RequirejsRailsTest < ActiveSupport::TestCase
17
17
  end
18
18
 
19
19
  test "CHANGELOG up to date" do
20
- changelog_match = (/^# v#{Requirejs::Rails::Version}/ =~ Pathname.new(__FILE__+'/../../CHANGELOG.md').cleanpath.read)
20
+ changelog_match = (/^### v#{Requirejs::Rails::Version}/ =~ Pathname.new(__FILE__+'/../../CHANGELOG.md').cleanpath.read)
21
21
  assert changelog_match, "CHANGELOG has no section for v#{Requirejs::Rails::Version}"
22
22
  end
23
23
  end
@@ -41,7 +41,7 @@ class RequirejsRailsConfigTest < ActiveSupport::TestCase
41
41
  test "matches configured logical assets" do
42
42
  assert_equal true, @cfg.asset_allowed?('foo.js')
43
43
  assert_equal false, @cfg.asset_allowed?('bar.frobnitz')
44
- @cfg.logical_asset_filter += [/\.frobnitz$/]
44
+ @cfg.logical_path_patterns += [/\.frobnitz$/]
45
45
  assert_equal true, @cfg.asset_allowed?('bar.frobnitz')
46
46
  end
47
47
 
@@ -136,9 +136,13 @@ class RequirejsHelperTest < ActionView::TestCase
136
136
  end
137
137
 
138
138
  test "requirejs_include_tag_with_block" do
139
- render text: wrap(requirejs_include_tag("application") do
140
- {"class" => controller.class.name.demodulize}
141
- end)
139
+ result = wrap(
140
+ requirejs_include_tag("application") do
141
+ {"class" => controller.class.name.demodulize}
142
+ end
143
+ )
144
+
145
+ render text: result
142
146
 
143
147
  assert_select "script:first-of-type[src=\"/javascripts/require.js\"]" \
144
148
  "[data-class=\"TestController\"]", count: 1
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @license almond 0.3.0 Copyright (c) 2011-2014, The Dojo Foundation All Rights Reserved.
2
+ * @license almond 0.3.1 Copyright (c) 2011-2014, The Dojo Foundation All Rights Reserved.
3
3
  * Available via the MIT or new BSD license.
4
4
  * see: http://github.com/jrburke/almond for details
5
5
  */
@@ -44,12 +44,6 @@ var requirejs, require, define;
44
44
  //otherwise, assume it is a top-level require that will
45
45
  //be relative to baseUrl in the end.
46
46
  if (baseName) {
47
- //Convert baseName to array, and lop off the last part,
48
- //so that . matches that "directory" and not name of the baseName's
49
- //module. For instance, baseName of "one/two/three", maps to
50
- //"one/two/three.js", but we want the directory, "one/two" for
51
- //this normalization.
52
- baseParts = baseParts.slice(0, baseParts.length - 1);
53
47
  name = name.split('/');
54
48
  lastIndex = name.length - 1;
55
49
 
@@ -58,7 +52,11 @@ var requirejs, require, define;
58
52
  name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, '');
59
53
  }
60
54
 
61
- name = baseParts.concat(name);
55
+ //Lop off the last part of baseParts, so that . matches the
56
+ //"directory" and not name of the baseName's module. For instance,
57
+ //baseName of "one/two/three", maps to "one/two/three.js", but we
58
+ //want the directory, "one/two" for this normalization.
59
+ name = baseParts.slice(0, baseParts.length - 1).concat(name);
62
60
 
63
61
  //start trimDots
64
62
  for (i = 0; i < name.length; i += 1) {
@@ -408,6 +406,9 @@ var requirejs, require, define;
408
406
  requirejs._defined = defined;
409
407
 
410
408
  define = function (name, deps, callback) {
409
+ if (typeof name !== 'string') {
410
+ throw new Error('See almond README: incorrect module build, no module name');
411
+ }
411
412
 
412
413
  //This module may not have dependencies
413
414
  if (!deps.splice) {