Skip to content

Commit

Permalink
Updated Strockets API and cleaner inner working
Browse files Browse the repository at this point in the history
  • Loading branch information
sagmor committed Jan 19, 2012
1 parent 9bf1f2b commit 840a6f5
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 83 deletions.
36 changes: 24 additions & 12 deletions README.md
Expand Up @@ -5,31 +5,43 @@ Use Stitch allong with Sprockets
## Warning
This is currently under revision so the gem's API might change

## Usage
## Installation

### Rails 3.1

Just include this gem in your Gemfile

```ruby
gem 'sprockets'
```

And all your javascript files will be compiled using Stitch/CommonJS.
You can add
### Others

```javascript
// no-stitch
```
After adding the gem to your Gemfile, register Strockets on your sprockets environment:

on any file's first line to prevent it from being stitched.
```ruby
env.append_path Strockets.stitch_path

Alternatively you can enable the compatibility mode with
env.register_postprocessor(
'application/javascript',
Strockets::StitchPostprocessor
)

```ruby
Sprockets.compatibility_mode!
```
## Usage

By default Strockets will Stitch every file excluding application.js and those inside a vendor directory or a gem.

to conditionaly stitch files that have the following header:
But you can force the stitching of a file by adding a directive to your file's header like

```javascript
// stitch
//= stitch true|false
```

## Configuration

Strockets has the following options:

* `Strockets.namespace`: Which namespace to use
* `Strockets.default_to_stitch`: Default Action (to stitch or not to stitch)
* `Strockets.stitch_exceptions`: Which files shouldn't follow the default action
5 changes: 2 additions & 3 deletions lib/strockets.rb
@@ -1,5 +1,4 @@
require "strockets/version"
require "strockets/config"
require "strockets/processor"
require "strockets/bundle"
require "strockets/environment"
require "strockets/stitch_postprocessor"
require "strockets/railtie"
@@ -1,5 +1,6 @@
//= stitch false
(function(/*! Stitch !*/) {
if (!this.require) {
if (!this.<%= Strockets.namespace %>) {

var modules = {},
cache = {},
Expand Down Expand Up @@ -46,11 +47,11 @@
return path.split('/').slice(0, -1).join('/');
};

this.require = function(name) {
this.<%= Strockets.namespace %> = function(name) {
return require(name, '');
}

this.require.define = function(bundle) {
this.<%= Strockets.namespace %>.define = function(bundle) {
for (var key in bundle)
modules[key] = bundle[key];
};
Expand Down
13 changes: 0 additions & 13 deletions lib/strockets/bundle.rb

This file was deleted.

33 changes: 29 additions & 4 deletions lib/strockets/config.rb
@@ -1,11 +1,36 @@
module Strockets
module Config
def compatibility_mode?
@compatibility_mode || false
STITCH_EXCEPTIONS_ON_TRUE = [%r[/application.js], %r[/vendor/], %r[/gems/]]
STITCH_EXCEPTIONS_ON_FALSE = []

def namespace
@namespace || "require"
end

def namespace=(namespace)
@namespace = namespace
end

def default_to_stitch?
@default_to_stitch || true
end
alias_method :default_to_stitch, :default_to_stitch?

def default_to_stitch=(default)
@stitch_by_default = default
end

def stitch_exceptions
@stitch_exceptions ||
default_to_stitch? ? STITCH_EXCEPTIONS_ON_TRUE : STITCH_EXCEPTIONS_ON_FALSE
end

def stitch_exceptions=(stitch_exceptions)
@stitch_exceptions = stitch_exceptions
end

def compatibility_mode!
@compatibility_mode = true
def stitch_path
File.expand_path("../assets",__FILE__)
end
end

Expand Down
17 changes: 0 additions & 17 deletions lib/strockets/environment.rb

This file was deleted.

31 changes: 0 additions & 31 deletions lib/strockets/processor.rb

This file was deleted.

1 change: 1 addition & 0 deletions lib/strockets/railtie.rb
@@ -0,0 +1 @@
require 'strockets/railties/engine' if defined? Rails
14 changes: 14 additions & 0 deletions lib/strockets/railties/engine.rb
@@ -0,0 +1,14 @@
module Strockets
module Railties
class Engine < ::Rails::Engine
initializer 'strockets.configure_rails_initialization' do |app|
app.assets.append_path Strockets.stitch_path

app.assets.register_postprocessor(
'application/javascript',
Strockets::StitchPostprocessor
)
end
end
end
end
73 changes: 73 additions & 0 deletions lib/strockets/stitch_postprocessor.rb
@@ -0,0 +1,73 @@
module Strockets
class StitchPostprocessor < Tilt::Template
HEADER_PATTERN = Sprockets::DirectiveProcessor::HEADER_PATTERN
DIRECTIVE_PATTERN = Sprockets::DirectiveProcessor::DIRECTIVE_PATTERN

def prepare; end

def evaluate(context, locals, &block)
@context = context

stitch? ? define : data
end

protected
attr_reader :context

def process_requires

end

def stitch?
stitch = directive?

if stitch.nil?
stitch = Strockets.default_to_stitch?
stitch = !stitch if exception?
end

stitch
end

def define
context.require_asset "stitch"

"\n#{ Strockets.namespace }.define({'#{
context.logical_path
}': function(exports, require, module) {\n#{
indent(data)
}\n}});\n"
end

def exception?
Strockets.stitch_exceptions.any?{ |e| e.match(context.pathname.to_s) }
end

def directive?
header = context.pathname.read[HEADER_PATTERN, 0] || ""
header.lines.each do |line|
if directive = line[DIRECTIVE_PATTERN, 1]
name, *args = Shellwords.shellwords(directive)
if name == "stitch"
case args.first
when "true" then return true
when "false" then return false
end
end
end
end

nil
end

def indent(string)
string.gsub(/$(.)/m, "\\1 ").strip
end
end
end

class Sprockets::DirectiveProcessor
# This is just a placeholder to make Sprockets erase stitch directives
def process_stitch_directive(arg)
end
end

0 comments on commit 840a6f5

Please sign in to comment.