@ohos-ports/modulator 0.1.0-beta.0

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.
package/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # Modulator
2
+
3
+ Run Node.js modules in a non-CommonJS environment, say a web browser. Do this by providing a shallow representation of the filesystem, a dummy CommonJS module api.
4
+
5
+ Compiling everything into one source file. And optionally minimizing the output to a separate source file with uglify.
6
+
7
+ Be able to include big dependencies like jQuery without taking forever. Super simple easy direct use. No binaries, make your own.
8
+
9
+ ### Installation.
10
+
11
+ npm install modulator
12
+
13
+ ## Documentation.
14
+
15
+ ### How to use the build tool.
16
+
17
+ `example/build.js`
18
+
19
+ var Modulator = require('modulator');
20
+ module.exports = new Modulator({
21
+ name: 'myBuild',
22
+ main: './source',
23
+ from: __dirname + '/',
24
+ write: __dirname + '/output.js',
25
+ uglify: __dirname + '/output-min.js',
26
+ header: '//myBuild',
27
+ footer: '//' + Date(),
28
+ apiName: 'myBuild'
29
+ });
30
+
31
+ `example/source.js`
32
+
33
+ var moduleFoo = require('moduleFoo'),
34
+ scriptBar = require('./scriptBar');
35
+ exports.foo = moduleFoo;
36
+ exports.bar = scriptBar;
37
+
38
+ `./node_modules/moduleFoo/index.js`
39
+
40
+ exports.name = 'moduleFoo';
41
+
42
+ `example/scriptBar.js`
43
+
44
+ exports.name = 'scriptBar';
45
+
46
+ `example/output.js`
47
+
48
+ //myBuild
49
+ (function(){
50
+ var myBuild = (function(){"use strict";
51
+ ...
52
+ }).call(this);
53
+ myBuild.provide("/source", function (require, module, exports) {
54
+ ...
55
+ });
56
+ myBuild.provide("/scriptBar", function (require, module, exports) {
57
+ ...
58
+ });
59
+ myBuild.provide("/node_modules/moduleFoo/index", function (require, module, exports) {
60
+ ...
61
+ });
62
+ (function(){"use strict";
63
+ ...
64
+ }).call(this);
65
+ }).call(this);
66
+ //Fri Dec 09 2011 20:39:29 GMT-0800 (PST)
67
+
68
+ ### How to use from the browser.
69
+
70
+ Now we're running `example/myBuild.js` in a web page.
71
+
72
+ <script src="example/output.js" type="text/javascript" charset="utf-8"></script>
73
+
74
+ From another script on the page we can access our module's exports from `myBuild.js` from `this.myBuildName` or `window.myBuildName`.
75
+
76
+ var myBuild = this.myBuildName;
77
+
78
+ Additionally we can require modules and files which `example/myBuild.js` required.
79
+
80
+ var moduleFoo = myBuild.require('moduleFoo'),
81
+ scriptBar = myBuild.require('./scriptBar');
82
+
83
+ If you decide you don't want myBuild polluting your global namespace you can noConflict it like you would with jQuery.
84
+
85
+ myBuild = this.myBuildName.noConflict();
86
+
87
+
88
+ ## MIT License
89
+
90
+ Copyright (C) 2011 by Roland Poulter
91
+
92
+ Permission is hereby granted, free of charge, to any person obtaining a copy
93
+ of this software and associated documentation files (the "Software"), to deal
94
+ in the Software without restriction, including without limitation the rights
95
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
96
+ copies of the Software, and to permit persons to whom the Software is
97
+ furnished to do so, subject to the following conditions:
98
+
99
+ The above copyright notice and this permission notice shall be included in
100
+ all copies or substantial portions of the Software.
101
+
102
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
103
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
104
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
105
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
106
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
107
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
108
+ THE SOFTWARE.
package/lib/JSLexer.js ADDED
@@ -0,0 +1,425 @@
1
+ /*global require module console*/
2
+ (function(){"use strict";
3
+ function JSLexer () {
4
+ return this.initJSLexer();
5
+ }
6
+
7
+ var JSLexer_ = JSLexer._ = function JSLexer_ () {return this;},
8
+ lex = JSLexer_.prototype = JSLexer.prototype;
9
+
10
+ JSLexer.check = function (obj) {
11
+ return obj && obj.initJSLexer === lex.initJSLexer;
12
+ };
13
+
14
+ JSLexer.create = function (obj, args) {
15
+ obj = obj || new JSLexer_();
16
+
17
+ obj.initJSLexer = lex.initJSLexer || function () {
18
+ return this.empty();
19
+ };
20
+
21
+ obj.empty = function () {
22
+ this.tokens = [];
23
+ return this;
24
+ };
25
+
26
+ obj.tokenize = function (src) {
27
+ if (this.tokens.length) {
28
+ return this;
29
+ }
30
+ this.source = src;
31
+ this.token = null;
32
+ this.line = 0;
33
+ this.ind = 0;
34
+ this.cur = '';
35
+ this.val = '';
36
+ var end = this.source.length,
37
+ mem;
38
+ while (this.ind < end) {
39
+ this.cur = this.source.charAt(this.ind);
40
+ if (this.config.whitespace[this.cur]) {
41
+ this.newToken('white', this.cur);
42
+ this.ind += 1;
43
+ } else if (this.config.lineTerminator[this.cur]) {
44
+ if (this.cur === '\u000D' && this.source.charAt(this.ind + 1) === '\u000A') {
45
+ this.newToken('line', '\u000D\u000A');
46
+ this.ind += 1;
47
+ } else {
48
+ this.newToken('line', this.cur);
49
+ }
50
+ this.line += 1;
51
+ this.ind += 1;
52
+ } else if (this.cur === '"' || this.cur === '\'') {
53
+ this.val = '';
54
+ mem = this.cur;
55
+ this.raw = mem;
56
+ this.ind += 1;
57
+ this.cur = this.source.charAt(this.ind);
58
+ while (this.cur !== mem && this.ind < end) {
59
+ if (this.cur === '\\') {
60
+ this.ind += 1;
61
+ this.raw += this.cur;
62
+ this.cur = this.source.charAt(this.ind);
63
+ if (this.isAcsiiEscape()) {
64
+ this.ind += 1;
65
+ this.raw += this.source.substr(this.ind - 1, 3);
66
+ this.val += String.fromCharCode(parseInt(this.source.substr(this.ind, 2), 16));
67
+ this.ind += 1;
68
+ } else if (this.isUnicodeEscape()) {
69
+ this.ind += 1;
70
+ this.raw += this.source.substr(this.ind - 1, 5);
71
+ this.val += String.fromCharCode(parseInt(this.source.substr(this.ind, 4), 16));
72
+ this.ind += 3;
73
+ } else if (this.cur >= '0' && this.cur <= '7') {
74
+ this.mem = this.val;
75
+ this.val = this.cur;
76
+ this.raw += this.cur;
77
+ this.cur = this.source.charAt(this.ind + 1);
78
+ if (this.val <= '3') {
79
+ if (this.cur >= '0' && this.cur <= '7') {
80
+ this.raw += this.cur;
81
+ this.advance();
82
+ if (this.cur >= '0' && this.cur <= '7') {
83
+ this.val += this.cur;
84
+ this.raw += this.cur;
85
+ this.ind += 1;
86
+ }
87
+ }
88
+ } else if (this.cur >= '0' && this.cur <= '7') {
89
+ this.val += this.cur;
90
+ this.raw += this.cur;
91
+ this.ind += 1;
92
+ }
93
+ this.val = this.mem + String.fromCharCode(parseInt(this.val, 8));
94
+ delete this.mem;
95
+ } else {
96
+ this.cur = this.config.singleEscape[this.cur] || this.cur;
97
+ this.val += this.cur;
98
+ this.raw += this.cur;
99
+ }
100
+ } else {
101
+ this.val += this.cur;
102
+ this.raw += this.cur;
103
+ }
104
+ this.ind += 1;
105
+ this.cur = this.source.charAt(this.ind);
106
+ }
107
+ this.ind += 1;
108
+ this.newToken('string', this.val);
109
+ this.token.raw = this.raw + mem;
110
+ delete this.raw;
111
+ } else if (this.isDigit()) {
112
+ this.val = '';
113
+ this.raw = '';
114
+ mem = this.tokens[this.tokens.length - 1];
115
+ if (mem && (mem.type === 'identity' || mem.type === 'number')) {
116
+ if (console && console.log) {
117
+ console.log ('A number cannot follow a identity or number.');
118
+ }
119
+ }
120
+ if (this.cur === '0' && ('xX').indexOf(this.source.charAt(this.ind + 1)) > -1 &&
121
+ (this.ind + 1 < end)) {
122
+ this.val = '';
123
+ this.ind += 1;
124
+ this.raw = '0' + this.source.charAt(this.ind);
125
+ this.ind += 1;
126
+ this.cur = this.source.charAt(this.ind);
127
+ while (this.config.hexDigit[this.cur] && this.ind < end) {
128
+ this.raw += this.cur;
129
+ this.advance();
130
+ }
131
+ this.val = parseInt(this.val, 16);
132
+ } else {
133
+ mem = false;
134
+ do {
135
+ this.advance();
136
+ } while (this.isDigit() && this.ind < end);
137
+ if (this.cur === '.') {
138
+ mem = true;
139
+ do {
140
+ this.advance();
141
+ } while (this.isDigit() && this.ind < end);
142
+ }
143
+ if (this.hasExponent()) {
144
+ mem = true;
145
+ this.advance();
146
+ if (('-+').indexOf(this.cur) > -1) {
147
+ this.advance();
148
+ }
149
+ do {
150
+ this.advance();
151
+ } while (this.isDigit() && this.ind < end);
152
+ }
153
+ this.raw = this.val;
154
+ this.val = mem ? parseFloat(this.val) : parseInt(this.val, 10);
155
+ }
156
+ this.newToken('number', this.val);
157
+ this.token.raw = this.raw;
158
+ delete this.raw;
159
+ } else if (this.isIdentifierStart()) {
160
+ this.val = '';
161
+ do {
162
+ this.advance();
163
+ } while (this.ind < end && this.isIdentifierPart());
164
+ this.newToken('identity', this.val);
165
+ } else if (this.cur === '/') {
166
+ this.ind += 1;
167
+ this.cur = this.source.charAt(this.ind);
168
+ if (this.cur === '*') {
169
+ this.val = '/*';
170
+ this.ind += 1;
171
+ this.cur = this.source.charAt(this.ind);
172
+ while (this.ind < end) {
173
+ if (this.cur === '*' && this.source.charAt(this.ind + 1) === '/') {
174
+ this.val += '*/';
175
+ this.ind += 2;
176
+ break;
177
+ }
178
+ this.advance();
179
+ }
180
+ this.newToken('comment', this.val);
181
+ } else if (this.cur === '/') {
182
+ this.val = '//';
183
+ this.ind += 1;
184
+ this.cur = this.source.charAt(this.ind);
185
+ while (!this.config.lineTerminator[this.cur] && this.ind < end) {
186
+ this.advance();
187
+ }
188
+ this.newToken('comment', this.val);
189
+ } else {
190
+ this.val = '/';
191
+ mem = this.ind;
192
+ while (true) {
193
+ if (this.ind > end || this.config.lineTerminator[this.cur]) {
194
+ this.ind = mem;
195
+ this.cur = this.source.charAt(this.ind);
196
+ if (this.cur === '=') {
197
+ this.newToken('punctuator', '/=');
198
+ this.ind += 1;
199
+ }
200
+ else this.newToken('punctuator', '/');
201
+ break;
202
+ } else if (this.cur === '\\') {
203
+ this.advance();
204
+ if (!this.config.lineTerminator[this.cur] && this.ind < end) {
205
+ this.val += this.cur;
206
+ this.ind += 1;
207
+ }
208
+ } else if (this.cur === '[') {
209
+ while (!this.config.lineTerminator[this.cur] && this.ind < end) {
210
+ this.advance();
211
+ if (this.cur === ']') {
212
+ this.advance();
213
+ break;
214
+ } else if (this.cur === '\\') {
215
+ if (!this.config.lineTerminator[this.cur] && this.ind < end) {
216
+ this.advance();
217
+ }
218
+ }
219
+ }
220
+ } else if (this.cur === '/') {
221
+ this.advance();
222
+ while (this.cur && ('gim').indexOf(this.cur) > -1 && this.ind < end) {
223
+ this.advance();
224
+ }
225
+ this.newToken('regex', this.val);
226
+ break;
227
+ } else {
228
+ this.val += this.cur;
229
+ this.ind += 1;
230
+ }
231
+ this.cur = this.source.charAt(this.ind);
232
+ }
233
+ }
234
+ } else if (this.config.punctuator[this.cur]) {
235
+ this.val = this.cur;
236
+ this.ind += 1;
237
+ this.cur = this.source.charAt(this.ind);
238
+ if (this.val === '<' || this.val === '>') {
239
+ if (this.cur === '=') {
240
+ this.val += this.cur;
241
+ this.ind += 1;
242
+ } else if (this.cur === this.val) {
243
+ this.advance();
244
+ if (this.cur === '=') {
245
+ this.val += this.cur;
246
+ this.ind += 1;
247
+ } else if (this.val === '>>') {
248
+ this.advance();
249
+ if (this.cur === '=') {
250
+ this.val += this.cur;
251
+ this.ind += 1;
252
+ } else if (this.cur === '>') {
253
+ this.advance();
254
+ if (this.cur === '=') {
255
+ this.val += this.cur;
256
+ this.ind += 1;
257
+ }
258
+ }
259
+ }
260
+ }
261
+ } else if ((this.val === '!' || this.val === '=') && this.cur === '=') {
262
+ this.advance();
263
+ if (this.cur === '=') {
264
+ this.val += this.cur;
265
+ this.ind += 1;
266
+ }
267
+ } else if (('&|+-').indexOf(this.val) > -1 && (this.cur === '=' || this.cur === this.val)) {
268
+ this.val += this.cur;
269
+ this.ind += 1;
270
+ } else if (('*%^').indexOf(this.val) > -1 && this.cur === '=') {
271
+ this.val += this.cur;
272
+ this.ind += 1;
273
+ }
274
+ this.newToken('punctuator', this.val);
275
+ } else {
276
+ this.ind += 1;
277
+ }
278
+ }
279
+ this.lines = this.line;
280
+ delete this.source;
281
+ delete this.token;
282
+ delete this.line;
283
+ delete this.ind;
284
+ delete this.cur;
285
+ delete this.val;
286
+ return this;
287
+ };
288
+
289
+ obj.isDigit = function () {
290
+ return this.cur >= '0' && this.cur <= '9';
291
+ };
292
+
293
+ obj.isAcsiiEscape = function () {
294
+ return this.cur === 'x' &&
295
+ this.config.hexDigit[this.source.charAt(this.ind + 1)] &&
296
+ this.config.hexDigit[this.source.charAt(this.ind + 2)];
297
+ };
298
+
299
+ obj.isUnicodeEscape = function () {
300
+ return this.cur === 'u' &&
301
+ this.config.hexDigit[this.source.charAt(this.ind + 1)] &&
302
+ this.config.hexDigit[this.source.charAt(this.ind + 2)] &&
303
+ this.config.hexDigit[this.source.charAt(this.ind + 3)] &&
304
+ this.config.hexDigit[this.source.charAt(this.ind + 4)];
305
+ };
306
+
307
+ obj.hasExponent = function () {
308
+ var n1 = this.source.charAt(this.ind + 1),
309
+ n2 = this.source.charAt(this.ind + 2);
310
+ return ('eE').indexOf(this.cur) && (
311
+ this.isDigit(n1) || (('-+').indexOf(n1) > -1 && this.isDigit(n2))
312
+ );
313
+ };
314
+
315
+ obj.isIdentifierStart = function () {
316
+ var config = this.config,
317
+ truth = false;
318
+ if (config.simpleIdenityExp.test(this.cur) ||
319
+ (config.unicodeIdentity && config.unicodeLetterExp.test(this.cur))) {
320
+ return true;
321
+ }
322
+ if (this.cur === '\\') {
323
+ this.ind += 1;
324
+ this.cur = this.source.charAt(this.ind);
325
+ truth = this.isUnicodeEscape();
326
+ this.ind -= 1;
327
+ this.cur = this.source.charAt(this.ind);
328
+ }
329
+ return truth;
330
+ };
331
+
332
+ obj.isIdentifierPart = function () {
333
+ return this.isIdentifierStart() ||
334
+ this.config.identifierPartExp.test(this.cur) || this.cur === '\u200C';
335
+ };
336
+
337
+ obj.newToken = function (type, value) {
338
+ var t = [];
339
+ t.type = type;
340
+ t.text = value;
341
+ t.line = this.line;
342
+ t.tokenIndex = this.tokens.length;
343
+ this.tokens.push(this.token = t);
344
+ return this;
345
+ };
346
+
347
+ obj.advance = function () {
348
+ this.val += this.cur;
349
+ this.ind += 1;
350
+ this.cur = this.source.charAt(this.ind);
351
+ return this;
352
+ };
353
+
354
+ function parseUnicodeRegExp (str) {
355
+ return new RegExp(str.replace(/[0-9A-F]{4}/g, '\\u$&'));
356
+ }
357
+
358
+ obj.config = lex.config ? Object.create(lex.config) : {
359
+ whitespace: {
360
+ '\u0009': true, //Tab - Tab
361
+ '\u000B': true, //VT - Vetical Tab
362
+ '\u000C': true, //FF - Form Feed
363
+ ' ': true,//'\u0020': true, //SP - Space
364
+ '\u00A0': true, //NBSP - No-break space
365
+ '\uFEFF': true, //BOM - Byte Order Mark
366
+ // Other Unicode Zs
367
+ '\u0085': true, '\u1680': true, '\u180E': true,
368
+ '\u2000': true, '\u2001': true, '\u2002': true,
369
+ '\u2003': true, '\u2004': true, '\u2005': true,
370
+ '\u2006': true, '\u2007': true, '\u2008': true,
371
+ '\u2009': true, '\u200A': true, '\u200B': true,
372
+ '\u200C': true, '\u200D': true, '\u2028': true,
373
+ '\u2029': true, '\u202f': true, '\u205F': true,
374
+ '\u2060': true, '\u2800': true, '\u3000': true
375
+ },
376
+ lineTerminator: {
377
+ '\u000A': true, //LF - Line Feed
378
+ '\u000D': true, //CR - Carriage Return
379
+ '\u2028': true, //LS - Line separator
380
+ '\u2029': true //PS - Paragraph separator
381
+ },
382
+ hexDigit: {
383
+ 0: true, 1: true, 2: true, 3: true, 4: true,
384
+ 5: true, 6: true, 7: true, 8: true, 9: true,
385
+ a: true, b: true, c: true, d: true, e: true, f: true,
386
+ A: true, B: true, C: true, D: true, E: true, F: true
387
+ },
388
+ singleEscape: {
389
+ '\\': '\\',
390
+ '\'': '\'',
391
+ '"': '"',
392
+ 'b': '\b',
393
+ 'f': '\f',
394
+ 'n': '\n',
395
+ 'r': '\r',
396
+ 't': '\t',
397
+ 'v': '\v'
398
+ },
399
+ punctuator: {
400
+ '{': true, '}': true, '(': true, ')': true, '[': true, ']': true,
401
+ '.': true, ';': true, ',': true, '<': true, '>': true,
402
+ '+': true, '-': true, '*': true, '%': true,
403
+ '&': true, '|': true, '^': true, '!': true,
404
+ '~': true, '?': true, ':': true, '=': true
405
+ // <=, >=, ==, !=, ===, !==, +=, -=,
406
+ // *=, %=, &=, |=. ^=, <<=, >>=, >>>=
407
+ // ++, --, <<, >>, >>>, &&, ||
408
+ },
409
+ simpleIdenityExp: /[A-Za-z0-9$_]/,
410
+ unicodeIdentity: true,
411
+ unicodeLetterExp: (function(){
412
+ return parseUnicodeRegExp('[0061-007A00AA00B500BA00DF-00F600F8-00FF01010103010501070109010B010D010F01110113011501170119011B011D011F01210123012501270129012B012D012F01310133013501370138013A013C013E014001420144014601480149014B014D014F01510153015501570159015B015D015F01610163016501670169016B016D016F0171017301750177017A017C017E-0180018301850188018C018D019201950199-019B019E01A101A301A501A801AA01AB01AD01B001B401B601B901BA01BD-01BF01C601C901CC01CE01D001D201D401D601D801DA01DC01DD01DF01E101E301E501E701E901EB01ED01EF01F001F301F501F901FB01FD01FF02010203020502070209020B020D020F02110213021502170219021B021D021F02210223022502270229022B022D022F02310233-0239023C023F0240024202470249024B024D024F-02930295-02AF037103730377037B-037D039003AC-03CE03D003D103D5-03D703D903DB03DD03DF03E103E303E503E703E903EB03ED03EF-03F303F503F803FB03FC0430-045F04610463046504670469046B046D046F04710473047504770479047B047D047F0481048B048D048F04910493049504970499049B049D049F04A104A304A504A704A904AB04AD04AF04B104B304B504B704B904BB04BD04BF04C204C404C604C804CA04CC04CE04CF04D104D304D504D704D904DB04DD04DF04E104E304E504E704E904EB04ED04EF04F104F304F504F704F904FB04FD04FF05010503050505070509050B050D050F05110513051505170519051B051D051F0521052305250561-05871D00-1D2B1D62-1D771D79-1D9A1E011E031E051E071E091E0B1E0D1E0F1E111E131E151E171E191E1B1E1D1E1F1E211E231E251E271E291E2B1E2D1E2F1E311E331E351E371E391E3B1E3D1E3F1E411E431E451E471E491E4B1E4D1E4F1E511E531E551E571E591E5B1E5D1E5F1E611E631E651E671E691E6B1E6D1E6F1E711E731E751E771E791E7B1E7D1E7F1E811E831E851E871E891E8B1E8D1E8F1E911E931E95-1E9D1E9F1EA11EA31EA51EA71EA91EAB1EAD1EAF1EB11EB31EB51EB71EB91EBB1EBD1EBF1EC11EC31EC51EC71EC91ECB1ECD1ECF1ED11ED31ED51ED71ED91EDB1EDD1EDF1EE11EE31EE51EE71EE91EEB1EED1EEF1EF11EF31EF51EF71EF91EFB1EFD1EFF-1F071F10-1F151F20-1F271F30-1F371F40-1F451F50-1F571F60-1F671F70-1F7D1F80-1F871F90-1F971FA0-1FA71FB0-1FB41FB61FB71FBE1FC2-1FC41FC61FC71FD0-1FD31FD61FD71FE0-1FE71FF2-1FF41FF61FF7210A210E210F2113212F21342139213C213D2146-2149214E21842C30-2C5E2C612C652C662C682C6A2C6C2C712C732C742C76-2C7C2C812C832C852C872C892C8B2C8D2C8F2C912C932C952C972C992C9B2C9D2C9F2CA12CA32CA52CA72CA92CAB2CAD2CAF2CB12CB32CB52CB72CB92CBB2CBD2CBF2CC12CC32CC52CC72CC92CCB2CCD2CCF2CD12CD32CD52CD72CD92CDB2CDD2CDF2CE12CE32CE42CEC2CEE2D00-2D25A641A643A645A647A649A64BA64DA64FA651A653A655A657A659A65BA65DA65FA663A665A667A669A66BA66DA681A683A685A687A689A68BA68DA68FA691A693A695A697A723A725A727A729A72BA72DA72F-A731A733A735A737A739A73BA73DA73FA741A743A745A747A749A74BA74DA74FA751A753A755A757A759A75BA75DA75FA761A763A765A767A769A76BA76DA76FA771-A778A77AA77CA77FA781A783A785A787A78CFB00-FB06FB13-FB17FF41-FF5A]|[0041-005A00C0-00D600D8-00DE01000102010401060108010A010C010E01100112011401160118011A011C011E01200122012401260128012A012C012E01300132013401360139013B013D013F0141014301450147014A014C014E01500152015401560158015A015C015E01600162016401660168016A016C016E017001720174017601780179017B017D018101820184018601870189-018B018E-0191019301940196-0198019C019D019F01A001A201A401A601A701A901AC01AE01AF01B1-01B301B501B701B801BC01C401C701CA01CD01CF01D101D301D501D701D901DB01DE01E001E201E401E601E801EA01EC01EE01F101F401F6-01F801FA01FC01FE02000202020402060208020A020C020E02100212021402160218021A021C021E02200222022402260228022A022C022E02300232023A023B023D023E02410243-02460248024A024C024E03700372037603860388-038A038C038E038F0391-03A103A3-03AB03CF03D2-03D403D803DA03DC03DE03E003E203E403E603E803EA03EC03EE03F403F703F903FA03FD-042F04600462046404660468046A046C046E04700472047404760478047A047C047E0480048A048C048E04900492049404960498049A049C049E04A004A204A404A604A804AA04AC04AE04B004B204B404B604B804BA04BC04BE04C004C104C304C504C704C904CB04CD04D004D204D404D604D804DA04DC04DE04E004E204E404E604E804EA04EC04EE04F004F204F404F604F804FA04FC04FE05000502050405060508050A050C050E05100512051405160518051A051C051E0520052205240531-055610A0-10C51E001E021E041E061E081E0A1E0C1E0E1E101E121E141E161E181E1A1E1C1E1E1E201E221E241E261E281E2A1E2C1E2E1E301E321E341E361E381E3A1E3C1E3E1E401E421E441E461E481E4A1E4C1E4E1E501E521E541E561E581E5A1E5C1E5E1E601E621E641E661E681E6A1E6C1E6E1E701E721E741E761E781E7A1E7C1E7E1E801E821E841E861E881E8A1E8C1E8E1E901E921E941E9E1EA01EA21EA41EA61EA81EAA1EAC1EAE1EB01EB21EB41EB61EB81EBA1EBC1EBE1EC01EC21EC41EC61EC81ECA1ECC1ECE1ED01ED21ED41ED61ED81EDA1EDC1EDE1EE01EE21EE41EE61EE81EEA1EEC1EEE1EF01EF21EF41EF61EF81EFA1EFC1EFE1F08-1F0F1F18-1F1D1F28-1F2F1F38-1F3F1F48-1F4D1F591F5B1F5D1F5F1F68-1F6F1FB8-1FBB1FC8-1FCB1FD8-1FDB1FE8-1FEC1FF8-1FFB21022107210B-210D2110-211221152119-211D212421262128212A-212D2130-2133213E213F214521832C00-2C2E2C602C62-2C642C672C692C6B2C6D-2C702C722C752C7E-2C802C822C842C862C882C8A2C8C2C8E2C902C922C942C962C982C9A2C9C2C9E2CA02CA22CA42CA62CA82CAA2CAC2CAE2CB02CB22CB42CB62CB82CBA2CBC2CBE2CC02CC22CC42CC62CC82CCA2CCC2CCE2CD02CD22CD42CD62CD82CDA2CDC2CDE2CE02CE22CEB2CEDA640A642A644A646A648A64AA64CA64EA650A652A654A656A658A65AA65CA65EA662A664A666A668A66AA66CA680A682A684A686A688A68AA68CA68EA690A692A694A696A722A724A726A728A72AA72CA72EA732A734A736A738A73AA73CA73EA740A742A744A746A748A74AA74CA74EA750A752A754A756A758A75AA75CA75EA760A762A764A766A768A76AA76CA76EA779A77BA77DA77EA780A782A784A786A78BFF21-FF3A]|[01C501C801CB01F21F88-1F8F1F98-1F9F1FA8-1FAF1FBC1FCC1FFC]|[02B0-02C102C6-02D102E0-02E402EC02EE0374037A0559064006E506E607F407F507FA081A0824082809710E460EC610FC17D718431AA71C78-1C7D1D2C-1D611D781D9B-1DBF2071207F2090-20942C7D2D6F2E2F30053031-3035303B309D309E30FC-30FEA015A4F8-A4FDA60CA67FA717-A71FA770A788A9CFAA70AADDFF70FF9EFF9F]|[01BB01C0-01C3029405D0-05EA05F0-05F20621-063F0641-064A066E066F0671-06D306D506EE06EF06FA-06FC06FF07100712-072F074D-07A507B107CA-07EA0800-08150904-0939093D09500958-096109720979-097F0985-098C098F09900993-09A809AA-09B009B209B6-09B909BD09CE09DC09DD09DF-09E109F009F10A05-0A0A0A0F0A100A13-0A280A2A-0A300A320A330A350A360A380A390A59-0A5C0A5E0A72-0A740A85-0A8D0A8F-0A910A93-0AA80AAA-0AB00AB20AB30AB5-0AB90ABD0AD00AE00AE10B05-0B0C0B0F0B100B13-0B280B2A-0B300B320B330B35-0B390B3D0B5C0B5D0B5F-0B610B710B830B85-0B8A0B8E-0B900B92-0B950B990B9A0B9C0B9E0B9F0BA30BA40BA8-0BAA0BAE-0BB90BD00C05-0C0C0C0E-0C100C12-0C280C2A-0C330C35-0C390C3D0C580C590C600C610C85-0C8C0C8E-0C900C92-0CA80CAA-0CB30CB5-0CB90CBD0CDE0CE00CE10D05-0D0C0D0E-0D100D12-0D280D2A-0D390D3D0D600D610D7A-0D7F0D85-0D960D9A-0DB10DB3-0DBB0DBD0DC0-0DC60E01-0E300E320E330E40-0E450E810E820E840E870E880E8A0E8D0E94-0E970E99-0E9F0EA1-0EA30EA50EA70EAA0EAB0EAD-0EB00EB20EB30EBD0EC0-0EC40EDC0EDD0F000F40-0F470F49-0F6C0F88-0F8B1000-102A103F1050-1055105A-105D106110651066106E-10701075-1081108E10D0-10FA1100-1248124A-124D1250-12561258125A-125D1260-1288128A-128D1290-12B012B2-12B512B8-12BE12C012C2-12C512C8-12D612D8-13101312-13151318-135A1380-138F13A0-13F41401-166C166F-167F1681-169A16A0-16EA1700-170C170E-17111720-17311740-17511760-176C176E-17701780-17B317DC1820-18421844-18771880-18A818AA18B0-18F51900-191C1950-196D1970-19741980-19AB19C1-19C71A00-1A161A20-1A541B05-1B331B45-1B4B1B83-1BA01BAE1BAF1C00-1C231C4D-1C4F1C5A-1C771CE9-1CEC1CEE-1CF12135-21382D30-2D652D80-2D962DA0-2DA62DA8-2DAE2DB0-2DB62DB8-2DBE2DC0-2DC62DC8-2DCE2DD0-2DD62DD8-2DDE3006303C3041-3096309F30A1-30FA30FF3105-312D3131-318E31A0-31B731F0-31FF3400-4DB54E00-9FCBA000-A014A016-A48CA4D0-A4F7A500-A60BA610-A61FA62AA62BA66EA6A0-A6E5A7FB-A801A803-A805A807-A80AA80C-A822A840-A873A882-A8B3A8F2-A8F7A8FBA90A-A925A930-A946A960-A97CA984-A9B2AA00-AA28AA40-AA42AA44-AA4BAA60-AA6FAA71-AA76AA7AAA80-AAAFAAB1AAB5AAB6AAB9-AABDAAC0AAC2AADBAADCABC0-ABE2AC00-D7A3D7B0-D7C6D7CB-D7FBF900-FA2DFA30-FA6DFA70-FAD9FB1DFB1F-FB28FB2A-FB36FB38-FB3CFB3EFB40FB41FB43FB44FB46-FBB1FBD3-FD3DFD50-FD8FFD92-FDC7FDF0-FDFBFE70-FE74FE76-FEFCFF66-FF6FFF71-FF9DFFA0-FFBEFFC2-FFC7FFCA-FFCFFFD2-FFD7FFDA-FFDC]|[16EE-16F02160-21822185-218830073021-30293038-303AA6E6-A6EF]');
413
+ })(),
414
+ identifierPartExp: (function(){
415
+ return parseUnicodeRegExp('[0300-036F0483-04870591-05BD05BF05C105C205C405C505C70610-061A064B-065E067006D6-06DC06DF-06E406E706E806EA-06ED07110730-074A07A6-07B007EB-07F30816-0819081B-08230825-08270829-082D0900-0902093C0941-0948094D0951-095509620963098109BC09C1-09C409CD09E209E30A010A020A3C0A410A420A470A480A4B-0A4D0A510A700A710A750A810A820ABC0AC1-0AC50AC70AC80ACD0AE20AE30B010B3C0B3F0B41-0B440B4D0B560B620B630B820BC00BCD0C3E-0C400C46-0C480C4A-0C4D0C550C560C620C630CBC0CBF0CC60CCC0CCD0CE20CE30D41-0D440D4D0D620D630DCA0DD2-0DD40DD60E310E34-0E3A0E47-0E4E0EB10EB4-0EB90EBB0EBC0EC8-0ECD0F180F190F350F370F390F71-0F7E0F80-0F840F860F870F90-0F970F99-0FBC0FC6102D-10301032-10371039103A103D103E10581059105E-10601071-1074108210851086108D109D135F1712-17141732-1734175217531772177317B7-17BD17C617C9-17D317DD180B-180D18A91920-19221927192819321939-193B1A171A181A561A58-1A5E1A601A621A65-1A6C1A73-1A7C1A7F1B00-1B031B341B36-1B3A1B3C1B421B6B-1B731B801B811BA2-1BA51BA81BA91C2C-1C331C361C371CD0-1CD21CD4-1CE01CE2-1CE81CED1DC0-1DE61DFD-1DFF20D0-20DC20E120E5-20F02CEF-2CF12DE0-2DFF302A-302F3099309AA66FA67CA67DA6F0A6F1A802A806A80BA825A826A8C4A8E0-A8F1A926-A92DA947-A951A980-A982A9B3A9B6-A9B9A9BCAA29-AA2EAA31AA32AA35AA36AA43AA4CAAB0AAB2-AAB4AAB7AAB8AABEAABFAAC1ABE5ABE8ABEDFB1EFE00-FE0FFE20-FE26]|[0903093E-09400949-094C094E0982098309BE-09C009C709C809CB09CC09D70A030A3E-0A400A830ABE-0AC00AC90ACB0ACC0B020B030B3E0B400B470B480B4B0B4C0B570BBE0BBF0BC10BC20BC6-0BC80BCA-0BCC0BD70C01-0C030C41-0C440C820C830CBE0CC0-0CC40CC70CC80CCA0CCB0CD50CD60D020D030D3E-0D400D46-0D480D4A-0D4C0D570D820D830DCF-0DD10DD8-0DDF0DF20DF30F3E0F3F0F7F102B102C10311038103B103C105610571062-10641067-106D108310841087-108C108F109A-109C17B617BE-17C517C717C81923-19261929-192B193019311933-193819B0-19C019C819C91A19-1A1B1A551A571A611A631A641A6D-1A721B041B351B3B1B3D-1B411B431B441B821BA11BA61BA71BAA1C24-1C2B1C341C351CE11CF2A823A824A827A880A881A8B4-A8C3A952A953A983A9B4A9B5A9BAA9BBA9BD-A9C0AA2FAA30AA33AA34AA4DAA7BABE3ABE4ABE6ABE7ABE9ABEAABEC]|[0030-00390660-066906F0-06F907C0-07C90966-096F09E6-09EF0A66-0A6F0AE6-0AEF0B66-0B6F0BE6-0BEF0C66-0C6F0CE6-0CEF0D66-0D6F0E50-0E590ED0-0ED90F20-0F291040-10491090-109917E0-17E91810-18191946-194F19D0-19DA1A80-1A891A90-1A991B50-1B591BB0-1BB91C40-1C491C50-1C59A620-A629A8D0-A8D9A900-A909A9D0-A9D9AA50-AA59ABF0-ABF9FF10-FF19]|[005F203F20402054FE33FE34FE4D-FE4FFF3F]');
416
+ })()
417
+ };
418
+
419
+ if (args) obj.initJSLexer.apply(obj, args);
420
+
421
+ return obj;
422
+ };
423
+
424
+ (module.exports = JSLexer).create(lex);
425
+ }).call(this);
@@ -0,0 +1,561 @@
1
+ /*global require module __dirname process
2
+ setTimeout clearTimeout console*/
3
+ (function(){"use strict";
4
+ var fs = require('fs'),
5
+ ujs = require('uglify-js'),
6
+ path = require('path'),
7
+ JSLexer = require('./JSLexer');
8
+
9
+ function isFunc (func) {
10
+ return typeof func === 'function';
11
+ }
12
+
13
+ function safeApply (func, that, args) {
14
+ if (isFunc(func)) return func.apply(that, args);
15
+ }
16
+
17
+ function Modulator (options, callback) {
18
+ return this.initModulator(options, callback);
19
+ }
20
+
21
+ var Modulator_ = Modulator._ = function () {return this;},
22
+ modulator = Modulator_.prototype = Modulator.prototype,
23
+ api,
24
+ out;
25
+
26
+ Modulator.check = function (obj) {
27
+ return obj && obj.initModulator === modulator.initModulator;
28
+ };
29
+
30
+ Modulator.create = function (obj, args) {
31
+ obj = obj || new Modulator_();
32
+
33
+ obj.initModulator = modulator.initModulator || function (options, callback) {
34
+ options = options || {};
35
+ this.verbose = options.verbose || false;
36
+ this.apiName = options.apiName || 'api';
37
+ this.header = options.header || '';
38
+ this.footer = options.footer || '';
39
+ this.name = options.name || 'module';
40
+ this.from = options.from ? path.resolve(options.from) : process.cwd();
41
+ this.source = '';
42
+ this.output = [];
43
+ this.memory = {};
44
+ return this.run(options, callback);
45
+ };
46
+
47
+ obj.run = function (options, callback) {
48
+ options = options || {};
49
+ callback = callback || options.callback;
50
+ if (options.main) {
51
+ this.lastMain = options.main;
52
+ this.require(options.main, function (err) {
53
+ if (!err) {
54
+ this.compile(function (err) {
55
+ var count = 0,
56
+ that = this,
57
+ errs = [];
58
+ function end (err) {
59
+ count -= 1;
60
+ if (err) errs.push(err);
61
+ if (count === 0) {
62
+ safeApply(callback, that, [errs.length > 1 ? errs : errs[0]]);
63
+ }
64
+ }
65
+ function out () {
66
+ if (options.write) {
67
+ count += 1;
68
+ that.write(options.write, end);
69
+ }
70
+ if (options.uglify) {
71
+ count += 1;
72
+ that.uglify(options.uglify, end);
73
+ }
74
+ if (count === 0) {
75
+ safeApply(callback, that);
76
+ }
77
+ }
78
+ if (!err) {
79
+ out();
80
+ if (options.watch || options.watch === 0) {
81
+ this.watch(options.watch, function () {
82
+ callback = options.watcher;
83
+ count = 2;
84
+ out();
85
+ });
86
+ }
87
+ } else {
88
+ safeApply(callback, this, [err]);
89
+ }
90
+ });
91
+ } else {
92
+ safeApply(callback, this, [err]);
93
+ }
94
+ }, this.from, this.name);
95
+ }
96
+ return this;
97
+ };
98
+
99
+ obj.reset = function (options, callback) {
100
+ delete this.lastMain;
101
+ return this.unwatch().initModulator(options || this, callback);
102
+ };
103
+
104
+ obj.forget = function () {
105
+ this.source = '';
106
+ this.output = [];
107
+ this.memory = {};
108
+ delete this.main;
109
+ return this;
110
+ };
111
+
112
+ obj.require = function (name, callback, from) {
113
+ from = from || this.from;
114
+ this.log('require', [name, from]);
115
+ if (('./').indexOf(name.charAt(0)) !== -1) {
116
+ this.requireFileOrDirectory(from + '/' + name, callback);
117
+ } else {
118
+ this.requireNodeModule(name, callback, from);
119
+ }
120
+ return this;
121
+ };
122
+
123
+ obj.requireFile = function (file, callback) {
124
+ this.log('requireFile', file);
125
+ if (!this.main) {
126
+ this.main = this.removeFileExt(path.relative(this.from, file));
127
+ }
128
+ var that = this;
129
+ if (this.memory[file]) {
130
+ safeApply(callback, this);
131
+ } else {
132
+ fs.readFile(file, 'utf8', function (err, source) {
133
+ if (err || that.memory[file]) {
134
+ safeApply(callback, that, [err]);
135
+ } else {
136
+ source = source || '';
137
+ that.output.push({file: file, source: source});
138
+ that.rememberFile(file);
139
+ that.requireRequirments(
140
+ path.dirname(file),
141
+ path.basename(file),
142
+ source,
143
+ callback
144
+ );
145
+ }
146
+ });
147
+ }
148
+ return this;
149
+ };
150
+
151
+ obj.requireDirectory = function (dir, callback) {
152
+ if (this.memory[dir]) {
153
+ safeApply(callback, this);
154
+ return this;
155
+ }
156
+ this.log('requireDirectory', dir);
157
+ var that = this,
158
+ pack = dir + '/package.json',
159
+ main = '/index';
160
+ function end (err) {
161
+ var src;
162
+ if (!err) {
163
+ src = 'module.exports = require(".' +
164
+ path.normalize('/' + that.removeFileExt(main)) + '");';
165
+ that.memory[dir] = true;
166
+ that.output.push({
167
+ dir: true,
168
+ file: dir + '/',
169
+ source: src
170
+ });
171
+ }
172
+ safeApply(callback, that, [err]);
173
+ }
174
+ if (fs.existsSync(pack)) {
175
+ fs.readFile(pack, 'utf8', function (err, data) {
176
+ if (err) {
177
+ safeApply(callback, that, [err]);
178
+ } else try {
179
+ data = JSON.parse(data);
180
+ if (data.main) {
181
+ main = '/' + data.main;
182
+ that.requireFileOrDirectory(dir + main, end);
183
+ } else {
184
+ that.requireFileOrDirectory(dir + main, end);
185
+ }
186
+ } catch (jsonErr) {
187
+ safeApply(callback, that, [
188
+ new Error(jsonErr + ' from ' + pack)
189
+ ]);
190
+ }
191
+ });
192
+ } else {
193
+ safeApply(callback, that, [that.missingFile(dir)]);
194
+ }
195
+ return this;
196
+ };
197
+
198
+ obj.requireFileOrDirectory = function (name, callback) {
199
+ this.log('requireFileOrDirectory', name);
200
+ var clean = this.removeFileExt(path.resolve(name)),
201
+ file = this.memory[clean + '.js'],
202
+ that = this;
203
+ function end () {
204
+ that.log('end requireFileOrDirectory', name);
205
+ if (fs.existsSync(clean)) {
206
+ fs.stat(clean, function (err, stat) {
207
+ if (err) {
208
+ safeApply(callback, that, [err]);
209
+ } else if (stat.isDirectory()) {
210
+ that.requireDirectory(clean, callback);
211
+ }
212
+ });
213
+ } else {
214
+ safeApply(callback, that, [that.missingFile(clean)]);
215
+ }
216
+ }
217
+ if (file) {
218
+ safeApply(callback, this);
219
+ } else if (name.charAt(name.length - 1) !== '/') {
220
+ if (fs.existsSync(clean + '.js')) {
221
+ that.requireFile(clean + '.js', callback);
222
+ } else {
223
+ end();
224
+ }
225
+ } else {
226
+ end();
227
+ }
228
+ return this;
229
+ };
230
+
231
+ obj.requireNodeModule = function (name, callback, from) {
232
+ this.log('requireNodeModule', [name, from]);
233
+ if (this.memory[name]) {
234
+ safeApply(callback, this);
235
+ return this;
236
+ }
237
+ var that = this,
238
+ list = this.listModuleDirectories(from || this.from),
239
+ count = list.length,
240
+ found;
241
+ function end (err) {
242
+ if (count < 1 || found) return;
243
+ that.log('end requireNodeModule', [name, from]);
244
+ if (!err) found = true;
245
+ else if (!err.missingFile && !err.missongModule) {
246
+ count = 1;
247
+ }
248
+ count -= 1;
249
+ if (found || count === 0) {
250
+ if (err && err.missingFile) {
251
+ err = new Error('Missing module.');
252
+ err.missingModule = name;
253
+ }
254
+ safeApply(callback, that, [err]);
255
+ }
256
+ }
257
+ if (count) {
258
+ this.memory[name] = true;
259
+ list.forEach(function (dir) {
260
+ var f = dir + '/' + name;
261
+ that.requireFileOrDirectory(f, end, name);
262
+ });
263
+ } else {
264
+ safeApply(callback, that, ['Missing module.']);
265
+ }
266
+ return this;
267
+ };
268
+
269
+ obj.requireRequirments = function (from, name, source, callback) {
270
+ this.log('requireRequirements', [from, name]);
271
+ var count = 0,
272
+ needs = this.listNeededRequirements(source),
273
+ that = this;
274
+ function end (err) {
275
+ if (count < 1) return;
276
+ that.log('end requireRequirements', [from, name]);
277
+ count -= 1;
278
+ if (err && (!err.missingFile || err.missingModule)) {
279
+ count = 0;
280
+ }
281
+ if (count === 0) {
282
+ safeApply(callback, that, [err]);
283
+ }
284
+ }
285
+ if (needs && needs.length) {
286
+ count = needs.length;
287
+ needs.forEach(function (_name) {
288
+ that.require(_name, end, from);
289
+ });
290
+ } else {
291
+ count = 1;
292
+ end();
293
+ }
294
+ return this;
295
+ };
296
+
297
+ obj.listModuleDirectories = function (from, callback) {
298
+ this.log('listModuleDirectories', from);
299
+ var parts = from.split('/'),
300
+ root = parts.indexOf('node_modules'),
301
+ dirs = [],
302
+ i = parts.length - 1;
303
+ if (root === -1) root = 0;
304
+ while (i >= root) {
305
+ if (parts[i] === 'node_modules') {
306
+ dirs.push(parts.join('/'));
307
+ } else if (parts.length) {
308
+ dirs.push(parts.join('/') + '/node_modules');
309
+ parts.pop();
310
+ }
311
+ i -= 1;
312
+ }
313
+ return dirs;
314
+ };
315
+
316
+ obj.listNeededRequirements = function (source) {
317
+ this.log('listNeededRequirements');
318
+ var tokens = new JSLexer().tokenize(source).tokens,
319
+ needs = [];
320
+ if (tokens && tokens.length) {
321
+ tokens.forEach(function (token, ind) {
322
+ var last,
323
+ next,
324
+ i,
325
+ l;
326
+ if (token.type === 'identity' && token.text === 'require') {
327
+ i = ind;
328
+ do {
329
+ last = i && tokens[i -= 1];
330
+ } while (last && last.type === 'white');
331
+ if (!last || last.text !== '.') {
332
+ i = ind;
333
+ l = tokens.length;
334
+ do {
335
+ next = i < l && tokens[i += 1];
336
+ } while (next && next.type === 'white');
337
+ if (next.text === '(') {
338
+ do {
339
+ next = i < l && tokens[i += 1];
340
+ } while (next && next.type === 'white');
341
+ if (next.type === 'string') {
342
+ needs.push(next.text);
343
+ }
344
+ }
345
+ }
346
+ }
347
+ });
348
+ }
349
+ return needs;
350
+ };
351
+
352
+ obj.discoverModuleAPI = function (callback) {
353
+ var that = this;
354
+ function end (err) {
355
+ if (err || (api && out)) {
356
+ safeApply(callback, that, [err, api, out]);
357
+ }
358
+ }
359
+ function alt (err, data) {
360
+ api = data;
361
+ end(err);
362
+ }
363
+ if (api && out) {
364
+ end();
365
+ } else {
366
+ fs.readFile(__dirname + '/../src/api.js', 'utf8', function (err, data) {
367
+ api = data;
368
+ end(err);
369
+ });
370
+ fs.readFile(__dirname + '/../src/out.js', 'utf8', function (err, data) {
371
+ out = data;
372
+ end(err);
373
+ });
374
+ }
375
+ return this;
376
+ };
377
+
378
+ obj.rememberFile = function (file) {
379
+ this.log('rememberFile', file);
380
+ this.memory[file] = this.memory[this.removeFileExt(file)] = true;
381
+ return this;
382
+ };
383
+
384
+ obj.missingFile = function (file) {
385
+ this.log('missingFile', file);
386
+ var err = new Error('Missing file.');
387
+ err.missingFile = file;
388
+ return err;
389
+ };
390
+
391
+ obj.removeFileExt = function (file) {
392
+ return path.dirname(file) + '/' + path.basename(file, '.js');
393
+ };
394
+
395
+ obj.compileFile = function (item) {
396
+ this.log('compileFile', item.file);
397
+ var file = path.normalize(
398
+ '/' + this.removeFileExt(path.relative(this.from, item.file))
399
+ );
400
+ if (item.dir && file !== '/') {
401
+ file += '/';
402
+ }
403
+ return ([
404
+ this.apiName + '.provide("' + file + '", function' + ' (require, module, exports) {',
405
+ item.source,
406
+ '});'
407
+ ]).join('\n');
408
+ };
409
+
410
+ obj.compileOutput = function () {
411
+ this.log('compileOutput');
412
+ var that = this,
413
+ source = [];
414
+ this.output.forEach(function (item) {
415
+ source.push(that.compileFile(item));
416
+ });
417
+ return source.join('\n');
418
+ };
419
+
420
+ obj.compile = function (callback) {
421
+ this.log('compile');
422
+ if (this.source) {
423
+ safeApply(callback, this);
424
+ } else {
425
+ this.discoverModuleAPI(function (err, api, out) {
426
+ var source = '',
427
+ name = this.name;
428
+ if ((/[^a-z0-9_$]/i).test(name)) {
429
+ name = '["' + name + '"]';
430
+ } else {
431
+ name = '.' + name;
432
+ }
433
+ out = out.
434
+ replace(/\(api\)/g, this.apiName).
435
+ replace(/\(name\)/g, name).
436
+ replace(/\(main\)/g, path.normalize('/' + this.main));
437
+ if (this.header) {
438
+ source += this.header + '\n';
439
+ }
440
+ source += '(function()' + '{\n';
441
+ source += 'var ' + this.apiName + ' = ' + api + '\n';
442
+ source += this.compileOutput() + '\n';
443
+ source += out + '\n';
444
+ source += '}).call(this);';
445
+ if (this.footer) {
446
+ source += '\n' + this.footer;
447
+ }
448
+ this.source = source;
449
+ safeApply(callback, this, [err]);
450
+ });
451
+ }
452
+ return this;
453
+ };
454
+
455
+ obj.write = function (file, callback) {
456
+ var that = this;
457
+ function end (err) {
458
+ safeApply(callback, that, [err]);
459
+ }
460
+ if (this.source) {
461
+ fs.writeFile(file, this.source, 'utf8', end);
462
+ } else {
463
+ end('Empty source.');
464
+ }
465
+ return this;
466
+ };
467
+
468
+ obj.uglify = function (file, callback) {
469
+ var that = this,
470
+ src = this.source,
471
+ ast;
472
+ function end (err) {
473
+ safeApply(callback, that, [err]);
474
+ }
475
+ if (src) {
476
+ ast = ujs.parser.parse(src);
477
+ ast = ujs.uglify.ast_mangle(ast);
478
+ ast = ujs.uglify.ast_squeeze(ast);
479
+ src = ujs.uglify.gen_code(ast);
480
+ if (this.header) {
481
+ src = this.header + '\n' + src;
482
+ }
483
+ fs.writeFile(file, src, 'utf8', end);
484
+ } else {
485
+ end('Empty source.');
486
+ }
487
+ return this;
488
+ };
489
+
490
+ obj.watch = function (options, callback) {
491
+ var that = this,
492
+ name,
493
+ wait,
494
+ watch,
495
+ files = this.watchers,
496
+ persist;
497
+ options = options || {};
498
+ callback = callback || options.callback;
499
+ delete options.callback;
500
+ if (isFinite(options)) {
501
+ watch = options;
502
+ options = {};
503
+ } else {
504
+ watch = options.interval;
505
+ }
506
+ wait = options.wait;
507
+ wait = isFinite(wait) ? wait : 100;
508
+ watch = isFinite(watch) ? watch : 0;
509
+ persist = options.persist;
510
+ if (!files) {
511
+ files = this.watchers = [];
512
+ }
513
+ for (name in this.memory) {
514
+ if (this.memory[name] === true && name.substr(name.length - 3) === '.js') {
515
+ if (files.indexOf(name) === -1) {
516
+ files.push(name);
517
+ }
518
+ }
519
+ }
520
+ files.forEach(function (file) {
521
+ var timer;
522
+ fs.watchFile(file, {
523
+ persistent: persist,
524
+ interval: watch
525
+ }, function (curr, prev) {
526
+ if (curr.mtime > prev.mtime) {
527
+ clearTimeout(timer);
528
+ timer = setTimeout(function () {
529
+ that.forget().run({main: that.lastMain}, function (err) {
530
+ safeApply(callback, that, [err]);
531
+ });
532
+ }, wait);
533
+ }
534
+ });
535
+ });
536
+ return this;
537
+ };
538
+
539
+ obj.unwatch = function () {
540
+ var files = this.watchers;
541
+ if (files) {
542
+ files.forEach(function (file) {
543
+ fs.unwatchFile(file);
544
+ });
545
+ delete this.watchers;
546
+ }
547
+ return this;
548
+ };
549
+
550
+ obj.log = function () {
551
+ if (this.verbose) console.log.apply(console, arguments);
552
+ return this;
553
+ };
554
+
555
+ if (args) obj.initModulator.apply(obj, args);
556
+
557
+ return obj;
558
+ };
559
+
560
+ (module.exports = Modulator).create(modulator);
561
+ }).call(this);
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@ohos-ports/modulator",
3
+ "main": "lib/Modulator.js",
4
+ "author": "Roland Poulter",
5
+ "version": "0.1.0-beta.0",
6
+ "keywords": [
7
+ "make",
8
+ "build",
9
+ "modules",
10
+ "browser",
11
+ "require",
12
+ "module",
13
+ "exports",
14
+ "unobtrusive"
15
+ ],
16
+ "homepage": "https://github.com/rolandpoulter/node_modulator",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/ohos-ports/ohos-ports.git",
20
+ "directory": "ports/modulator/0.1.0"
21
+ },
22
+ "description": "Easy build tool for running node modules in a non-CommonJS environment.",
23
+ "contributors": [],
24
+ "dependencies": {
25
+ "uglify-js": "^1.3.5"
26
+ },
27
+ "bugs": {
28
+ "url": "https://github.com/ohos-ports/ohos-ports/issues"
29
+ }
30
+ }
package/src/api.js ADDED
@@ -0,0 +1,114 @@
1
+ (function(){"use strict";
2
+ var global = this,
3
+ require = this.require,
4
+ modules = {};
5
+ function resolveModule (path) {
6
+ var slash = path.indexOf('/'),
7
+ name,
8
+ mod,
9
+ m;
10
+ if (slash === -1) return path;
11
+ name = path.substring(0, slash);
12
+ path = path.substr(slash + 1);
13
+ mod = modules[name];
14
+ if (!mod) return path;
15
+ for (m in modules) {
16
+ if (modules[m] === mod && m !== name) {
17
+ break;
18
+ }
19
+ m = null;
20
+ }
21
+ slash = m.lastIndexOf(name + '/');
22
+ if (slash !== -1) {
23
+ m = m.substr(0, slash + name.length);
24
+ } else {
25
+ m = '';
26
+ }
27
+ return (m ? m : name) + '/' + path;
28
+ }
29
+ function resolve (path, from) {
30
+ if (path.charAt(0) === '/') return path;
31
+ if (path.charAt(0) !== '.') {
32
+ return resolveModule(path);
33
+ }
34
+ var names = path.split('/'),
35
+ clean = [],
36
+ len = names.length,
37
+ i = 0,
38
+ p;
39
+ from = from || '';
40
+ if (from.charAt(from.length - 1) === '/') {
41
+ from = from.substring(0, from.length - 1);
42
+ }
43
+ for (; i < len; i += 1) {
44
+ p = names[i];
45
+ if (p === '..') {
46
+ from = from.substring(0, from.lastIndexOf('/')) || '';
47
+ } else if (p && p !== '.') {
48
+ clean.push(p);
49
+ }
50
+ }
51
+ if (from === '/') from = '';
52
+ return from + '/' + clean.join('/');
53
+ }
54
+ function fetch (path) {
55
+ return modules[path] ||
56
+ modules[path + 'index'] ||
57
+ modules[path + '/'] ||
58
+ modules[path + '/index'];
59
+ }
60
+ return {
61
+ require: function (path, from) {
62
+ var module = fetch(path),
63
+ err = new Error('not found');
64
+ if (!module) {
65
+ path = resolve(path, from);
66
+ module = fetch(path);
67
+ }
68
+ if (module) {
69
+ if (module.install) {
70
+ module.install();
71
+ }
72
+ return module.exports;
73
+ }
74
+ err.path = path;
75
+ err.from = from;
76
+ throw err;
77
+ },
78
+ provide: function (path, factory) {
79
+ var that = this;
80
+ modules[path] = {
81
+ exports: {},
82
+ require: function (p) {
83
+ var from = path.substring(0, path.lastIndexOf('/'));
84
+ return that.require(p, from);
85
+ },
86
+ install: function () {
87
+ delete this.install;
88
+ factory.call(global, this.require, this, this.exports);
89
+ return this;
90
+ }
91
+ };
92
+ var dir = '/node_modules/',
93
+ index = path.lastIndexOf(dir),
94
+ module = index === -1 ? null : path.substr(index + dir.length);
95
+ if (module) {
96
+ modules[module] = modules[module] || modules[path];
97
+ }
98
+ return this;
99
+ },
100
+ freeze: function (path) {
101
+ var obj;
102
+ delete this.provide;
103
+ delete this.freeze;
104
+ if (path) {
105
+ obj = this.require(path);
106
+ obj.require = this.require;
107
+ obj.conflict = this.conflict;
108
+ obj.noConflict = this.noConflict;
109
+ return obj;
110
+ }
111
+ return this;
112
+ }
113
+ };
114
+ }).call(this);
package/src/out.js ADDED
@@ -0,0 +1,15 @@
1
+ (function(){"use strict";
2
+ /*global module name api main global require*/
3
+ var that = this;
4
+ if (typeof module !== 'undefined') {
5
+ module.exports = (api).freeze();
6
+ } else {
7
+ (api) = (api).freeze("(main)");
8
+ (api).conflict = this(name);
9
+ (api).noConflict = function () {
10
+ if (require) that.require = that.require.conflict;
11
+ return (that(name) = (this.conflict || this));
12
+ };
13
+ this(name) = (api);
14
+ }
15
+ }).call(this);