Skip to content

Commit

Permalink
Re: #7 - Support storing all modules inside of a global object
Browse files Browse the repository at this point in the history
  • Loading branch information
gfranko committed Nov 25, 2013
1 parent 702ddc9 commit 732ad40
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 45 deletions.
174 changes: 133 additions & 41 deletions src/amdclean.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
publicAPI = {
// Current project version number
VERSION: '0.2.6',
// Default Options
defaultOptions: {
'globalObject': false,
'globalObjectName': 'amdclean'
},
// Environment - either node or web
env: codeEnv,
// All of the error messages presented to users
Expand Down Expand Up @@ -198,21 +203,53 @@
convertToObjectDeclaration: function(obj) {
var node = obj.node,
moduleName = obj.moduleName,
moduleReturnValue = obj.moduleReturnValue;
return {
'type': 'VariableDeclaration',
'declarations': [
{
'type': 'VariableDeclarator',
'id': {
'type': 'Identifier',
'name': moduleName
},
'init': moduleReturnValue
moduleReturnValue = obj.moduleReturnValue,
options = publicAPI.options,
updatedNode = (function() {
if(options.globalObject === true && options.globalObjectName) {
return {
'type': 'ExpressionStatement',
'expression': {
'type': 'AssignmentExpression',
'operator': '=',
'left': {
'type': 'MemberExpression',
'computed': true,
'object': {
'type': 'Identifier',
'name': options.globalObjectName
},
'property': {
'type': 'Literal',
'value': moduleName,
'raw': "" + moduleName + ""
}
},
"right": {
"type": "CallExpression",
"callee": moduleReturnValue,
"arguments": []
}
}
};
} else {
return {
'type': 'VariableDeclaration',
'declarations': [
{
'type': 'VariableDeclarator',
'id': {
'type': 'Identifier',
'name': moduleName
},
'init': moduleReturnValue
}
],
'kind': 'var'
};
}
],
'kind': 'var'
};
}());
return updatedNode;
},
// convertToIIFE
// -------------
Expand Down Expand Up @@ -248,37 +285,66 @@
var moduleName = obj.moduleName,
callbackFuncParams = obj.callbackFuncParams,
callbackFunc = obj.callbackFunc,
dependencyNames = obj.dependencyNames;
return {
'type': 'VariableDeclaration',
'declarations': [
{
'type': 'VariableDeclarator',
dependencyNames = obj.dependencyNames,
options = publicAPI.options,
cb = {
'type': 'CallExpression',
'callee': {
'type': 'FunctionExpression',
'id': {
'type': 'Identifier',
'name': moduleName
'name': ''
},
'init': {
'type': 'CallExpression',
'callee': {
'type': 'FunctionExpression',
'id': {
'type': 'Identifier',
'name': ''
'params': callbackFuncParams,
'defaults': [],
'body': callbackFunc.body,
'rest': callbackFunc.rest,
'generator': callbackFunc.generator,
'expression': callbackFunc.expression
},
'arguments': dependencyNames
},
updatedNode = (function() {
if(options.globalObject === true && options.globalObjectName) {
return {
'type': 'ExpressionStatement',
'expression': {
'type': 'AssignmentExpression',
'operator': '=',
'left': {
'type': 'MemberExpression',
'computed': true,
'object': {
'type': 'Identifier',
'name': options.globalObjectName
},
'property': {
'type': 'Literal',
'value': moduleName,
'raw': "" + moduleName + ""
}
},
'params': callbackFuncParams,
'defaults': [],
'body': callbackFunc.body,
'rest': callbackFunc.rest,
'generator': callbackFunc.generator,
'expression': callbackFunc.expression
},
'arguments': dependencyNames
}
"right": cb
}
};
} else {
return {
'type': 'VariableDeclaration',
'declarations': [
{
'type': 'VariableDeclarator',
'id': {
'type': 'Identifier',
'name': moduleName
},
'init': cb
}
],
'kind': 'var'
};
}
],
'kind': 'var'
};
}());
return updatedNode;
},
// convertToFunctionExpression
// ---------------------------
Expand Down Expand Up @@ -498,7 +564,14 @@
var code = {},
ast = {},
escodegenOptions = {},
globalModules = obj.globalModules;
globalModules = obj.globalModules,
options = {};

publicAPI.options = publicAPI.defaultOptions;

if(_.isPlainObject(obj)) {
publicAPI.options = options = _.extend({}, publicAPI.options, obj);
}
if(!_ || !_.isPlainObject) {
throw new Error(publicAPI.errorMsgs.lodash);
}
Expand Down Expand Up @@ -566,6 +639,25 @@
}
});
}
if(options.globalObject === true && options.globalObjectName) {
ast.body.unshift({
'type': 'VariableDeclaration',
'declarations': [
{
'type': 'VariableDeclarator',
'id': {
'type': 'Identifier',
'name': options.globalObjectName
},
'init': {
'type': 'ObjectExpression',
'properties': []
}
}
],
'kind': 'var'
});
}
escodegenOptions = _.isPlainObject(obj.escodegen) ? obj.escodegen : {};
return publicAPI.generateCode(ast, escodegenOptions);
}
Expand Down
23 changes: 19 additions & 4 deletions test/specs/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,26 @@ describe('amdclean specs', function() {

it('should support the simplified CJS wrapper', function() {
var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports){exports.bar = require('./bar');});",
cleanedCode = amdclean.clean({ globalModules: ['example'], code: AMDcode, escodegen: { format: { compact: true } } }),
standardJavaScript = "define('./modules/example',['example1','example2'],function(one,two){});";
cleanedCode = amdclean.clean({ code: AMDcode, escodegen: { format: { compact: true } } }),
standardJavaScript = "var foo=function (require,exports,bar){exports.bar=bar;return exports;}({},{},bar);";

expect(cleanedCode).toBe(standardJavaScript);
});

it('should support global modules', function() {
var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports){exports.bar = require('./bar');});",
cleanedCode = amdclean.clean({ globalModules: ['foo'], code: AMDcode, escodegen: { format: { compact: true } } }),
standardJavaScript = "var foo=function (require,exports,bar){exports.bar=bar;return exports;}({},{},bar);window.foo=foo;";

console.log('cleanedCode', cleanedCode);
// expect(cleanedCode).toBe(standardJavaScript);
expect(cleanedCode).toBe(standardJavaScript);
});

it('should support storing modules inside of a global object', function() {
var AMDcode = "define('foo', ['require', 'exports', './bar'], function(require, exports){exports.bar = require('./bar');});",
cleanedCode = amdclean.clean({ globalObject: true, globalObjectName: 'yeabuddy', code: AMDcode, escodegen: { format: { compact: true } } }),
standardJavaScript = "var yeabuddy={};yeabuddy['foo']=function (require,exports,bar){exports.bar=bar;return exports;}({},{},bar);";

expect(cleanedCode).toBe(standardJavaScript);
});

});
Expand Down

0 comments on commit 732ad40

Please sign in to comment.