sprockets-almond 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a02d21d2f81e5dee524f3b3d01ce733f0fb4091b
4
+ data.tar.gz: a637aa35b6cd0a970cfe0fcc2e5407eb06df71fb
5
+ SHA512:
6
+ metadata.gz: 60ac9092259b964ca79566e4751ecccee39ea91ee35a77a0dc13e091f39909a25b1790de7af031322e07a7c9cb366972a5eeaa50c8a8785ca3739fcca478b520
7
+ data.tar.gz: 3935bc18b1c4ad48e3ba7d764472eff4a0ab24065fe365ec7637d8a015255f1387891f6940aa6b877837ee554517341f8bbdb105663a69287cdaad4a88654d45
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ This library adds AMD (via Almond) support to [Sprockets](https://github.com/sstephenson/sprockets).
2
+
3
+ ## What is AMD?
4
+
5
+ The AMD format is a way of encapsulating JavaScript libraries, ensuring they have to explicitly require and export properties they use.
6
+
7
+ ## This library
8
+
9
+ This library adds AMD support to Sprockets, so it can wrap up JavaScript files as modules, and serve them appropriately. This is done by using `define_module` to declare a new module. You should put your files under `app/assets/javascripts/modules/`.
10
+
11
+ ```
12
+ define_module(["backbone"], function(BB) {});
13
+ ```
14
+
15
+ Sprockets will then postprocess your JS and insert the correct logical asset name as module name.
16
+
17
+ ```
18
+ define("tickets/controller", ["backbone"], function(BB) {});
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ 1. Add `gem 'sprockets-almond'` to your `Gemfile`
24
+ 1. Add `//= require sprockets/almond ` to your `application.js`
25
+ 1. Require all the modules, e.g.: `//= require_tree ./models`
@@ -0,0 +1,421 @@
1
+ /**
2
+ * @license almond 0.2.9 Copyright (c) 2011-2014, The Dojo Foundation All Rights Reserved.
3
+ * Available via the MIT or new BSD license.
4
+ * see: http://github.com/jrburke/almond for details
5
+ */
6
+ //Going sloppy to avoid 'use strict' string cost, but strict practices should
7
+ //be followed.
8
+ /*jslint sloppy: true */
9
+ /*global setTimeout: false */
10
+
11
+ var requirejs, require, define;
12
+ (function (undef) {
13
+ var main, req, makeMap, handlers,
14
+ defined = {},
15
+ waiting = {},
16
+ config = {},
17
+ defining = {},
18
+ hasOwn = Object.prototype.hasOwnProperty,
19
+ aps = [].slice,
20
+ jsSuffixRegExp = /\.js$/;
21
+
22
+ function hasProp(obj, prop) {
23
+ return hasOwn.call(obj, prop);
24
+ }
25
+
26
+ /**
27
+ * Given a relative module name, like ./something, normalize it to
28
+ * a real name that can be mapped to a path.
29
+ * @param {String} name the relative name
30
+ * @param {String} baseName a real name that the name arg is relative
31
+ * to.
32
+ * @returns {String} normalized name
33
+ */
34
+ function normalize(name, baseName) {
35
+ var nameParts, nameSegment, mapValue, foundMap, lastIndex,
36
+ foundI, foundStarMap, starI, i, j, part,
37
+ baseParts = baseName && baseName.split("/"),
38
+ map = config.map,
39
+ starMap = (map && map['*']) || {};
40
+
41
+ //Adjust any relative paths.
42
+ if (name && name.charAt(0) === ".") {
43
+ //If have a base name, try to normalize against it,
44
+ //otherwise, assume it is a top-level require that will
45
+ //be relative to baseUrl in the end.
46
+ if (baseName) {
47
+ //Convert baseName to array, and lop off the last part,
48
+ //so that . matches that "directory" and not name of the baseName's
49
+ //module. For instance, baseName of "one/two/three", maps to
50
+ //"one/two/three.js", but we want the directory, "one/two" for
51
+ //this normalization.
52
+ baseParts = baseParts.slice(0, baseParts.length - 1);
53
+ name = name.split('/');
54
+ lastIndex = name.length - 1;
55
+
56
+ // Node .js allowance:
57
+ if (config.nodeIdCompat && jsSuffixRegExp.test(name[lastIndex])) {
58
+ name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, '');
59
+ }
60
+
61
+ name = baseParts.concat(name);
62
+
63
+ //start trimDots
64
+ for (i = 0; i < name.length; i += 1) {
65
+ part = name[i];
66
+ if (part === ".") {
67
+ name.splice(i, 1);
68
+ i -= 1;
69
+ } else if (part === "..") {
70
+ if (i === 1 && (name[2] === '..' || name[0] === '..')) {
71
+ //End of the line. Keep at least one non-dot
72
+ //path segment at the front so it can be mapped
73
+ //correctly to disk. Otherwise, there is likely
74
+ //no path mapping for a path starting with '..'.
75
+ //This can still fail, but catches the most reasonable
76
+ //uses of ..
77
+ break;
78
+ } else if (i > 0) {
79
+ name.splice(i - 1, 2);
80
+ i -= 2;
81
+ }
82
+ }
83
+ }
84
+ //end trimDots
85
+
86
+ name = name.join("/");
87
+ } else if (name.indexOf('./') === 0) {
88
+ // No baseName, so this is ID is resolved relative
89
+ // to baseUrl, pull off the leading dot.
90
+ name = name.substring(2);
91
+ }
92
+ }
93
+
94
+ //Apply map config if available.
95
+ if ((baseParts || starMap) && map) {
96
+ nameParts = name.split('/');
97
+
98
+ for (i = nameParts.length; i > 0; i -= 1) {
99
+ nameSegment = nameParts.slice(0, i).join("/");
100
+
101
+ if (baseParts) {
102
+ //Find the longest baseName segment match in the config.
103
+ //So, do joins on the biggest to smallest lengths of baseParts.
104
+ for (j = baseParts.length; j > 0; j -= 1) {
105
+ mapValue = map[baseParts.slice(0, j).join('/')];
106
+
107
+ //baseName segment has config, find if it has one for
108
+ //this name.
109
+ if (mapValue) {
110
+ mapValue = mapValue[nameSegment];
111
+ if (mapValue) {
112
+ //Match, update name to the new value.
113
+ foundMap = mapValue;
114
+ foundI = i;
115
+ break;
116
+ }
117
+ }
118
+ }
119
+ }
120
+
121
+ if (foundMap) {
122
+ break;
123
+ }
124
+
125
+ //Check for a star map match, but just hold on to it,
126
+ //if there is a shorter segment match later in a matching
127
+ //config, then favor over this star map.
128
+ if (!foundStarMap && starMap && starMap[nameSegment]) {
129
+ foundStarMap = starMap[nameSegment];
130
+ starI = i;
131
+ }
132
+ }
133
+
134
+ if (!foundMap && foundStarMap) {
135
+ foundMap = foundStarMap;
136
+ foundI = starI;
137
+ }
138
+
139
+ if (foundMap) {
140
+ nameParts.splice(0, foundI, foundMap);
141
+ name = nameParts.join('/');
142
+ }
143
+ }
144
+
145
+ return name;
146
+ }
147
+
148
+ function makeRequire(relName, forceSync) {
149
+ return function () {
150
+ //A version of a require function that passes a moduleName
151
+ //value for items that may need to
152
+ //look up paths relative to the moduleName
153
+ return req.apply(undef, aps.call(arguments, 0).concat([relName, forceSync]));
154
+ };
155
+ }
156
+
157
+ function makeNormalize(relName) {
158
+ return function (name) {
159
+ return normalize(name, relName);
160
+ };
161
+ }
162
+
163
+ function makeLoad(depName) {
164
+ return function (value) {
165
+ defined[depName] = value;
166
+ };
167
+ }
168
+
169
+ function callDep(name) {
170
+ if (hasProp(waiting, name)) {
171
+ var args = waiting[name];
172
+ delete waiting[name];
173
+ defining[name] = true;
174
+ main.apply(undef, args);
175
+ }
176
+
177
+ if (!hasProp(defined, name) && !hasProp(defining, name)) {
178
+ throw new Error('No ' + name);
179
+ }
180
+ return defined[name];
181
+ }
182
+
183
+ //Turns a plugin!resource to [plugin, resource]
184
+ //with the plugin being undefined if the name
185
+ //did not have a plugin prefix.
186
+ function splitPrefix(name) {
187
+ var prefix,
188
+ index = name ? name.indexOf('!') : -1;
189
+ if (index > -1) {
190
+ prefix = name.substring(0, index);
191
+ name = name.substring(index + 1, name.length);
192
+ }
193
+ return [prefix, name];
194
+ }
195
+
196
+ /**
197
+ * Makes a name map, normalizing the name, and using a plugin
198
+ * for normalization if necessary. Grabs a ref to plugin
199
+ * too, as an optimization.
200
+ */
201
+ makeMap = function (name, relName) {
202
+ var plugin,
203
+ parts = splitPrefix(name),
204
+ prefix = parts[0];
205
+
206
+ name = parts[1];
207
+
208
+ if (prefix) {
209
+ prefix = normalize(prefix, relName);
210
+ plugin = callDep(prefix);
211
+ }
212
+
213
+ //Normalize according
214
+ if (prefix) {
215
+ if (plugin && plugin.normalize) {
216
+ name = plugin.normalize(name, makeNormalize(relName));
217
+ } else {
218
+ name = normalize(name, relName);
219
+ }
220
+ } else {
221
+ name = normalize(name, relName);
222
+ parts = splitPrefix(name);
223
+ prefix = parts[0];
224
+ name = parts[1];
225
+ if (prefix) {
226
+ plugin = callDep(prefix);
227
+ }
228
+ }
229
+
230
+ //Using ridiculous property names for space reasons
231
+ return {
232
+ f: prefix ? prefix + '!' + name : name, //fullName
233
+ n: name,
234
+ pr: prefix,
235
+ p: plugin
236
+ };
237
+ };
238
+
239
+ function makeConfig(name) {
240
+ return function () {
241
+ return (config && config.config && config.config[name]) || {};
242
+ };
243
+ }
244
+
245
+ handlers = {
246
+ require: function (name) {
247
+ return makeRequire(name);
248
+ },
249
+ exports: function (name) {
250
+ var e = defined[name];
251
+ if (typeof e !== 'undefined') {
252
+ return e;
253
+ } else {
254
+ return (defined[name] = {});
255
+ }
256
+ },
257
+ module: function (name) {
258
+ return {
259
+ id: name,
260
+ uri: '',
261
+ exports: defined[name],
262
+ config: makeConfig(name)
263
+ };
264
+ }
265
+ };
266
+
267
+ main = function (name, deps, callback, relName) {
268
+ var cjsModule, depName, ret, map, i,
269
+ args = [],
270
+ callbackType = typeof callback,
271
+ usingExports;
272
+
273
+ //Use name if no relName
274
+ relName = relName || name;
275
+
276
+ //Call the callback to define the module, if necessary.
277
+ if (callbackType === 'undefined' || callbackType === 'function') {
278
+ //Pull out the defined dependencies and pass the ordered
279
+ //values to the callback.
280
+ //Default to [require, exports, module] if no deps
281
+ deps = !deps.length && callback.length ? ['require', 'exports', 'module'] : deps;
282
+ for (i = 0; i < deps.length; i += 1) {
283
+ map = makeMap(deps[i], relName);
284
+ depName = map.f;
285
+
286
+ //Fast path CommonJS standard dependencies.
287
+ if (depName === "require") {
288
+ args[i] = handlers.require(name);
289
+ } else if (depName === "exports") {
290
+ //CommonJS module spec 1.1
291
+ args[i] = handlers.exports(name);
292
+ usingExports = true;
293
+ } else if (depName === "module") {
294
+ //CommonJS module spec 1.1
295
+ cjsModule = args[i] = handlers.module(name);
296
+ } else if (hasProp(defined, depName) ||
297
+ hasProp(waiting, depName) ||
298
+ hasProp(defining, depName)) {
299
+ args[i] = callDep(depName);
300
+ } else if (map.p) {
301
+ map.p.load(map.n, makeRequire(relName, true), makeLoad(depName), {});
302
+ args[i] = defined[depName];
303
+ } else {
304
+ throw new Error(name + ' missing ' + depName);
305
+ }
306
+ }
307
+
308
+ ret = callback ? callback.apply(defined[name], args) : undefined;
309
+
310
+ if (name) {
311
+ //If setting exports via "module" is in play,
312
+ //favor that over return value and exports. After that,
313
+ //favor a non-undefined return value over exports use.
314
+ if (cjsModule && cjsModule.exports !== undef &&
315
+ cjsModule.exports !== defined[name]) {
316
+ defined[name] = cjsModule.exports;
317
+ } else if (ret !== undef || !usingExports) {
318
+ //Use the return value from the function.
319
+ defined[name] = ret;
320
+ }
321
+ }
322
+ } else if (name) {
323
+ //May just be an object definition for the module. Only
324
+ //worry about defining if have a module name.
325
+ defined[name] = callback;
326
+ }
327
+ };
328
+
329
+ requirejs = require = req = function (deps, callback, relName, forceSync, alt) {
330
+ if (typeof deps === "string") {
331
+ if (handlers[deps]) {
332
+ //callback in this case is really relName
333
+ return handlers[deps](callback);
334
+ }
335
+ //Just return the module wanted. In this scenario, the
336
+ //deps arg is the module name, and second arg (if passed)
337
+ //is just the relName.
338
+ //Normalize module name, if it contains . or ..
339
+ return callDep(makeMap(deps, callback).f);
340
+ } else if (!deps.splice) {
341
+ //deps is a config object, not an array.
342
+ config = deps;
343
+ if (config.deps) {
344
+ req(config.deps, config.callback);
345
+ }
346
+ if (!callback) {
347
+ return;
348
+ }
349
+
350
+ if (callback.splice) {
351
+ //callback is an array, which means it is a dependency list.
352
+ //Adjust args if there are dependencies
353
+ deps = callback;
354
+ callback = relName;
355
+ relName = null;
356
+ } else {
357
+ deps = undef;
358
+ }
359
+ }
360
+
361
+ //Support require(['a'])
362
+ callback = callback || function () {};
363
+
364
+ //If relName is a function, it is an errback handler,
365
+ //so remove it.
366
+ if (typeof relName === 'function') {
367
+ relName = forceSync;
368
+ forceSync = alt;
369
+ }
370
+
371
+ //Simulate async callback;
372
+ if (forceSync) {
373
+ main(undef, deps, callback, relName);
374
+ } else {
375
+ //Using a non-zero value because of concern for what old browsers
376
+ //do, and latest browsers "upgrade" to 4 if lower value is used:
377
+ //http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#dom-windowtimers-settimeout:
378
+ //If want a value immediately, use require('id') instead -- something
379
+ //that works in almond on the global level, but not guaranteed and
380
+ //unlikely to work in other AMD implementations.
381
+ setTimeout(function () {
382
+ main(undef, deps, callback, relName);
383
+ }, 4);
384
+ }
385
+
386
+ return req;
387
+ };
388
+
389
+ /**
390
+ * Just drops the config on the floor, but returns req in case
391
+ * the config return value is used.
392
+ */
393
+ req.config = function (cfg) {
394
+ return req(cfg);
395
+ };
396
+
397
+ /**
398
+ * Expose module registry for debugging and tooling
399
+ */
400
+ requirejs._defined = defined;
401
+
402
+ define = function (name, deps, callback) {
403
+
404
+ //This module may not have dependencies
405
+ if (!deps.splice) {
406
+ //deps is not an array, so probably means
407
+ //an object literal or factory function for
408
+ //the value. Adjust args.
409
+ callback = deps;
410
+ deps = [];
411
+ }
412
+
413
+ if (!hasProp(defined, name) && !hasProp(waiting, name)) {
414
+ waiting[name] = [name, deps, callback];
415
+ }
416
+ };
417
+
418
+ define.amd = {
419
+ jQuery: true
420
+ };
421
+ }());
@@ -0,0 +1 @@
1
+ require 'sprockets/define_module'
@@ -0,0 +1,29 @@
1
+ require 'sprockets'
2
+ require 'tilt'
3
+
4
+ module Sprockets
5
+ class DefineModule < Sprockets::Processor
6
+ class << self
7
+ attr_accessor :default_base_path
8
+ end
9
+
10
+ attr_reader :base_path
11
+
12
+ self.default_base_path = 'modules/'
13
+
14
+ def prepare
15
+ @base_path = self.class.default_base_path
16
+ end
17
+
18
+ def evaluate context, locals
19
+ if n = context.logical_path.match(/^#{base_path}([\w\/]+)/)
20
+ data.gsub! /define_module\s*\(/, "define(\"#{n[1]}\", "
21
+ else
22
+ data
23
+ end
24
+ end
25
+ end
26
+
27
+ register_postprocessor 'application/javascript', DefineModule
28
+ append_path File.expand_path('../../../assets', __FILE__)
29
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprockets-almond
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sven Winkler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sprockets
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Adds AMD support and Almond to the asset pipeline
70
+ email:
71
+ - svenwin@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - README.md
77
+ - lib/sprockets/define_module.rb
78
+ - lib/sprockets-almond.rb
79
+ - assets/sprockets/almond.js
80
+ homepage: https://github.com/svenwin/sprockets-almond
81
+ licenses: []
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: 1.9.3
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.1.11
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Adds AMD support and Almond to the asset pipeline
103
+ test_files: []