strockets 0.2.0 → 0.3.0

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/README.md CHANGED
@@ -3,33 +3,49 @@
3
3
  Use Stitch allong with Sprockets
4
4
 
5
5
  ## Warning
6
- This is currently under revision so the gem's API might change
6
+ This is currently under revision so the gem's API might change but at this state is very less likely
7
7
 
8
- ## Usage
8
+ ## Installation
9
+
10
+ ### Rails 3.1
9
11
 
10
12
  Just include this gem in your Gemfile
11
13
 
12
14
  ```ruby
13
- gem 'sprockets'
15
+ # Gemfile
16
+ gem 'strockets'
14
17
  ```
15
18
 
16
- And all your javascript files will be compiled using Stitch/CommonJS.
17
- You can add
19
+ ### Others
18
20
 
19
- ```javascript
20
- // no-stitch
21
- ```
21
+ Add the gem to your Gemfile and register Strockets on your sprockets environment:
22
22
 
23
- on any file's first line to prevent it from being stitched.
23
+ ```ruby
24
+ # Gemfile
25
+ gem 'strockets'
24
26
 
25
- Alternatively you can enable the compatibility mode with
27
+ # initializer_file.rb
28
+ env.append_path Strockets.stitch_path
29
+ env.register_postprocessor(
30
+ 'application/javascript',
31
+ Strockets::StitchPostprocessor
32
+ )
26
33
 
27
- ```ruby
28
- Sprockets.compatibility_mode!
29
34
  ```
35
+ ## Usage
36
+
37
+ By default Strockets will Stitch every file excluding application.js and those inside a vendor directory or a gem.
30
38
 
31
- to conditionaly stitch files that have the following header:
39
+ But you can force the stitching of a file by adding a directive to your file's header like
32
40
 
33
41
  ```javascript
34
- // stitch
42
+ //= stitch true|false
35
43
  ```
44
+
45
+ ## Configuration
46
+
47
+ Strockets has the following options:
48
+
49
+ * `Strockets.namespace`: Which namespace to use
50
+ * `Strockets.defaults_to_stitch`: Default Action (to stitch or not to stitch)
51
+ * `Strockets.stitch_exceptions`: Which files shouldn't follow the default action
@@ -1,5 +1,6 @@
1
+ //= stitch false
1
2
  (function(/*! Stitch !*/) {
2
- if (!this.require) {
3
+ if (!this.<%= Strockets.namespace %>) {
3
4
 
4
5
  var modules = {},
5
6
  cache = {},
@@ -46,11 +47,11 @@
46
47
  return path.split('/').slice(0, -1).join('/');
47
48
  };
48
49
 
49
- this.require = function(name) {
50
+ this.<%= Strockets.namespace %> = function(name) {
50
51
  return require(name, '');
51
52
  }
52
53
 
53
- this.require.define = function(bundle) {
54
+ this.<%= Strockets.namespace %>.define = function(bundle) {
54
55
  for (var key in bundle)
55
56
  modules[key] = bundle[key];
56
57
  };
@@ -1,11 +1,36 @@
1
1
  module Strockets
2
2
  module Config
3
- def compatibility_mode?
4
- @compatibility_mode || false
3
+ STITCH_EXCEPTIONS_ON_TRUE = [%r[/application.js], %r[/vendor/], %r[/gems/]]
4
+ STITCH_EXCEPTIONS_ON_FALSE = []
5
+
6
+ def namespace
7
+ @namespace || "require"
8
+ end
9
+
10
+ def namespace=(namespace)
11
+ @namespace = namespace
12
+ end
13
+
14
+ def defaults_to_stitch?
15
+ @defaults_to_stitch || true
16
+ end
17
+ alias_method :defaults_to_stitch, :defaults_to_stitch?
18
+
19
+ def defaults_to_stitch=(default)
20
+ @defaults_to_stitch = default
21
+ end
22
+
23
+ def stitch_exceptions
24
+ @stitch_exceptions ||
25
+ defaults_to_stitch? ? STITCH_EXCEPTIONS_ON_TRUE : STITCH_EXCEPTIONS_ON_FALSE
26
+ end
27
+
28
+ def stitch_exceptions=(stitch_exceptions)
29
+ @stitch_exceptions = stitch_exceptions
5
30
  end
6
31
 
7
- def compatibility_mode!
8
- @compatibility_mode = true
32
+ def stitch_path
33
+ File.expand_path("../assets",__FILE__)
9
34
  end
10
35
  end
11
36
 
@@ -0,0 +1 @@
1
+ require 'strockets/railties/engine' if defined? Rails
@@ -0,0 +1,14 @@
1
+ module Strockets
2
+ module Railties
3
+ class Engine < ::Rails::Engine
4
+ initializer 'strockets.configure_rails_initialization' do |app|
5
+ app.assets.append_path Strockets.stitch_path
6
+
7
+ app.assets.register_postprocessor(
8
+ 'application/javascript',
9
+ Strockets::StitchPostprocessor
10
+ )
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,73 @@
1
+ module Strockets
2
+ class StitchPostprocessor < Tilt::Template
3
+ HEADER_PATTERN = Sprockets::DirectiveProcessor::HEADER_PATTERN
4
+ DIRECTIVE_PATTERN = Sprockets::DirectiveProcessor::DIRECTIVE_PATTERN
5
+
6
+ def prepare; end
7
+
8
+ def evaluate(context, locals, &block)
9
+ @context = context
10
+
11
+ stitch? ? define : data
12
+ end
13
+
14
+ protected
15
+ attr_reader :context
16
+
17
+ def process_requires
18
+
19
+ end
20
+
21
+ def stitch?
22
+ stitch = directive?
23
+
24
+ if stitch.nil?
25
+ stitch = Strockets.defaults_to_stitch?
26
+ stitch = !stitch if exception?
27
+ end
28
+
29
+ stitch
30
+ end
31
+
32
+ def define
33
+ context.require_asset "stitch"
34
+
35
+ "\n#{ Strockets.namespace }.define({'#{
36
+ context.logical_path
37
+ }': function(exports, require, module) {\n#{
38
+ indent(data)
39
+ }\n}});\n"
40
+ end
41
+
42
+ def exception?
43
+ Strockets.stitch_exceptions.any?{ |e| e.match(context.pathname.to_s) }
44
+ end
45
+
46
+ def directive?
47
+ header = context.pathname.read[HEADER_PATTERN, 0] || ""
48
+ header.lines.each do |line|
49
+ if directive = line[DIRECTIVE_PATTERN, 1]
50
+ name, *args = Shellwords.shellwords(directive)
51
+ if name == "stitch"
52
+ case args.first
53
+ when "true" then return true
54
+ when "false" then return false
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ nil
61
+ end
62
+
63
+ def indent(string)
64
+ string.gsub(/$(.)/m, "\\1 ").strip
65
+ end
66
+ end
67
+ end
68
+
69
+ class Sprockets::DirectiveProcessor
70
+ # This is just a placeholder to make Sprockets erase stitch directives
71
+ def process_stitch_directive(arg)
72
+ end
73
+ end
@@ -1,3 +1,3 @@
1
1
  module Strockets
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/strockets.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require "strockets/version"
2
2
  require "strockets/config"
3
- require "strockets/processor"
4
- require "strockets/bundle"
5
- require "strockets/environment"
3
+ require "strockets/stitch_postprocessor"
4
+ require "strockets/railtie"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strockets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
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: 2011-12-14 00:00:00.000000000 Z
12
+ date: 2012-01-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70297367288240 !ruby/object:Gem::Requirement
16
+ requirement: &70177853989400 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70297367288240
24
+ version_requirements: *70177853989400
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sprockets
27
- requirement: &70297367287720 !ruby/object:Gem::Requirement
27
+ requirement: &70177853988840 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 2.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70297367287720
35
+ version_requirements: *70177853988840
36
36
  description: Use Stitch allong with Sprockets
37
37
  email:
38
38
  - me@sagmor.com
@@ -45,11 +45,11 @@ files:
45
45
  - README.md
46
46
  - Rakefile
47
47
  - lib/strockets.rb
48
- - lib/strockets/bundle.rb
48
+ - lib/strockets/assets/stitch.js.erb
49
49
  - lib/strockets/config.rb
50
- - lib/strockets/environment.rb
51
- - lib/strockets/processor.rb
52
- - lib/strockets/stitch.js
50
+ - lib/strockets/railtie.rb
51
+ - lib/strockets/railties/engine.rb
52
+ - lib/strockets/stitch_postprocessor.rb
53
53
  - lib/strockets/version.rb
54
54
  - strockets.gemspec
55
55
  homepage: https://github.com/sagmor/strockets
@@ -66,7 +66,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
66
66
  version: '0'
67
67
  segments:
68
68
  - 0
69
- hash: -2558210479809988184
69
+ hash: 4466840386315886126
70
70
  required_rubygems_version: !ruby/object:Gem::Requirement
71
71
  none: false
72
72
  requirements:
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  version: '0'
76
76
  segments:
77
77
  - 0
78
- hash: -2558210479809988184
78
+ hash: 4466840386315886126
79
79
  requirements: []
80
80
  rubyforge_project: strockets
81
81
  rubygems_version: 1.8.10
@@ -1,13 +0,0 @@
1
- module Strockets
2
- class Bundle < Tilt::Template
3
- self.default_mime_type = 'application/javascript'
4
-
5
- def prepare
6
- end
7
-
8
- def evaluate(scope, locals, &block)
9
- File.read(File.expand_path('../stitch.js',__FILE__)) + data
10
- end
11
- end
12
- end
13
-
@@ -1,17 +0,0 @@
1
- module Strockets
2
- module Environment
3
- def self.included(base)
4
- base.send :alias_method, :initialize_without_stitch, :initialize
5
- base.send :alias_method, :initialize, :initialize_with_stitch
6
- end
7
-
8
- def initialize_with_stitch(*args)
9
- initialize_without_stitch(*args)
10
-
11
- self.register_bundle_processor 'application/javascript', Bundle
12
- self.register_postprocessor 'application/javascript', Processor
13
- end
14
- end
15
- end
16
-
17
- Sprockets::Environment.send :include, Strockets::Environment
@@ -1,31 +0,0 @@
1
- module Strockets
2
- class Processor < Tilt::Template
3
- self.default_mime_type = 'application/javascript'
4
-
5
- def prepare
6
- end
7
-
8
- def evaluate(scope, locals, &block)
9
- if stitch? scope
10
- "\nrequire.define({'#{ scope.logical_path }': function(exports, require, module) {\n" +
11
- indent(data) +
12
- "\n}});\n"
13
- else
14
- data
15
- end
16
- end
17
-
18
- private
19
- def stitch?(scope)
20
- return false if scope.pathname.basename.to_s =~ /\.jst/
21
-
22
- directive = File.open(scope.pathname){|f| f.readline}.downcase.gsub(/[^a-z\-]/,"") rescue nil
23
-
24
- Strockets.compatibility_mode? ? directive == "stitch" : directive != "no-stitch"
25
- end
26
-
27
- def indent(string)
28
- string.gsub(/$(.)/m, "\\1 ").strip
29
- end
30
- end
31
- end