Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

bundle exec rake assets:precompile results in node.js error under 0.7.0 #41

Merged
merged 2 commits into from Apr 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,11 @@
# v0.7.1

- Liberalize asset path filtering. `0.7.0` added filtering on the logical
asset path which was too aggressive in that only `.js` files were allowed in
builds. The RequireJS config variable `logical_asset_filter` has been
added, which allows `.js`, `.html`, `.txt` files by default and is user
configurable.

# v0.7.0

- Support for [almond](https://github.com/jrburke/almond) via
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
requirejs-rails (0.7.0)
requirejs-rails (0.7.1)
railties (>= 3.1.1, < 3.3)

GEM
Expand Down
71 changes: 49 additions & 22 deletions README.md
Expand Up @@ -6,15 +6,15 @@ Integrates [RequireJS](http://requirejs.org/) into the Rails 3 Asset Pipeline.

## Usage

1. Add this to your Rails app's `Gemfile`:
1. Add this to your Rails app's `Gemfile`:

```
gem 'requirejs-rails'
```

2. Remove all Sprockets directives such as `//= require jquery` from `application.js` and elsewhere. Instead establish JavaScript dependencies using AMD-style `define()` and `require()` calls.
2. Remove all Sprockets directives such as `//= require jquery` from `application.js` and elsewhere. Instead establish JavaScript dependencies using AMD-style `define()` and `require()` calls.

3. Use `requirejs_include_tag` at the top-level of your app's layout(s). Other modules will be pulled in dynamically by `require.js` in development and for production builds optimized by `r.js`. Here's a basic `app/views/layouts/application.html.erb` modified for `requirejs-rails`:
3. Use `requirejs_include_tag` at the top-level of your app's layout(s). Other modules will be pulled in dynamically by `require.js` in development and for production builds optimized by `r.js`. Here's a basic `app/views/layouts/application.html.erb` modified for `requirejs-rails`:

```erb
<!DOCTYPE html>
Expand All @@ -34,32 +34,35 @@ Integrates [RequireJS](http://requirejs.org/) into the Rails 3 Asset Pipeline.
</html>
```

4. Organize your JavaScript or CoffeeScript code into modules using `define()`:
4. Organize your JavaScript or CoffeeScript code into modules using `define()`:

```coffeescript
# app/assets/javascripts/views/tweet_view.js.coffee
```coffeescript
# app/assets/javascripts/views/tweet_view.js.coffee

define ['backbone'], (Backbone) ->
class TweetView extends Backbone.View
# ...
```
define ['backbone'], (Backbone) ->
class TweetView extends Backbone.View
# ...
```

5. Instantiate your app using `require()` from a top-level module such as `application.js`:
5. Instantiate your app using `require()` from a top-level module such as `application.js`:

```coffeescript
# app/assets/javascripts/application.js.coffee
```coffeescript
# app/assets/javascripts/application.js.coffee

require ['jquery', 'backbone', 'TheApp'], ($, Backbone, TheApp) ->
require ['jquery', 'backbone', 'TheApp'], ($, Backbone, TheApp) ->

# Start up the app once the DOM is ready
$ ->
window.App = new TheApp()
Backbone.history.start
pushState: true
window.App.start()
```
# Start up the app once the DOM is ready
$ ->
window.App = new TheApp()
Backbone.history.start
pushState: true
window.App.start()
```

6. When ready, build your assets for production deployment as usual. `requirejs-rails` defaults to a single-file build of `application.js`. Additional modules and r.js layered builds may be specified via `config\requirejs.yml`; see the Configuration section below.
6. When ready, build your assets for production deployment as usual.
`requirejs-rails` defaults to a single-file build of `application.js`.
Additional modules and r.js layered builds may be specified via
`config\requirejs.yml`; see the Configuration section below.

```rake assets:precompile```

Expand Down Expand Up @@ -137,6 +140,30 @@ modules:
wrap: true
```

### Build-time asset filter

The `requirejs-rails` build process uses the Asset Pipeline to assemble assets
for the `r.js` build. By default, assets ending in `.js`, `.html`, and `.txt`
will be made available to the build. If you have other asset suffixes to
include, use the `logical_asset_filter` config setting to add them.

For example, if your templates all end in `.templ` like so...

```javascript
// in app/assets/javascripts/myapp.js
define(function (require) {
var stuff = require('text!stuff.templ');
// ...
});
```

... then this config setting will ensure they're picked up in the build:

```ruby
# in config/application.rb
config.requirejs.logical_asset_filter += [/\.templ$/]
```

## Advanced features

### Additional data attributes
Expand Down
7 changes: 7 additions & 0 deletions lib/requirejs/rails/config.rb
Expand Up @@ -13,6 +13,7 @@ def initialize
super
self.manifest = nil

self.logical_asset_filter = [/\.js$/,/\.html$/,/\.txt$/]
self.tmp_dir = Rails.root + 'tmp'
self.bin_dir = Pathname.new(__FILE__+'/../../../../bin').cleanpath

Expand Down Expand Up @@ -130,5 +131,11 @@ def module_path_for(mod)
def get_binding
return binding()
end

def asset_allowed?(asset)
self.logical_asset_filter.reduce(false) do |accum, matcher|
accum || (matcher =~ asset)
end ? true : false
end
end
end
2 changes: 1 addition & 1 deletion lib/requirejs/rails/version.rb
@@ -1,6 +1,6 @@
module Requirejs
module Rails
Version = "0.7.0"
Version = "0.7.1"
LibVersion = "1.0.7"
end
end
2 changes: 1 addition & 1 deletion lib/tasks/requirejs-rails_tasks.rake
Expand Up @@ -90,7 +90,7 @@ EOM
"requirejs:clean"] do
requirejs.config.source_dir.mkpath
requirejs.env.each_logical_path do |logical_path|
next unless logical_path =~ /\.js$/
next unless requirejs.config.asset_allowed?(logical_path)
if asset = requirejs.env.find_asset(logical_path)
filename = requirejs.config.source_dir + asset.logical_path
filename.dirname.mkpath
Expand Down
7 changes: 7 additions & 0 deletions test/requirejs-rails_test.rb
Expand Up @@ -37,6 +37,13 @@ def setup
@cfg.loader = :wombat
end
end

test "matches configured logical assets" do
assert_equal true, @cfg.asset_allowed?('foo.js')
assert_equal false, @cfg.asset_allowed?('bar.frobnitz')
@cfg.logical_asset_filter += [/\.frobnitz$/]
assert_equal true, @cfg.asset_allowed?('bar.frobnitz')
end
end

class RequirejsHelperTest < ActionView::TestCase
Expand Down