Initial, working version

This commit is contained in:
Loic Nageleisen 2013-09-30 16:27:12 +02:00
commit e9a75766fd
12 changed files with 196 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
*.gem
Gemfile.lock
.rbenv-version
.ruby-version

5
Gemfile Normal file
View file

@ -0,0 +1,5 @@
source 'https://rubygems.org'
gemspec
gem 'pry'

20
LICENSE Normal file
View file

@ -0,0 +1,20 @@
Copyright (c) 2012 Loic Nageleisen
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

11
README.mdown Normal file
View file

@ -0,0 +1,11 @@
# sprockets-umodule
[umodule-js](https://github.com/lloeki/umodule-js) integration with Sprockets
## Goal
Save you from the boilerplate of writing module wrapping.
## Usage
Add sprockets-umodule to your Gemfile. Name your module files with .umodule.

1
lib/sprockets-umodule.rb Normal file
View file

@ -0,0 +1 @@
require 'sprockets/umodule'

15
lib/sprockets/umodule.rb Normal file
View file

@ -0,0 +1,15 @@
require 'sprockets/umodule/version'
require 'sprockets/umodule/wrapper'
require 'sprockets/engines'
module Sprockets
module Umodule
class << self
attr_accessor :options
end
@options = {}
end
register_engine '.umodule', Umodule::Wrapper
end

View file

@ -0,0 +1,7 @@
(function () {
define = require('module').define;
define('<%= module_name %>', function (exports, require, module) {
<%= source %>
});
})();

View file

@ -0,0 +1,5 @@
module Sprockets
module Umodule
VERSION = '1.0.0'
end
end

View file

@ -0,0 +1,47 @@
require 'tilt'
module Sprockets
module Umodule
class Wrapper < Tilt::Template
@wrapper_template = File.read(File.join(File.dirname(__FILE__), 'umodule.js.erb'))
class << self
attr_reader :wrapper_template
def engine_initialized?
defined?(ERB)
end
end
def initialize_engine
require 'erb'
end
def prepare
@erb = ERB.new(wrapper_template)
end
def evaluate(context, locals, &block)
bind(@erb).render(context.logical_path, data)
end
private
def wrapper_template
self.class.wrapper_template
end
def bind(erb)
Class.new do
def initialize(erb)
@erb = erb
end
def render(module_name, source)
@erb.result(binding)
end
end.new(erb)
end
end
end
end

13
spec/spec_helper.rb Normal file
View file

@ -0,0 +1,13 @@
require 'sprockets'
require 'sprockets-umodule'
require 'sprockets-helpers'
require 'construct'
require 'pry'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
RSpec.configure do |config|
config.include Construct::Helpers
end

View file

@ -0,0 +1,44 @@
require 'spec_helper'
RSpec.configure do |c|
c.filter_run_excluding broken: true
c.filter_run_excluding todo: true
end
describe Sprockets::Umodule do
before :each do
@root = create_construct
@assets = @root.directory 'assets'
@env = Sprockets::Environment.new @root.to_s
@env.append_path @assets.to_s
end
after :each do
@root.destroy!
end
it 'processes umodule js files' do
@assets.file 'foo.js.umodule', %Q[bar = function () { "bar" };\n]
asset = @env['foo.js']
asset.to_s.should eq <<-SOURCE
(function () {
define = require('module').define;
define('foo', function (exports, require, module) {
bar = function () { "bar" };
});
})();
SOURCE
end
it 'sets the module name with the directory' do
@assets.file 'sub/foo.js.umodule', %Q[bar = function () { "bar" };\n]
asset = @env['sub/foo.js']
asset.to_s.should include %q[define('sub/foo',]
end
it 'indents the module contents prettily'
it 'works with coffeescript'
end

24
sprockets-umodule.gemspec Normal file
View file

@ -0,0 +1,24 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path('../lib', __FILE__)
require 'sprockets/umodule/version'
Gem::Specification.new do |s|
s.name = 'sprockets-umodule'
s.version = Sprockets::Umodule::VERSION
s.authors = ['Loic Nageleisen']
s.email = ['loic.nageleisen@gmail.com']
s.homepage = 'http://github.com/lloeki/sprockets-umodule'
s.summary = %q{uModule wrapping for the Sprockets asset pipeline.}
s.description = %q{uModule wrapping for the Sprockets asset pipeline.}
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n")
.map { |f| File.basename(f) }
s.require_paths = ['lib']
s.add_dependency 'umodule-source'
s.add_dependency 'tilt', '~> 1.1'
s.add_development_dependency 'sprockets-helpers', '~> 0.6'
s.add_development_dependency 'rspec', '~> 2.6'
s.add_development_dependency 'test-construct', '~> 1.2'
s.add_development_dependency 'rake'
end