7
7
*
8
8
* @license {PUKE-RIGHTS-LICENSE}.
9
9
* @copyright {PUKE-RIGHTS-COPYRIGHT}
10
- * @name {PUKE-GIT-ROOT }/jsboot/ gister/packman.js{PUKE-GIT-REVISION}
10
+ * @name {PUKE-GIT-ROOT }/gister/packman.js{PUKE-GIT-REVISION}
11
11
*/
12
12
13
-
14
- // Guarantee that require is here
15
-
16
- /*
17
- Inline module declarations
18
-
19
- // Define sources
20
- gister.module('Bar').at('bar.js');
21
- gister.module('Foo').at('foo.js');
22
-
23
- Modules can be declared inline:
24
-
25
- gister.module('Foo', function() {
26
- export let x = 42;
27
- });
28
-
29
- gister.import('bar.js').as('Bar');
30
- gister.import({'y': 'localName'}).from('Bar');
31
- gister.import('y').from("bar.js");
32
-
33
- External module load
34
-
35
- Modules can be loaded from external resources:
36
-
37
- // Variant A: import URL syntax
38
-
39
- // Variant B: module = syntax
40
- It is not necessary to bind a module to a local name, if the programmer simply wishes to import
41
- directly from the module:
42
-
43
- */
44
-
45
-
46
- /*
47
- gister.use('Module').as('Toto').from('Thing');
48
- gister.use('Module').as('Toto').from('Thing');
49
- gister.add('Toto1234').as('TotoVariableName');
50
- gister.pack('Stuffy.Thing', function(){
51
- });
52
-
53
-
54
- define = function(name, deps, callback) {
55
- require = req = function(deps, callback, relName, forceSync, alt) {
56
-
57
- */
58
-
59
13
/*global window*/
60
14
61
- if ( typeof jsBoot == 'undefined' )
62
- var jsBoot = { } ;
63
-
64
15
( function ( globalObject ) {
65
16
'use strict' ;
66
17
67
- // Adapted from JSON3 (credit Oyvind Sean Kinsey)
68
- // Detect the "define" function exposed by asynchronous module loaders
69
- // var isLoader = typeof define === 'function' && define.amd;
70
- // var internalObj = typeof exports == 'object' && exports;
71
-
72
18
var toUse = [ ] ;
73
19
var lastUse ;
74
20
var toAdd = [ ] ;
@@ -81,41 +27,17 @@ if (typeof jsBoot == 'undefined')
81
27
}
82
28
if ( lastAdd ) {
83
29
if ( ! lastAdd . name )
84
- throw new Error ( 'NEED_A_NAME' , ' Trying to bind something without name') ;
30
+ throw new Error ( 'NEED_A_NAME: Trying to bind " something" without any name' ) ;
85
31
toAdd . push ( lastAdd ) ;
86
32
lastAdd = null ;
87
33
}
88
34
} ;
89
35
90
- this . use = function ( a , optional ) {
91
- flush ( ) ;
92
- lastUse = { module : a , name : a . split ( '.' ) . pop ( ) , optional : optional } ;
93
- return this ;
94
- } ;
95
-
96
- this . add = function ( a , optional ) {
97
- if ( ( a === undefined ) && ! optional )
98
- throw new Error ( 'UNDEFINED' , 'Requesting something local that is undefined' ) ;
99
- flush ( ) ;
100
- lastAdd = { value : a } ;
101
- return this ;
102
- } ;
103
-
104
- this . as = function ( a ) {
105
- if ( lastUse )
106
- lastUse . name = a ;
107
- else if ( lastAdd )
108
- lastAdd . name = a ;
109
- flush ( ) ;
110
- } ;
111
-
112
-
113
-
114
36
var simplePull = function ( glob , name , optional ) {
115
37
name . split ( '.' ) . forEach ( function ( fragment ) {
116
38
if ( ! glob || ! ( fragment in glob ) )
117
39
if ( ! optional )
118
- throw new Error ( 'MISSING' , ' Trying to require something that doesn\'t exist: ' + name ) ;
40
+ throw new Error ( 'MISSING: Trying to require something that doesn\'t exist' + name ) ;
119
41
else
120
42
return ( glob = undefined ) ;
121
43
glob = glob [ fragment ] ;
@@ -135,9 +57,7 @@ if (typeof jsBoot == 'undefined')
135
57
return ret ;
136
58
} ;
137
59
138
- // var amdHack = {};
139
-
140
- this . pack = function ( name , factory ) {
60
+ var packer = function ( ) {
141
61
// Close anything going on
142
62
flush ( ) ;
143
63
// Dereference requested stuff and flush it
@@ -150,149 +70,52 @@ if (typeof jsBoot == 'undefined')
150
70
var api = { } ;
151
71
localAdd . forEach ( function ( item ) {
152
72
if ( item . name in api )
153
- throw new Error ( 'ALREADY_DEFINED' , ' You are shadowing ' + api [ item . name ] + ' with ' +
73
+ throw new Error ( 'ALREADY_DEFINED: You are shadowing ' + api [ item . name ] + ' with ' +
154
74
item + ' for name ' + item . name ) ;
155
75
api [ item . name ] = item . value ;
156
76
} ) ;
157
77
158
- // If AMD pattern
159
- /*
160
- if (isLoader || internalObj) {
161
- // Get dependencies names
162
- var deps = [];
163
- var udeps = [];
164
- var localNames = [];
165
- localUse.forEach(function(item) {
166
- var k = item.module.replace(/\./g, '/').split('/');
167
- var rest = [];
168
- while(!(k.join('/') in amdHack) && k.length){
169
- rest.unshift(k.pop());
170
- console.warn("******** unshifting", JSON.stringify(rest));
171
- }
172
- if(!k.length)
173
- throw new Error('UNRESOLVED', 'Requested dep doesnt pan out');
174
- deps.push(k.join('/'));
175
- udeps.push(rest);
176
-
177
- localNames.push(item.name);
178
- });
179
-
180
- // Fuck the identifier as well AMD style
181
- name = name.replace(/\./g, '/');
182
- // deps.unshift(name);
183
-
184
- if (isLoader) {
185
- var module;
186
- var there = (name in amdHack);
187
- amdHack[name] = module = there ? amdHack[name] : {};
188
- if(!there){
189
- console.warn("defininnnnnnnnng", name);
190
- define(name, amdHack[name]);
191
- }
192
-
193
- console.warn("getting", JSON.stringify(deps), JSON.stringify(udeps));
194
- require(deps, function(){
195
- console.warn("Actually defining/requiring with no name", name);
196
- var args = Array.prototype.slice.call(arguments);
197
- localNames.forEach(function(key, idx){
198
- if (key in api)
199
- throw new Error('ALREADY_DEFINED', 'You are shadowing ' + key);
200
- api[key] = args.shift();
201
- while(udeps[idx].length){
202
- console.warn("----------> raising the bar");
203
- api[key] = api[key][udeps[idx].shift()];
204
- }
205
- if(api[key] === undefined)
206
- throw new Error('MISSING', 'Trying to require something that doesn\'t exist');
207
- });
208
- // udeps
209
- console.warn("getting udeps", udeps);
210
-
211
- var sub = {};
212
- sub = factory.apply(sub, [api]) || sub;
213
- Object.keys(sub).forEach(function(key){
214
- module[key] = sub[key];
215
- define(name + '/' + key, module[key]);
216
- });
217
- });
218
-
219
- // require(deps, function(mod){
220
- // var module = mod || {};
221
- // var args = Array.prototype.slice.call(arguments, 1);
222
- // localNames.forEach(function(key){
223
- // if (key in api)
224
- // throw new Error('ALREADY_DEFINED', 'You are shadowing ' + key);
225
- // api[key] = args.shift();
226
- // if(api[key] === undefined)
227
- // throw new Error('MISSING', 'Trying to require something that doesn\'t exist');
228
- // });
229
- // console.warn("requiring", name, mod, api);
230
-
231
- // factory.apply(module, [api]) || module;
232
- // });
233
-
234
- }
235
- }else{*/
236
78
localUse . forEach ( function ( item ) {
237
79
if ( item . name in api )
238
- throw new Error ( 'ALREADY_DEFINED' , ' You are shadowing name ' + item . name ) ;
80
+ throw new Error ( 'ALREADY_DEFINED: You are shadowing name ' + item . name ) ;
239
81
api [ item . name ] = simplePull ( globalObject , item . module , item . optional ) ;
240
82
} ) ;
83
+ return api ;
84
+ } ;
241
85
242
- var r = parentPull ( globalObject , name ) ;
243
- r . o [ r . k ] = factory . apply ( r . o [ r . k ] , [ api ] ) || r . o [ r . k ] ;
244
- // }
86
+ this . use = function ( a , optional ) {
87
+ flush ( ) ;
88
+ lastUse = { module : a , name : a . split ( '.' ) . pop ( ) , optional : optional } ;
89
+ return this ;
245
90
} ;
246
91
247
- this . run = function ( factory ) {
248
- // Close anything going on
92
+ this . add = function ( a , optional ) {
93
+ if ( ( a === undefined ) && ! optional )
94
+ throw new Error ( 'UNDEFINED: Requesting something local that is undefined' ) ;
249
95
flush ( ) ;
250
- // Dereference requested stuff and flush it
251
- var localUse = toUse ;
252
- var localAdd = toAdd ;
253
- toUse = [ ] ;
254
- toAdd = [ ] ;
96
+ lastAdd = { value : a } ;
97
+ return this ;
98
+ } ;
255
99
256
- // Add local elements to the API
257
- var api = { } ;
258
- localAdd . forEach ( function ( item ) {
259
- if ( item . name in api )
260
- throw new Error ( 'ALREADY_DEFINED' , 'You are shadowing ' + item . name ) ;
261
- api [ item . name ] = item . value ;
262
- } ) ;
100
+ this . as = function ( a ) {
101
+ if ( lastUse )
102
+ lastUse . name = a ;
103
+ else if ( lastAdd )
104
+ lastAdd . name = a ;
105
+ flush ( ) ;
106
+ } ;
263
107
264
- // If AMD pattern
265
- /* if (isLoader || internalObj) {
266
- // Get dependencies names
267
- var deps = localUse.map(function(item) {
268
- return item.module.replace(/\./g, '/');
269
- });
270
- var localNames = localUse.map(function(item) {
271
- return item.name;
272
- });
273
- if (isLoader) {
274
- require(deps, function(){
275
- var args = Array.prototype.slice.call(arguments);
276
- localNames.forEach(function(key){
277
- if (key in api)
278
- throw new Error('ALREADY_DEFINED', 'You are shadowing ' + key);
279
- api[key] = args.shift();
280
- if(api[key] === undefined)
281
- throw new Error('MISSING', 'Trying to require something that doesn\'t exist');
282
- });
283
- factory.apply({}, [api]);
284
- });
285
- }
286
- // Regular pattern
287
- } else {*/
288
- localUse . forEach ( function ( item ) {
289
- if ( item . name in api )
290
- throw new Error ( 'ALREADY_DEFINED' , 'You are shadowing ' + item . name ) ;
291
- api [ item . name ] = simplePull ( globalObject , item . module , item . optional ) ;
292
- } ) ;
108
+ this . pack = function ( name , factory ) {
109
+ var api = packer ( ) ;
110
+ var r = parentPull ( globalObject , name ) ;
111
+ var ret = factory . apply ( r . o [ r . k ] , [ api ] ) ;
112
+ if ( ret )
113
+ r . o [ r . k ] = ret ;
114
+ } ;
293
115
116
+ this . run = function ( factory ) {
117
+ var api = packer ( ) ;
294
118
factory . apply ( { } , [ api ] ) ;
295
- // }
296
119
} ;
297
- } ) . apply ( jsBoot , [ window ] ) ;
298
120
121
+ } ) . apply ( jsBoot , [ window ] ) ;
0 commit comments