requirejs-rails 0.5.6 → 0.6.1
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.
- data/CHANGELOG.md +14 -0
- data/Gemfile.lock +1 -1
- data/README.md +66 -17
- data/app/helpers/requirejs_helper.rb +38 -7
- data/lib/requirejs/rails/config.rb +63 -6
- data/lib/requirejs/rails/version.rb +1 -1
- data/test/requirejs-rails_test.rb +10 -0
- metadata +11 -11
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
# v0.6.1
|
2
|
+
|
3
|
+
- Fix regression in production env when `paths` specified in requirejs.yml.
|
4
|
+
|
5
|
+
# v0.6.0
|
6
|
+
|
7
|
+
**NOTE:** Upgrade to 0.6.1! This was yanked due to a regression.
|
8
|
+
|
9
|
+
- We now generate a paths config to hit digested assets when needed (in
|
10
|
+
`production` or when `config.assets.digest` is true). Fixes #20.
|
11
|
+
- Support for generating additional data attributes on the require.js script
|
12
|
+
tag via `requirejs_include_tag`. See [README](README.md) for details. Closes
|
13
|
+
pull request #32; thanks to @hollow for the submission!
|
14
|
+
|
1
15
|
# v0.5.6
|
2
16
|
|
3
17
|
- Upgrade to RequireJS and r.js 1.0.7
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -65,14 +65,22 @@ Integrates [RequireJS](http://requirejs.org/) into the Rails 3 Asset Pipeline.
|
|
65
65
|
|
66
66
|
## Configuration
|
67
67
|
|
68
|
-
|
68
|
+
### The Basics
|
69
|
+
|
70
|
+
Configuration lives in `config/requirejs.yml`. These values are inspected and
|
71
|
+
used by `requirejs-rails` and passed along as configuration for require.js and
|
72
|
+
`r.js`. The default configuration declares `application.js` as the sole
|
73
|
+
top-level module. This can be overridden by creating
|
74
|
+
a `config/requirejs.yml`, such as:
|
69
75
|
|
70
76
|
```yaml
|
71
77
|
modules:
|
72
78
|
- name: 'mytoplevel'
|
73
79
|
```
|
74
80
|
|
75
|
-
You may pass in [require.js config
|
81
|
+
You may pass in [require.js config
|
82
|
+
options](http://requirejs.org/docs/api.html#config) as needed. For example,
|
83
|
+
to add path parameters:
|
76
84
|
|
77
85
|
```yaml
|
78
86
|
paths:
|
@@ -80,7 +88,12 @@ paths:
|
|
80
88
|
"d3.time": "d3/d3.time"
|
81
89
|
```
|
82
90
|
|
83
|
-
|
91
|
+
### Layered builds
|
92
|
+
|
93
|
+
Only modules specified in the configuration will be created as build artifacts
|
94
|
+
by `r.js`. [Layered r.js
|
95
|
+
builds](http://requirejs.org/docs/faq-optimization.html#priority) be
|
96
|
+
configured like so:
|
84
97
|
|
85
98
|
```yaml
|
86
99
|
modules:
|
@@ -89,9 +102,43 @@ modules:
|
|
89
102
|
exclude: ['appcommon']
|
90
103
|
- name: 'page2'
|
91
104
|
exclude: ['appcommon']
|
105
|
+
priority: ['appcommon']
|
106
|
+
```
|
107
|
+
|
108
|
+
In this example, only modules `page1` and `page2` are intended for direct
|
109
|
+
loading via `requirejs_include_tag`. The `appcommon` module contains
|
110
|
+
dependencies shared by the per-page modules. As a guideline, each module in
|
111
|
+
the configuration should be referenced by one of:
|
112
|
+
|
113
|
+
- A `requirejs_include_tag` in a template
|
114
|
+
- Pulled in via a dynamic `require()` call. Modules which are solely
|
115
|
+
referenced by a dynamic `require()` call (i.e. a call not optimized by r.js)
|
116
|
+
**must** be specified in the modules section in order to produce a correct
|
117
|
+
build.
|
118
|
+
- Be a common library module like `appcommon`, listed in the `priority` config
|
119
|
+
option.
|
120
|
+
|
121
|
+
## Advanced features
|
122
|
+
|
123
|
+
### Additional data attributes
|
124
|
+
|
125
|
+
`requirejs_include_tag` accepts an optional block which should return a hash.
|
126
|
+
This hash will be used to populate additional `data-...` attributes like so:
|
127
|
+
|
128
|
+
```erb
|
129
|
+
<%= requirejs_include_tag "page1" do |controller|
|
130
|
+
{ 'foo' => controller.foo,
|
131
|
+
'bar' => controller.bar
|
132
|
+
}
|
133
|
+
end
|
134
|
+
%>
|
92
135
|
```
|
93
136
|
|
94
|
-
|
137
|
+
This will generate a script tag like so:
|
138
|
+
|
139
|
+
```
|
140
|
+
<script data-main="/assets/page1.js" data-foo="..." data-bar="..." src="/assets/require.js"></script>
|
141
|
+
```
|
95
142
|
|
96
143
|
## Using AMD libraries
|
97
144
|
|
@@ -111,29 +158,31 @@ define ['jquery'], ($) ->
|
|
111
158
|
|
112
159
|
### Backbone.js
|
113
160
|
|
114
|
-
|
115
|
-
|
116
|
-
|
161
|
+
Backbone 0.9.x doesn't support AMD natively. I recommend the [amdjs
|
162
|
+
fork of Backbone](https://github.com/amdjs/backbone/) which adds AMD
|
163
|
+
support and actively tracks mainline.
|
117
164
|
|
118
165
|
### Underscore.js
|
119
166
|
|
120
|
-
Underscore
|
121
|
-
|
122
|
-
**IMPORTANT:** Underscore has **removed** AMD support again in the 1.3.x series. Please consult the [requirejs mailing list](http://groups.google.com/group/requirejs/) for the current recommended solution.
|
167
|
+
Underscore 1.3.x likewise doesn't have AMD support. Again, see
|
168
|
+
the [amdjs fork of Underscore](https://github.com/amdjs/underscore).
|
123
169
|
|
124
170
|
## Changes
|
125
171
|
|
126
|
-
Usage changes that impact folks upgrading along the 0.x series are
|
172
|
+
Usage changes that impact folks upgrading along the 0.x series are
|
173
|
+
documented here. See [the Changelog](CHANGELOG.md) for other details.
|
127
174
|
|
128
175
|
### v0.5.1
|
129
176
|
|
130
177
|
- `requirejs_include_tag` now generates a data-main attribute if given an argument, ala:
|
131
178
|
|
132
179
|
```erb
|
133
|
-
|
134
|
-
|
180
|
+
<%= requirejs_include_tag "application" %>
|
181
|
+
```
|
135
182
|
|
136
|
-
This usage is preferred to using a separate
|
183
|
+
This usage is preferred to using a separate
|
184
|
+
`javascript_include_tag`, which will produce errors from require.js or
|
185
|
+
r.js if the included script uses define anonymously, or not at all.
|
137
186
|
|
138
187
|
### v0.5.0
|
139
188
|
|
@@ -141,12 +190,12 @@ Usage changes that impact folks upgrading along the 0.x series are documented he
|
|
141
190
|
- It is no longer necessary or desirable to specify `baseUrl` explicitly in the configuration.
|
142
191
|
- Users should migrate application configuration previously in `application.js` (ala `require.config(...)`) to `config/requirejs.yml`
|
143
192
|
|
144
|
-
|
193
|
+
|
145
194
|
|
146
195
|
## TODOs
|
147
196
|
|
148
|
-
|
149
|
-
|
197
|
+
Please check out [our GitHub issues page](https://github.com/jwhitley/requirejs-rails/issues)
|
198
|
+
to see what's upcoming and to file feature requests and bug reports.
|
150
199
|
|
151
200
|
----
|
152
201
|
|
@@ -1,6 +1,19 @@
|
|
1
1
|
require 'requirejs/error'
|
2
2
|
|
3
3
|
module RequirejsHelper
|
4
|
+
def _requirejs_data(name, &block)
|
5
|
+
{}.tap do |data|
|
6
|
+
if name
|
7
|
+
name += ".js" unless name =~ /\.js$/
|
8
|
+
data['main'] = javascript_path(name)
|
9
|
+
end
|
10
|
+
|
11
|
+
data.merge!(yield controller) if block_given?
|
12
|
+
end.map do |k, v|
|
13
|
+
%Q{data-#{k}="#{v}"}
|
14
|
+
end.join(" ")
|
15
|
+
end
|
16
|
+
|
4
17
|
def _data_main(name)
|
5
18
|
if name
|
6
19
|
name += ".js" unless name =~ /\.js$/
|
@@ -10,18 +23,36 @@ module RequirejsHelper
|
|
10
23
|
end
|
11
24
|
end
|
12
25
|
|
13
|
-
def requirejs_include_tag(name=nil)
|
26
|
+
def requirejs_include_tag(name=nil, &block)
|
14
27
|
html = ""
|
15
|
-
|
28
|
+
requirejs = Rails.application.config.requirejs
|
29
|
+
|
16
30
|
if controller.requirejs_included
|
17
31
|
raise Requirejs::MultipleIncludeError, "Only one requirejs_include_tag allowed per page."
|
18
32
|
end
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
33
|
+
|
34
|
+
unless requirejs.run_config.empty?
|
35
|
+
run_config = requirejs.run_config
|
36
|
+
if Rails.application.config.assets.digest
|
37
|
+
modules = requirejs.build_config['modules'].map { |m| m['name'] }
|
38
|
+
|
39
|
+
# Generate digestified paths from the modules spec
|
40
|
+
paths = {}
|
41
|
+
modules.each { |m| paths[m] = javascript_path(m).sub /\.js$/,'' }
|
42
|
+
|
43
|
+
# Override uesr paths, whose mappings are only relevant in dev mode
|
44
|
+
# and in the build_config.
|
45
|
+
run_config['paths'] = paths
|
46
|
+
end
|
47
|
+
html.concat <<-HTML
|
48
|
+
<script>var require = #{run_config.to_json};</script>
|
49
|
+
HTML
|
50
|
+
end
|
51
|
+
|
52
|
+
html.concat <<-HTML
|
53
|
+
<script #{_requirejs_data(name, &block)} src="#{javascript_path 'require.js'}"></script>
|
24
54
|
HTML
|
55
|
+
|
25
56
|
controller.requirejs_included = true
|
26
57
|
html.html_safe
|
27
58
|
end
|
@@ -30,15 +30,72 @@ module Requirejs::Rails
|
|
30
30
|
self.user_config = {}
|
31
31
|
end
|
32
32
|
|
33
|
-
self.
|
33
|
+
self.run_config_whitelist = %w{
|
34
|
+
baseUrl
|
35
|
+
callback
|
36
|
+
catchError
|
37
|
+
context
|
38
|
+
deps
|
39
|
+
jQuery
|
40
|
+
locale
|
41
|
+
packages
|
42
|
+
paths
|
43
|
+
priority
|
44
|
+
scriptType
|
45
|
+
urlArgs
|
46
|
+
waitSeconds
|
47
|
+
xhtml
|
48
|
+
}
|
49
|
+
|
50
|
+
self.build_config_whitelist = %w{
|
51
|
+
appDir
|
52
|
+
baseUrl
|
53
|
+
closure
|
54
|
+
cssImportIgnore
|
55
|
+
cssIn
|
56
|
+
dir
|
57
|
+
fileExclusionRegExp
|
58
|
+
findNestedDependencies
|
59
|
+
has
|
60
|
+
hasOnSave
|
61
|
+
include
|
62
|
+
inlineText
|
63
|
+
locale
|
64
|
+
mainConfigFile
|
65
|
+
modules
|
66
|
+
name
|
67
|
+
namespace
|
68
|
+
onBuildRead
|
69
|
+
onBuildWrite
|
70
|
+
optimize
|
71
|
+
optimizeAllPluginResources
|
72
|
+
optimizeCss
|
73
|
+
out
|
74
|
+
packagePaths
|
75
|
+
packages
|
76
|
+
paths
|
77
|
+
pragmas
|
78
|
+
pragmasOnSave
|
79
|
+
preserveLicenseComments
|
80
|
+
skipModuleInsertion
|
81
|
+
skipPragmas
|
82
|
+
uglify
|
83
|
+
useStrict
|
84
|
+
wrap
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
def build_config
|
89
|
+
build_config = self.run_config.merge "baseUrl" => source_dir.to_s
|
90
|
+
build_config.merge!(self.user_config).slice(*self.build_config_whitelist)
|
91
|
+
end
|
92
|
+
|
93
|
+
def run_config
|
94
|
+
run_config = {
|
34
95
|
"baseUrl" => "/assets",
|
35
96
|
"modules" => [ { 'name' => 'application' } ]
|
36
97
|
}
|
37
|
-
|
38
|
-
self.run_config_json = self.run_config.to_json
|
39
|
-
|
40
|
-
self.build_config = self.run_config.merge "baseUrl" => source_dir.to_s
|
41
|
-
self.build_config.merge!(self.user_config)
|
98
|
+
run_config.merge!(self.user_config).slice(*self.run_config_whitelist)
|
42
99
|
end
|
43
100
|
|
44
101
|
def module_path_for(name)
|
@@ -38,6 +38,16 @@ class RequirejsHelperTest < ActionView::TestCase
|
|
38
38
|
assert_select "script:last-of-type[src^=/javascripts/require.js][data-main^=/javascripts/application.js]", :count => 1
|
39
39
|
end
|
40
40
|
|
41
|
+
test "requirejs_include_tag_with_block" do
|
42
|
+
test_block = Proc.new do |controller|
|
43
|
+
{ 'class' => controller.class.to_s.demodulize }
|
44
|
+
end
|
45
|
+
|
46
|
+
render :text => wrap(requirejs_include_tag("application", &test_block))
|
47
|
+
assert_select "script:last-of-type[src^=/javascripts/require.js][data-main^=/javascripts/application.js]", :count => 1
|
48
|
+
assert_select "script:last-of-type[src^=/javascripts/require.js][data-class^=TestController]", :count => 1
|
49
|
+
end
|
50
|
+
|
41
51
|
test "requirejs_include_tag can appear only once" do
|
42
52
|
assert_raises Requirejs::MultipleIncludeError do
|
43
53
|
render :text => "#{requirejs_include_tag}\n#{requirejs_include_tag}"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: requirejs-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-03-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: railties
|
16
|
-
requirement: &
|
16
|
+
requirement: &70307822048760 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -24,10 +24,10 @@ dependencies:
|
|
24
24
|
version: '3.3'
|
25
25
|
type: :runtime
|
26
26
|
prerelease: false
|
27
|
-
version_requirements: *
|
27
|
+
version_requirements: *70307822048760
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: rails
|
30
|
-
requirement: &
|
30
|
+
requirement: &70307822046940 !ruby/object:Gem::Requirement
|
31
31
|
none: false
|
32
32
|
requirements:
|
33
33
|
- - ! '>='
|
@@ -38,10 +38,10 @@ dependencies:
|
|
38
38
|
version: '3.3'
|
39
39
|
type: :development
|
40
40
|
prerelease: false
|
41
|
-
version_requirements: *
|
41
|
+
version_requirements: *70307822046940
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: sqlite3
|
44
|
-
requirement: &
|
44
|
+
requirement: &70307822042460 !ruby/object:Gem::Requirement
|
45
45
|
none: false
|
46
46
|
requirements:
|
47
47
|
- - ! '>='
|
@@ -49,7 +49,7 @@ dependencies:
|
|
49
49
|
version: '0'
|
50
50
|
type: :development
|
51
51
|
prerelease: false
|
52
|
-
version_requirements: *
|
52
|
+
version_requirements: *70307822042460
|
53
53
|
description: This gem provides RequireJS support for your Rails 3 application.
|
54
54
|
email:
|
55
55
|
- whitley@bangpath.org
|
@@ -128,7 +128,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
128
128
|
version: '0'
|
129
129
|
segments:
|
130
130
|
- 0
|
131
|
-
hash:
|
131
|
+
hash: 4356678976778407183
|
132
132
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
133
|
none: false
|
134
134
|
requirements:
|
@@ -137,12 +137,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
137
|
version: '0'
|
138
138
|
segments:
|
139
139
|
- 0
|
140
|
-
hash:
|
140
|
+
hash: 4356678976778407183
|
141
141
|
requirements:
|
142
142
|
- node.js is required for 'rake assets:precompile', used to run the r.js build
|
143
143
|
- If needed, jQuery should be v1.7 or greater (jquery-rails >= 1.0.17).
|
144
144
|
rubyforge_project:
|
145
|
-
rubygems_version: 1.8.
|
145
|
+
rubygems_version: 1.8.11
|
146
146
|
signing_key:
|
147
147
|
specification_version: 3
|
148
148
|
summary: Use RequireJS with the Rails 3 Asset Pipeline
|