jsrequire 0.0.5
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/.document +5 -0
- data/.gitignore +26 -0
- data/LICENSE +21 -0
- data/README.rdoc +50 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/jsrequire.gemspec +66 -0
- data/lib/jsrequire.rb +177 -0
- data/test/fixtures/different-place/b.js +2 -0
- data/test/fixtures/javascripts/a.js +2 -0
- data/test/fixtures/javascripts/c.js +3 -0
- data/test/fixtures/javascripts/file.with.dot.js +0 -0
- data/test/fixtures/javascripts/namespace/a.js +0 -0
- data/test/fixtures/javascripts/namespace/json_reader.js +0 -0
- data/test/fixtures/javascripts/namespace/norequire.js +0 -0
- data/test/fixtures/javascripts/norequire.js +0 -0
- data/test/fixtures/javascripts/norequire.with.dot.in.name.js +0 -0
- data/test/fixtures/javascripts/require_filename_with_dot.js +1 -0
- data/test/fixtures/javascripts/require_filename_with_js.js +1 -0
- data/test/fixtures/javascripts/require_namespaced_file.js +3 -0
- data/test/fixtures/javascripts/require_non_existing_file.js +2 -0
- data/test/fixtures/javascripts/requirecss.js +2 -0
- data/test/fixtures/stylesheets/style.css +0 -0
- data/test/helper.rb +12 -0
- data/test/test_jsrequire.rb +155 -0
- metadata +81 -0
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
*.bundle
|
2
|
+
*.o
|
3
|
+
*.orig
|
4
|
+
*.rb~
|
5
|
+
*.so
|
6
|
+
*.swp
|
7
|
+
*.tmproj
|
8
|
+
*~
|
9
|
+
.DS_Store
|
10
|
+
.idea
|
11
|
+
.loadpath
|
12
|
+
.project
|
13
|
+
/data
|
14
|
+
/index
|
15
|
+
config/database.yml
|
16
|
+
coverage
|
17
|
+
index/*
|
18
|
+
log/*.log
|
19
|
+
log/*.pid
|
20
|
+
log/mongrel.pid
|
21
|
+
Makefile
|
22
|
+
mkmf.log
|
23
|
+
out
|
24
|
+
pkg/*
|
25
|
+
tmp
|
26
|
+
tmp.pot~
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2010 aekym
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
= jsrequire
|
2
|
+
|
3
|
+
A small lib to add a require functionality to JavaScript files. On top of a file add something like:
|
4
|
+
|
5
|
+
/* require foo/bar */
|
6
|
+
|
7
|
+
This includes the file foo/bar.js into the file. Dependencies are resolved in right order of require statments over all files.
|
8
|
+
|
9
|
+
## controller
|
10
|
+
|
11
|
+
# resolve js dependencies
|
12
|
+
|
13
|
+
path_to_js = "/absolute/path/to/javascripts"
|
14
|
+
|
15
|
+
jsrequire = JsRequire.new
|
16
|
+
dependencies = jsrequire.resolve_dependencies(File.join(path_to_js, "foo.js"))
|
17
|
+
|
18
|
+
|
19
|
+
# rewrite js paths for web usage
|
20
|
+
|
21
|
+
@javascripts = JsRequire::web_path_helper(dependencies[:javascripts],
|
22
|
+
path_to_js => "/public/javascripts"
|
23
|
+
})
|
24
|
+
|
25
|
+
@stylesheets = dependencies[:stylesheets]
|
26
|
+
|
27
|
+
|
28
|
+
## haml view
|
29
|
+
|
30
|
+
- for css in @stylesheets
|
31
|
+
%link{:rel => "stylesheet", :href => css, :type => "text/css"}
|
32
|
+
|
33
|
+
- for js in @javascripts
|
34
|
+
%script{:type => "text/javascript", :src => js}
|
35
|
+
|
36
|
+
|
37
|
+
== Note on Patches/Pull Requests
|
38
|
+
|
39
|
+
* Fork the project.
|
40
|
+
* Make your feature addition or bug fix.
|
41
|
+
* Add tests for it. This is important so I don't break it in a
|
42
|
+
future version unintentionally.
|
43
|
+
* Commit, do not mess with rakefile, version, or history.
|
44
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
45
|
+
* Send me a pull request. Bonus points for topic branches.
|
46
|
+
|
47
|
+
== Copyright
|
48
|
+
|
49
|
+
Copyright (c) 2010 aekym. See LICENSE for details.
|
50
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "jsrequire"
|
8
|
+
gem.summary = %Q{Organizes requirements of assets in JavaScript files}
|
9
|
+
gem.description = %Q{Organizes requirements of assets in JavaScript files, resolved dependencies of js files and helps include depending css files.}
|
10
|
+
gem.email = "me@aekym.com"
|
11
|
+
gem.homepage = "http://github.com/aekym/jsrequire"
|
12
|
+
gem.authors = ["aekym"]
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
21
|
+
test.libs << 'lib' << 'test'
|
22
|
+
test.pattern = 'test/**/test_*.rb'
|
23
|
+
test.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'rcov/rcovtask'
|
28
|
+
Rcov::RcovTask.new do |test|
|
29
|
+
test.libs << 'test'
|
30
|
+
test.pattern = 'test/**/test_*.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
rescue LoadError
|
34
|
+
task :rcov do
|
35
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
task :test => :check_dependencies
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'rake/rdoctask'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "jsrequire #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
52
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.5
|
data/jsrequire.gemspec
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{jsrequire}
|
8
|
+
s.version = "0.0.5"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["aekym"]
|
12
|
+
s.date = %q{2010-02-05}
|
13
|
+
s.description = %q{Organizes requirements of assets in JavaScript files, resolved dependencies of js files and helps include depending css files.}
|
14
|
+
s.email = %q{me@aekym.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"jsrequire.gemspec",
|
27
|
+
"lib/jsrequire.rb",
|
28
|
+
"test/fixtures/different-place/b.js",
|
29
|
+
"test/fixtures/javascripts/a.js",
|
30
|
+
"test/fixtures/javascripts/c.js",
|
31
|
+
"test/fixtures/javascripts/file.with.dot.js",
|
32
|
+
"test/fixtures/javascripts/namespace/a.js",
|
33
|
+
"test/fixtures/javascripts/namespace/json_reader.js",
|
34
|
+
"test/fixtures/javascripts/namespace/norequire.js",
|
35
|
+
"test/fixtures/javascripts/norequire.js",
|
36
|
+
"test/fixtures/javascripts/norequire.with.dot.in.name.js",
|
37
|
+
"test/fixtures/javascripts/require_filename_with_dot.js",
|
38
|
+
"test/fixtures/javascripts/require_filename_with_js.js",
|
39
|
+
"test/fixtures/javascripts/require_namespaced_file.js",
|
40
|
+
"test/fixtures/javascripts/require_non_existing_file.js",
|
41
|
+
"test/fixtures/javascripts/requirecss.js",
|
42
|
+
"test/fixtures/stylesheets/style.css",
|
43
|
+
"test/helper.rb",
|
44
|
+
"test/test_jsrequire.rb"
|
45
|
+
]
|
46
|
+
s.homepage = %q{http://github.com/aekym/jsrequire}
|
47
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
48
|
+
s.require_paths = ["lib"]
|
49
|
+
s.rubygems_version = %q{1.3.5}
|
50
|
+
s.summary = %q{Organizes requirements of assets in JavaScript files}
|
51
|
+
s.test_files = [
|
52
|
+
"test/helper.rb",
|
53
|
+
"test/test_jsrequire.rb"
|
54
|
+
]
|
55
|
+
|
56
|
+
if s.respond_to? :specification_version then
|
57
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
58
|
+
s.specification_version = 3
|
59
|
+
|
60
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
61
|
+
else
|
62
|
+
end
|
63
|
+
else
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
data/lib/jsrequire.rb
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
class JsRequire
|
5
|
+
|
6
|
+
class FileNotFoundInLoadpath < ArgumentError; end
|
7
|
+
|
8
|
+
|
9
|
+
def initialize(loadpaths = nil)
|
10
|
+
@extract_loadpaths = []
|
11
|
+
|
12
|
+
loadpaths = [loadpaths] unless loadpaths.is_a?(Array)
|
13
|
+
@additional_loadpaths = JsRequire::normalize_filepaths(loadpaths.compact)
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
# resolve dependencies of js input files
|
18
|
+
#
|
19
|
+
# returns a hash with js and css dependencies
|
20
|
+
#
|
21
|
+
# js files are returned with absolute filepaths,
|
22
|
+
# css files not. css files are returned as given
|
23
|
+
# by the parsed require statement.
|
24
|
+
#
|
25
|
+
# e.g.
|
26
|
+
#
|
27
|
+
# {
|
28
|
+
# :javascripts => ["/foo/bar.js"],
|
29
|
+
# :stylesheets => ["style.css"]
|
30
|
+
# }
|
31
|
+
#
|
32
|
+
def resolve_dependencies(files)
|
33
|
+
@stylesheets = Hash.new { |h,k| h[k] = [] }
|
34
|
+
@extract_loadpaths = extract_loadpaths(files)
|
35
|
+
|
36
|
+
js = extract_dependencies_recursive(JsRequire::normalize_filepaths(files))
|
37
|
+
|
38
|
+
{
|
39
|
+
:javascripts => js,
|
40
|
+
:stylesheets => @stylesheets.map { |k,v| v }.flatten.uniq.sort
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
# convert absolute filepaths to relatives by
|
46
|
+
# cutting the absolute path to the webroot
|
47
|
+
#
|
48
|
+
# returns the webroot relative filepaths
|
49
|
+
#
|
50
|
+
# web_path_helper(["/foo/bar.js"], {"/foo" => "/javascripts"})
|
51
|
+
# => ["/javascripts/bar.js"]
|
52
|
+
#
|
53
|
+
# @param webroots: array of strings to remove the prefix path
|
54
|
+
# or a hash to replace with defined string
|
55
|
+
#
|
56
|
+
def self.web_path_helper(files, webroots)
|
57
|
+
webroots = [webroots] unless webroots.is_a?(Enumerable)
|
58
|
+
|
59
|
+
files.map do |f|
|
60
|
+
rel_file = nil
|
61
|
+
webroots.each do |wr, replacement|
|
62
|
+
wr = normalize_filepath(wr)
|
63
|
+
rel_file = f.sub(/^#{Regexp.escape wr}/, replacement || '')
|
64
|
+
break if rel_file != f
|
65
|
+
end
|
66
|
+
rel_file || f
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
# builds namespaces from script files by pathnames
|
72
|
+
# when the <namespace_prefix> is found in path.
|
73
|
+
#
|
74
|
+
# e.g.
|
75
|
+
#
|
76
|
+
# namespace_helper(["/foo/bar/quux/file1.js", "/foo/bar/baz/file2.js"], "bar")
|
77
|
+
# => ["bar.baz", "bar.quux"]
|
78
|
+
#
|
79
|
+
# Interessting for ExtJs#namespace
|
80
|
+
#
|
81
|
+
def self.namespace_helper(files, namespace_prefix)
|
82
|
+
files.inject([]) do |arr,js|
|
83
|
+
if js =~ /\/(#{namespace_prefix}\/.+)$/
|
84
|
+
file = File.dirname($1).gsub("/", ".")
|
85
|
+
arr << file
|
86
|
+
end
|
87
|
+
arr
|
88
|
+
end.sort.uniq
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
protected
|
94
|
+
|
95
|
+
|
96
|
+
def self.normalize_filepath(file)
|
97
|
+
File.expand_path(file)
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
def self.normalize_filepaths(files)
|
102
|
+
files.map { |f| normalize_filepath(f) }
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
def extract_loadpaths(files)
|
107
|
+
JsRequire::normalize_filepaths(files.map { |f| File.dirname(f) }.uniq)
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
def is_file?(filename)
|
113
|
+
File.file?(filename) && File.readable?(filename) && filename =~ /^\//
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
def find_file(filename)
|
118
|
+
return filename if is_file?(filename)
|
119
|
+
|
120
|
+
loadpaths = @extract_loadpaths + @additional_loadpaths
|
121
|
+
loadpaths.each do |path|
|
122
|
+
file = File.expand_path(filename, path)
|
123
|
+
return file if is_file?(file)
|
124
|
+
end
|
125
|
+
|
126
|
+
# fallback for namespaced files
|
127
|
+
if filename =~ /\./
|
128
|
+
loadpaths.each do |path|
|
129
|
+
ext = File.extname(filename)
|
130
|
+
file = File.expand_path(filename.gsub(/#{ext}$/, '').gsub('.', '/') + ext, path)
|
131
|
+
return file if is_file?(file)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
raise FileNotFoundInLoadpath, "File '#{filename}' not found in loadpaths '#{loadpaths.join("', '")}'."
|
136
|
+
end
|
137
|
+
|
138
|
+
|
139
|
+
def extract_dependencies(filename)
|
140
|
+
is_require = true
|
141
|
+
js = []
|
142
|
+
|
143
|
+
File.open(filename, "r") do |f|
|
144
|
+
begin
|
145
|
+
case line = f.gets
|
146
|
+
when /^\s*\/\*\s*require\s*(\S+)\s*\*\/\s*$/
|
147
|
+
js << "#{$1}.js"
|
148
|
+
when /^\s*\/\*\s*css\s*(\S+)\s*\*\/\s*$/
|
149
|
+
@stylesheets[filename] << $1 + ".css"
|
150
|
+
else
|
151
|
+
is_require = false
|
152
|
+
end
|
153
|
+
|
154
|
+
end while is_require
|
155
|
+
end
|
156
|
+
|
157
|
+
js.uniq.map { |f| find_file(f) }
|
158
|
+
end
|
159
|
+
|
160
|
+
|
161
|
+
def extract_dependencies_recursive(files, included_files = [])
|
162
|
+
js = []
|
163
|
+
files.each do |f|
|
164
|
+
file = find_file(f)
|
165
|
+
|
166
|
+
unless included_files.include?(file)
|
167
|
+
js += extract_dependencies_recursive(extract_dependencies(file), js + [file] + included_files)
|
168
|
+
js << file
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
js
|
173
|
+
end
|
174
|
+
|
175
|
+
|
176
|
+
end
|
177
|
+
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
/* require file.with.dot */
|
@@ -0,0 +1 @@
|
|
1
|
+
/* require namespace.json_reader */
|
File without changes
|
data/test/helper.rb
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper.rb'
|
2
|
+
|
3
|
+
class TestJsRequire < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@fixtures_dir = File.join(File.dirname(__FILE__), "fixtures")
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
def assert_requires(file, javascripts, stylesheets = [])
|
11
|
+
@jsrequire = JsRequire.new
|
12
|
+
data = @jsrequire.resolve_dependencies(File.join(@fixtures_dir + "/javascripts", file))
|
13
|
+
|
14
|
+
expect = [file, javascripts].flatten.map { |js| File.expand_path(js, @fixtures_dir + "/javascripts") }.sort
|
15
|
+
assert_equal expect, data[:javascripts].sort
|
16
|
+
assert_equal stylesheets, data[:stylesheets]
|
17
|
+
end
|
18
|
+
|
19
|
+
context "#resolve_dependencies" do
|
20
|
+
|
21
|
+
should "return the basic hash with empty results" do
|
22
|
+
@jsrequire = JsRequire.new
|
23
|
+
data = @jsrequire.resolve_dependencies([])
|
24
|
+
|
25
|
+
assert !data.include?(:bernd)
|
26
|
+
|
27
|
+
assert data.include?(:javascripts)
|
28
|
+
assert data.include?(:stylesheets)
|
29
|
+
end
|
30
|
+
|
31
|
+
should "require nothing" do
|
32
|
+
assert_requires("norequire.js", [])
|
33
|
+
end
|
34
|
+
|
35
|
+
should "require recursive dependencies" do
|
36
|
+
file = File.join(@fixtures_dir, "javascripts/a.js")
|
37
|
+
|
38
|
+
@jsrequire = JsRequire.new File.join(@fixtures_dir, "different-place")
|
39
|
+
data = @jsrequire.resolve_dependencies(file)
|
40
|
+
|
41
|
+
expect = ["javascripts/norequire.js", "different-place/b.js", "javascripts/a.js"].map { |js| File.expand_path(js, @fixtures_dir) }
|
42
|
+
assert_equal expect, data[:javascripts]
|
43
|
+
end
|
44
|
+
|
45
|
+
should "require recursive dependencies in right order" do
|
46
|
+
files = ["different-place/b.js", "javascripts/c.js"].map { |f| File.join(@fixtures_dir, f) }
|
47
|
+
|
48
|
+
@jsrequire = JsRequire.new File.join(@fixtures_dir, "different-place")
|
49
|
+
data = @jsrequire.resolve_dependencies(files)
|
50
|
+
|
51
|
+
expect = ["javascripts/norequire.js", "different-place/b.js", "javascripts/a.js", "javascripts/c.js"].map { |js| File.expand_path(js, @fixtures_dir) }
|
52
|
+
assert_equal expect, data[:javascripts]
|
53
|
+
end
|
54
|
+
|
55
|
+
should "require one css file" do
|
56
|
+
assert_requires("requirecss.js", [], ["style.css"])
|
57
|
+
end
|
58
|
+
|
59
|
+
should "resolve dependencies from loadpath with source file from different place" do
|
60
|
+
loadpath = File.join(@fixtures_dir, "javascripts")
|
61
|
+
@jsrequire = JsRequire.new(loadpath)
|
62
|
+
|
63
|
+
source_file = File.join(@fixtures_dir, "different-place/b.js")
|
64
|
+
dep = @jsrequire.resolve_dependencies(source_file)
|
65
|
+
|
66
|
+
files = ["javascripts/norequire.js", "different-place/b.js"].map { |f| File.expand_path(f, @fixtures_dir) }
|
67
|
+
assert_equal files, dep[:javascripts]
|
68
|
+
end
|
69
|
+
|
70
|
+
should "not be able to resolve dependent files because of missing loadpath" do
|
71
|
+
@jsrequire = JsRequire.new
|
72
|
+
source_file = File.join(@fixtures_dir, "different-place/b.js")
|
73
|
+
required_file_found = true
|
74
|
+
|
75
|
+
begin
|
76
|
+
dep = @jsrequire.resolve_dependencies(source_file)
|
77
|
+
rescue JsRequire::FileNotFoundInLoadpath
|
78
|
+
required_file_found = false
|
79
|
+
end
|
80
|
+
|
81
|
+
assert !required_file_found, "load of required files should fail"
|
82
|
+
end
|
83
|
+
|
84
|
+
should "throw an exception when requiring a non existing file" do
|
85
|
+
assert_raises JsRequire::FileNotFoundInLoadpath do
|
86
|
+
assert_requires("require_non_existing_file.js", [])
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
should "be able to require a file via namespaces" do
|
91
|
+
assert_requires("require_namespaced_file.js", "namespace/a.js")
|
92
|
+
end
|
93
|
+
|
94
|
+
should "be able to require a file with dots in filename" do
|
95
|
+
assert_requires("require_filename_with_dot.js", "file.with.dot.js")
|
96
|
+
end
|
97
|
+
|
98
|
+
should "be able to require namespaced files starting with 'js'" do
|
99
|
+
assert_requires("require_filename_with_js.js", "namespace/json_reader.js")
|
100
|
+
end
|
101
|
+
|
102
|
+
should "check loadpaths" do
|
103
|
+
additional_loadpath = File.join(@fixtures_dir, "different-place")
|
104
|
+
extracted_loadpath = File.join(@fixtures_dir, "javascripts")
|
105
|
+
files = ["norequire.js", "requirecss.js"].map { |f| File.join(extracted_loadpath, f) }
|
106
|
+
@jsrequire = JsRequire.new(additional_loadpath)
|
107
|
+
@jsrequire.resolve_dependencies(files)
|
108
|
+
|
109
|
+
extracted_loadpaths = [File.expand_path(extracted_loadpath)]
|
110
|
+
assert_equal extracted_loadpaths, @jsrequire.instance_eval("@extract_loadpaths")
|
111
|
+
|
112
|
+
additional_loadpaths = [File.expand_path(additional_loadpath)]
|
113
|
+
assert_equal additional_loadpaths, @jsrequire.instance_eval("@additional_loadpaths")
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
context "helper methods" do
|
121
|
+
|
122
|
+
should "#web_path_helper" do
|
123
|
+
loadpath = File.join(@fixtures_dir, "javascripts")
|
124
|
+
@jsrequire = JsRequire.new(loadpath)
|
125
|
+
|
126
|
+
source_file = File.join(@fixtures_dir, "different-place/b.js")
|
127
|
+
dep = @jsrequire.resolve_dependencies(source_file)
|
128
|
+
|
129
|
+
absolute_files = ["javascripts/norequire.js", "different-place/b.js"].map { |f| File.expand_path(f, @fixtures_dir) }
|
130
|
+
assert_equal absolute_files, dep[:javascripts]
|
131
|
+
|
132
|
+
relative_files = ["/javascripts/norequire.js", "/different-place/b.js"]
|
133
|
+
assert_equal relative_files, JsRequire::web_path_helper(dep[:javascripts], @fixtures_dir)
|
134
|
+
|
135
|
+
relative_files = ["/javascripts/norequire.js", "/different-place/b.js"]
|
136
|
+
assert_equal relative_files, JsRequire::web_path_helper(dep[:javascripts], [@fixtures_dir])
|
137
|
+
|
138
|
+
other_relative_files = ["/bernd/norequire.js", "/baerbel/b.js"]
|
139
|
+
assert_equal other_relative_files, JsRequire::web_path_helper(dep[:javascripts], {
|
140
|
+
File.join(@fixtures_dir, "javascripts") => "/bernd",
|
141
|
+
File.join(@fixtures_dir, "different-place") => "/baerbel"
|
142
|
+
})
|
143
|
+
end
|
144
|
+
|
145
|
+
should "#namespace_helper" do
|
146
|
+
js = ["/platform/public/javascripts/si/module/Hastenichtgesehen.js", "/platform/public/javascripts/si/desktop/Wurstwaren.js", "/platform/public/javascripts/si/applet/GeradNeu.js"]
|
147
|
+
ns = ["si.applet", "si.desktop", "si.module"]
|
148
|
+
|
149
|
+
assert_equal ns, JsRequire::namespace_helper(js, "si")
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsrequire
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- aekym
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-05 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Organizes requirements of assets in JavaScript files, resolved dependencies of js files and helps include depending css files.
|
17
|
+
email: me@aekym.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- jsrequire.gemspec
|
33
|
+
- lib/jsrequire.rb
|
34
|
+
- test/fixtures/different-place/b.js
|
35
|
+
- test/fixtures/javascripts/a.js
|
36
|
+
- test/fixtures/javascripts/c.js
|
37
|
+
- test/fixtures/javascripts/file.with.dot.js
|
38
|
+
- test/fixtures/javascripts/namespace/a.js
|
39
|
+
- test/fixtures/javascripts/namespace/json_reader.js
|
40
|
+
- test/fixtures/javascripts/namespace/norequire.js
|
41
|
+
- test/fixtures/javascripts/norequire.js
|
42
|
+
- test/fixtures/javascripts/norequire.with.dot.in.name.js
|
43
|
+
- test/fixtures/javascripts/require_filename_with_dot.js
|
44
|
+
- test/fixtures/javascripts/require_filename_with_js.js
|
45
|
+
- test/fixtures/javascripts/require_namespaced_file.js
|
46
|
+
- test/fixtures/javascripts/require_non_existing_file.js
|
47
|
+
- test/fixtures/javascripts/requirecss.js
|
48
|
+
- test/fixtures/stylesheets/style.css
|
49
|
+
- test/helper.rb
|
50
|
+
- test/test_jsrequire.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/aekym/jsrequire
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options:
|
57
|
+
- --charset=UTF-8
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.3.5
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Organizes requirements of assets in JavaScript files
|
79
|
+
test_files:
|
80
|
+
- test/helper.rb
|
81
|
+
- test/test_jsrequire.rb
|