Skip to content

Commit 732ad40

Browse files
committed
Re: #7 - Support storing all modules inside of a global object
1 parent 702ddc9 commit 732ad40

File tree

2 files changed

+152
-45
lines changed

2 files changed

+152
-45
lines changed

src/amdclean.js

Lines changed: 133 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
publicAPI = {
3737
// Current project version number
3838
VERSION: '0.2.6',
39+
// Default Options
40+
defaultOptions: {
41+
'globalObject': false,
42+
'globalObjectName': 'amdclean'
43+
},
3944
// Environment - either node or web
4045
env: codeEnv,
4146
// All of the error messages presented to users
@@ -198,21 +203,53 @@
198203
convertToObjectDeclaration: function(obj) {
199204
var node = obj.node,
200205
moduleName = obj.moduleName,
201-
moduleReturnValue = obj.moduleReturnValue;
202-
return {
203-
'type': 'VariableDeclaration',
204-
'declarations': [
205-
{
206-
'type': 'VariableDeclarator',
207-
'id': {
208-
'type': 'Identifier',
209-
'name': moduleName
210-
},
211-
'init': moduleReturnValue
206+
moduleReturnValue = obj.moduleReturnValue,
207+
options = publicAPI.options,
208+
updatedNode = (function() {
209+
if(options.globalObject === true && options.globalObjectName) {
210+
return {
211+
'type': 'ExpressionStatement',
212+
'expression': {
213+
'type': 'AssignmentExpression',
214+
'operator': '=',
215+
'left': {
216+
'type': 'MemberExpression',
217+
'computed': true,
218+
'object': {
219+
'type': 'Identifier',
220+
'name': options.globalObjectName
221+
},
222+
'property': {
223+
'type': 'Literal',
224+
'value': moduleName,
225+
'raw': "" + moduleName + ""
226+
}
227+
},
228+
"right": {
229+
"type": "CallExpression",
230+
"callee": moduleReturnValue,
231+
"arguments": []
232+
}
233+
}
234+
};
235+
} else {
236+
return {
237+
'type': 'VariableDeclaration',
238+
'declarations': [
239+
{
240+
'type': 'VariableDeclarator',
241+
'id': {
242+
'type': 'Identifier',
243+
'name': moduleName
244+
},
245+
'init': moduleReturnValue
246+
}
247+
],
248+
'kind': 'var'
249+
};
212250
}
213-
],
214-
'kind': 'var'
215-
};
251+
}());
252+
return updatedNode;
216253
},
217254
// convertToIIFE
218255
// -------------
@@ -248,37 +285,66 @@
248285
var moduleName = obj.moduleName,
249286
callbackFuncParams = obj.callbackFuncParams,
250287
callbackFunc = obj.callbackFunc,
251-
dependencyNames = obj.dependencyNames;
252-
return {
253-
'type': 'VariableDeclaration',
254-
'declarations': [
255-
{
256-
'type': 'VariableDeclarator',
288+
dependencyNames = obj.dependencyNames,
289+
options = publicAPI.options,
290+
cb = {
291+
'type': 'CallExpression',
292+
'callee': {
293+
'type': 'FunctionExpression',
257294
'id': {
258295
'type': 'Identifier',
259-
'name': moduleName
296+
'name': ''
260297
},
261-
'init': {
262-
'type': 'CallExpression',
263-
'callee': {
264-
'type': 'FunctionExpression',
265-
'id': {
266-
'type': 'Identifier',
267-
'name': ''
298+
'params': callbackFuncParams,
299+
'defaults': [],
300+
'body': callbackFunc.body,
301+
'rest': callbackFunc.rest,
302+
'generator': callbackFunc.generator,
303+
'expression': callbackFunc.expression
304+
},
305+
'arguments': dependencyNames
306+
},
307+
updatedNode = (function() {
308+
if(options.globalObject === true && options.globalObjectName) {
309+
return {
310+
'type': 'ExpressionStatement',
311+
'expression': {
312+
'type': 'AssignmentExpression',
313+
'operator': '=',
314+
'left': {
315+
'type': 'MemberExpression',
316+
'computed': true,
317+
'object': {
318+
'type': 'Identifier',
319+
'name': options.globalObjectName
320+
},
321+
'property': {
322+
'type': 'Literal',
323+
'value': moduleName,
324+
'raw': "" + moduleName + ""
325+
}
268326
},
269-
'params': callbackFuncParams,
270-
'defaults': [],
271-
'body': callbackFunc.body,
272-
'rest': callbackFunc.rest,
273-
'generator': callbackFunc.generator,
274-
'expression': callbackFunc.expression
275-
},
276-
'arguments': dependencyNames
277-
}
327+
"right": cb
328+
}
329+
};
330+
} else {
331+
return {
332+
'type': 'VariableDeclaration',
333+
'declarations': [
334+
{
335+
'type': 'VariableDeclarator',
336+
'id': {
337+
'type': 'Identifier',
338+
'name': moduleName
339+
},
340+
'init': cb
341+
}
342+
],
343+
'kind': 'var'
344+
};
278345
}
279-
],
280-
'kind': 'var'
281-
};
346+
}());
347+
return updatedNode;
282348
},
283349
// convertToFunctionExpression
284350
// ---------------------------
@@ -498,7 +564,14 @@
498564
var code = {},
499565
ast = {},
500566
escodegenOptions = {},
501-
globalModules = obj.globalModules;
567+
globalModules = obj.globalModules,
568+
options = {};
569+
570+
publicAPI.options = publicAPI.defaultOptions;
571+
572+
if(_.isPlainObject(obj)) {
573+
publicAPI.options = options = _.extend({}, publicAPI.options, obj);
574+
}
502575
if(!_ || !_.isPlainObject) {
503576
throw new Error(publicAPI.errorMsgs.lodash);
504577
}
@@ -566,6 +639,25 @@
566639
}
567640
});
568641
}
642+
if(options.globalObject === true && options.globalObjectName) {
643+
ast.body.unshift({
644+
'type': 'VariableDeclaration',
645+
'declarations': [
646+
{
647+
'type': 'VariableDeclarator',
648+
'id': {
649+
'type': 'Identifier',
650+
'name': options.globalObjectName
651+
},
652+
'init': {
653+
'type': 'ObjectExpression',
654+
'properties': []
655+
}
656+
}
657+
],
658+
'kind': 'var'
659+
});
660+
}
569661
escodegenOptions = _.isPlainObject(obj.escodegen) ? obj.escodegen : {};
570662
return publicAPI.generateCode(ast, escodegenOptions);
571663
}

test/specs/convert.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,26 @@ describe('amdclean specs', function() {
5151

5252
it('should support the simplified CJS wrapper', function() {
5353
var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports){exports.bar = require('./bar');});",
54-
cleanedCode = amdclean.clean({ globalModules: ['example'], code: AMDcode, escodegen: { format: { compact: true } } }),
55-
standardJavaScript = "define('./modules/example',['example1','example2'],function(one,two){});";
54+
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
55+
standardJavaScript = "var foo=function (require,exports,bar){exports.bar=bar;return exports;}({},{},bar);";
56+
57+
expect(cleanedCode).toBe(standardJavaScript);
58+
});
59+
60+
it('should support global modules', function() {
61+
var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports){exports.bar = require('./bar');});",
62+
cleanedCode = amdclean.clean({ globalModules: ['foo'], code: AMDcode, escodegen: { format: { compact: true } } }),
63+
standardJavaScript = "var foo=function (require,exports,bar){exports.bar=bar;return exports;}({},{},bar);window.foo=foo;";
5664

57-
console.log('cleanedCode', cleanedCode);
58-
// expect(cleanedCode).toBe(standardJavaScript);
65+
expect(cleanedCode).toBe(standardJavaScript);
66+
});
67+
68+
it('should support storing modules inside of a global object', function() {
69+
var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports){exports.bar = require('./bar');});",
70+
cleanedCode = amdclean.clean({ globalObject: true, globalObjectName: 'yeabuddy', code: AMDcode, escodegen: { format: { compact: true } } }),
71+
standardJavaScript = "var yeabuddy={};yeabuddy['foo']=function (require,exports,bar){exports.bar=bar;return exports;}({},{},bar);";
72+
73+
expect(cleanedCode).toBe(standardJavaScript);
5974
});
6075

6176
});

0 commit comments

Comments
 (0)