seajs-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
3
+
4
+ # Custom for Visual Studio
5
+ *.cs diff=csharp
6
+ *.sln merge=union
7
+ *.csproj merge=union
8
+ *.vbproj merge=union
9
+ *.fsproj merge=union
10
+ *.dbproj merge=union
11
+
12
+ # Standard to msysgit
13
+ *.doc diff=astextplain
14
+ *.DOC diff=astextplain
15
+ *.docx diff=astextplain
16
+ *.DOCX diff=astextplain
17
+ *.dot diff=astextplain
18
+ *.DOT diff=astextplain
19
+ *.pdf diff=astextplain
20
+ *.PDF diff=astextplain
21
+ *.rtf diff=astextplain
22
+ *.RTF diff=astextplain
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in seajs-rails.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,85 @@
1
+ # Sea.js for Rails 3.x
2
+
3
+ Integrates Sea.js into the Rails 3 Asset Pipeline.
4
+ 提供一种将 Sea.js 与 Asset Pipeline 结合起来的方式,底层通过 spm-chaos-build 来实现
5
+
6
+ ## 安装
7
+
8
+ 首先请安装好 spm-chaos-build ,参见 https://github.com/edokeh/spm-chaos-build
9
+
10
+ $ npm install spm -g
11
+ $ npm install spm-chaos-build -g
12
+
13
+ 然后在 Gemfile 中添加这个 gem
14
+
15
+ gem 'seajs-rails'
16
+
17
+ 并执行命令
18
+
19
+ $ bundle
20
+
21
+ ## 使用方式
22
+
23
+ ### 初始化
24
+
25
+ 执行以下命令初始化项目
26
+
27
+ $ rake seajs:setup
28
+
29
+ 这会安装最新版本的 Sea.js 到相应目录下,并在 config 目录生成 seajs_config.yml
30
+
31
+ 配置文件说明:
32
+
33
+ ```yaml
34
+ seajs_path: seajs/seajs/2.0.0/sea.js # 这是配置 seajs 的路径
35
+ family: klog # 与打包合并有关的参数,自动生成为项目的名称,一般不需要修改
36
+ output: # 合并策略,具体参见 spm-chaos-build
37
+ relative:
38
+ - application.js
39
+ all: []
40
+ ```
41
+
42
+ ouput 项其实就是 package.json 中的 spm.output
43
+ 支持 relative(只合并相对路径), all(合并所有路径) 两种方式
44
+
45
+ 目录说明:
46
+
47
+ your-rails
48
+ ├─app
49
+ │ ├─assets
50
+ │ │ ├─images
51
+ │ │ ├─javascripts
52
+ │ │ │ ├─a.js <-- your js code
53
+ │ │ │ └─sea-modules <-- seajs base 目录
54
+ │ │ │ ├─gallery
55
+ │ │ │ ├─jquery
56
+ │ │ │ └─seajs
57
+ │ │ │ └─seajs
58
+ │ │ │ └─2.0.0
59
+ │ │ └─stylesheets
60
+
61
+ 与普通的开发基本一致,只是在 javascripts 目录下多了一个 sea-modules 作为 Sea.js 的 base 目录
62
+ 普通的业务代码依然放置在 javascripts 目录下(非 CMD 模块的 JS 也放在这里)
63
+
64
+ ### 使用
65
+
66
+ 提供两个 helper 方法:seajs_tag 与 seajs_use
67
+
68
+ ```erb
69
+ <%= seajs_tag %>
70
+ <%= seajs_use 'blogs/show' %>
71
+ ```
72
+ seajs_tag 用于引入 seajs ,并且会根据是否合并引入必要的配置
73
+ seajs_use 用于加载 CMD 模块,支持传递多个模块名称
74
+
75
+ ### 合并
76
+
77
+ 这个 gem 会将 seajs 的打包合并过程嵌入到 assets:precompile 任务中,所以执行这个命令
78
+
79
+ $ rake assets:precompile
80
+
81
+ Sea.js 的合并过程不会与原有的 assets pipeline 冲突,但是请将配置分开
82
+ * 需要合并的 CMD 模块文件请在 seajs_config.yml 中配置
83
+ * 而非 CMD 模块的文件请依然通过 `config.assets.precompile` 配置
84
+
85
+ 合并完成后,不需要改动页面,两个 helper 方法能够自动处理
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,36 @@
1
+ module SeajsHelper
2
+ # generate seajs.use()
3
+ def seajs_use(*modules)
4
+ seajs_config = Rails.application.config.seajs
5
+
6
+ if seajs_config.compiled?
7
+ modules.map! { |m| seajs_config.family + '/' + m }
8
+ else
9
+ modules.map! { |m| '/assets/' + m }
10
+ end
11
+
12
+ html = <<-html
13
+ <script>
14
+ seajs.use(#{modules.to_s})
15
+ </script>
16
+ html
17
+
18
+ html.html_safe
19
+ end
20
+
21
+ # include seajs script and map config
22
+ def seajs_tag
23
+ seajs_config = Rails.application.config.seajs
24
+
25
+ html = <<-html
26
+ <script src="/assets/sea-modules/#{seajs_config.seajs_path}"></script>
27
+ <script>
28
+ seajs.config({
29
+ map: #{seajs_config.map_json || '[]'}
30
+ })
31
+ </script>
32
+ html
33
+
34
+ html.html_safe
35
+ end
36
+ end
@@ -0,0 +1 @@
1
+ require 'seajs/rails'
@@ -0,0 +1,5 @@
1
+ module Seajs
2
+ module Rails
3
+ require 'seajs/rails/engine'
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ require 'active_support/ordered_options'
2
+ require 'yaml'
3
+
4
+ module Seajs::Rails
5
+ class Config < ::ActiveSupport::OrderedOptions
6
+
7
+ def initialize
8
+ super
9
+ # config/seajs_config.yml
10
+ self.config_path = ::Rails.root.join('config', 'seajs_config.yml')
11
+ # public/assets/seajs_map.json
12
+ self.map_path = File.join(::Rails.public_path, ::Rails.application.config.assets.prefix, "seajs-map.json")
13
+ end
14
+
15
+ def load_config_from_file
16
+ if File.exist?(self.config_path)
17
+ self.merge! YAML.load_file(self.config_path).symbolize_keys
18
+ end
19
+
20
+ if File.exist?(self.map_path)
21
+ self.map_json = File.open(map_path).read
22
+ self.is_compiled = true
23
+ else
24
+ self.is_compiled = false
25
+ end
26
+ end
27
+
28
+ def compiled?
29
+ is_compiled
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,31 @@
1
+ require 'seajs/rails/config'
2
+
3
+ module Seajs
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+
7
+ config.before_configuration do
8
+ config.seajs = Seajs::Rails::Config.new
9
+ end
10
+
11
+ config.before_initialize do |app|
12
+ config = app.config
13
+
14
+ # find the default precompile filter and modify it
15
+ index = config.assets.precompile.find_index do |filter|
16
+ filter.respond_to?(:call) && filter.call('sea.js.map')
17
+ end
18
+
19
+ if index
20
+ # add filter,don't precompile app/javascripts/sea-modules/
21
+ filter = Proc.new do |path|
22
+ !File.extname(path).in?(['.js', '.css']) and !path.start_with?('sea-modules/')
23
+ end
24
+ config.assets.precompile[index] = filter
25
+ end
26
+
27
+ config.seajs.load_config_from_file
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,6 @@
1
+ module Seajs
2
+ module Rails
3
+ VERSION = "0.0.1"
4
+ SEAJS_VERSION = "2.0.0"
5
+ end
6
+ end
@@ -0,0 +1,76 @@
1
+ require 'seajs/rails/version'
2
+
3
+ namespace :seajs do
4
+ desc "Check whether spm & spm-chaos-build is ok"
5
+ task :test_env do
6
+ begin
7
+ `spm-chaos-build -h`
8
+ rescue Errno::ENOENT
9
+ puts 'Please intall spm-chaos-build first.'
10
+ exit 1
11
+ end
12
+ end
13
+
14
+ desc 'init setup'
15
+ task :setup => :test_env do
16
+ # install seajs
17
+ path = Rails.root.join('app', 'assets', 'javascripts')
18
+ `cd #{path} && spm install seajs@#{Seajs::Rails::SEAJS_VERSION}`
19
+
20
+ # generate seajs config file
21
+ File.open(Rails.root.join('config', 'seajs_config.yml'), 'w') do |f|
22
+ config_temp = File.expand_path('../template/seajs_config.yml', __FILE__)
23
+ templ = Erubis::Eruby.new(File.open(config_temp).read)
24
+ f.write templ.result(:seajs_path => "seajs/seajs/#{Seajs::Rails::SEAJS_VERSION}/sea.js",
25
+ :family => Rails.application.class.parent_name.downcase)
26
+ end
27
+ end
28
+
29
+ namespace :compile do
30
+ task :external do
31
+ ruby_rake_task "seajs:compile:all"
32
+ end
33
+
34
+ task :all => ["seajs:compile:prepare_dir",
35
+ "seajs:compile:generate_json",
36
+ "seajs:compile:build",
37
+ "seajs:compile:clean"]
38
+
39
+ desc 'copy `app/assets/javascripts` to ``/public/assets/``'
40
+ task :prepare_dir do
41
+ cp_r Rails.root.join('app', 'assets', 'javascripts'), File.join(Rails.public_path, 'assets'), :remove_destination => true
42
+ cp File.expand_path('../template/Gruntfile.js', __FILE__), File.join(Rails.public_path, 'assets')
43
+ end
44
+
45
+ desc 'generate package.json by seajs_config.yml'
46
+ task :generate_json do
47
+ File.open(File.join(Rails.public_path, 'assets', 'javascripts', 'package.json'), 'w') do |f|
48
+
49
+ seajs_config = Rails.application.config.seajs
50
+ seajs_config.load_config_from_file
51
+ pkg = {:family => seajs_config.family, :spm => {:output => seajs_config.output}}
52
+ f.write pkg.to_json
53
+ end
54
+ end
55
+
56
+ desc 'run chaos-build in assets for `javascripts` dir'
57
+ task :build do
58
+ path = File.join(Rails.public_path, 'assets')
59
+ `cd #{path} && spm chaos-build javascripts`
60
+
61
+ unless $?.success?
62
+ raise RuntimeError, "spm chaos-build failed."
63
+ end
64
+ end
65
+
66
+ task :clean do
67
+ cp_r File.join(Rails.public_path, 'assets', 'javascripts', 'sea-modules', '.'), File.join(Rails.public_path, 'assets', 'sea-modules')
68
+ rm_rf File.join(Rails.public_path, 'assets', 'javascripts')
69
+ rm_rf File.join(Rails.public_path, 'assets', 'Gruntfile.js')
70
+ end
71
+ end
72
+ end
73
+
74
+ Rake::Task["assets:precompile"].enhance do
75
+ Rake::Task["seajs:compile:external"].invoke
76
+ end
@@ -0,0 +1,30 @@
1
+ var chaosBuild = require('spm-chaos-build');
2
+
3
+ module.exports = function (grunt) {
4
+ chaosBuild.loadTasks(grunt);
5
+
6
+ var config = chaosBuild.getConfig('javascripts', {
7
+ outputDirectory : 'javascripts/sea-modules',
8
+ gzip : 'all'
9
+ });
10
+ grunt.initConfig(config);
11
+
12
+ grunt.registerTask('write-manifest', function () {
13
+ var mapArr = grunt.config.get('md5map');
14
+ var family = config.family;
15
+ grunt.file.write('seajs-map.json', JSON.stringify(mapArr, null, '\t'));
16
+ });
17
+
18
+ grunt.registerTask('chaos-build', [
19
+ 'clean:dist', // delete dist direcotry first
20
+ 'transport:spm', // src/* -> .build/src/*
21
+ 'concat:relative', // .build/src/* -> .build/dist/*.js
22
+ 'concat:all',
23
+ 'uglify:js', // .build/dist/*.js -> .build/dist/*.js
24
+ 'md5:js', // .build/dist/*.js -> dist/*-md5.js
25
+ 'clean:spm',
26
+ 'spm-newline',
27
+ 'compress',
28
+ 'write-manifest'
29
+ ]);
30
+ };
@@ -0,0 +1,6 @@
1
+ seajs_path: <%= seajs_path %>
2
+ family: <%= family %>
3
+ output:
4
+ relative:
5
+ - application.js
6
+ all: []
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'seajs/rails/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "seajs-rails"
8
+ gem.version = Seajs::Rails::VERSION
9
+ gem.authors = ["edokeh"]
10
+ gem.email = ["edokeh@163.com"]
11
+ gem.description = %q{Integrates Sea.js into the Rails 3 Asset Pipeline.}
12
+ gem.summary = %q{This gem provides Sea.js support for your Rails 3 application.}
13
+ gem.homepage = "https://github.com/edokeh/seajs-rails"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "railties", ">= 3.1.1", "< 4.1"
21
+
22
+ gem.requirements << "spm-chaos-build is required for 'rake assets:precompile'. See https://github.com/edokeh/spm-chaos-build"
23
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seajs-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - edokeh
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.1
22
+ - - <
23
+ - !ruby/object:Gem::Version
24
+ version: '4.1'
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.1.1
33
+ - - <
34
+ - !ruby/object:Gem::Version
35
+ version: '4.1'
36
+ description: Integrates Sea.js into the Rails 3 Asset Pipeline.
37
+ email:
38
+ - edokeh@163.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitattributes
44
+ - .gitignore
45
+ - Gemfile
46
+ - LICENSE.txt
47
+ - README.md
48
+ - Rakefile
49
+ - app/helpers/seajs_helper.rb
50
+ - lib/seajs-rails.rb
51
+ - lib/seajs/rails.rb
52
+ - lib/seajs/rails/config.rb
53
+ - lib/seajs/rails/engine.rb
54
+ - lib/seajs/rails/version.rb
55
+ - lib/tasks/seajs-rails.rake
56
+ - lib/tasks/template/Gruntfile.js
57
+ - lib/tasks/template/seajs_config.yml
58
+ - seajs-rails.gemspec
59
+ homepage: https://github.com/edokeh/seajs-rails
60
+ licenses: []
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements:
78
+ - spm-chaos-build is required for 'rake assets:precompile'. See https://github.com/edokeh/spm-chaos-build
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.25
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: This gem provides Sea.js support for your Rails 3 application.
84
+ test_files: []