|
36 | 36 | publicAPI = { |
37 | 37 | // Current project version number |
38 | 38 | VERSION: '0.2.6', |
| 39 | + // Default Options |
| 40 | + defaultOptions: { |
| 41 | + 'globalObject': false, |
| 42 | + 'globalObjectName': 'amdclean' |
| 43 | + }, |
39 | 44 | // Environment - either node or web |
40 | 45 | env: codeEnv, |
41 | 46 | // All of the error messages presented to users |
|
198 | 203 | convertToObjectDeclaration: function(obj) { |
199 | 204 | var node = obj.node, |
200 | 205 | 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 | + }; |
212 | 250 | } |
213 | | - ], |
214 | | - 'kind': 'var' |
215 | | - }; |
| 251 | + }()); |
| 252 | + return updatedNode; |
216 | 253 | }, |
217 | 254 | // convertToIIFE |
218 | 255 | // ------------- |
|
248 | 285 | var moduleName = obj.moduleName, |
249 | 286 | callbackFuncParams = obj.callbackFuncParams, |
250 | 287 | 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', |
257 | 294 | 'id': { |
258 | 295 | 'type': 'Identifier', |
259 | | - 'name': moduleName |
| 296 | + 'name': '' |
260 | 297 | }, |
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 | + } |
268 | 326 | }, |
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 | + }; |
278 | 345 | } |
279 | | - ], |
280 | | - 'kind': 'var' |
281 | | - }; |
| 346 | + }()); |
| 347 | + return updatedNode; |
282 | 348 | }, |
283 | 349 | // convertToFunctionExpression |
284 | 350 | // --------------------------- |
|
498 | 564 | var code = {}, |
499 | 565 | ast = {}, |
500 | 566 | 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 | + } |
502 | 575 | if(!_ || !_.isPlainObject) { |
503 | 576 | throw new Error(publicAPI.errorMsgs.lodash); |
504 | 577 | } |
|
566 | 639 | } |
567 | 640 | }); |
568 | 641 | } |
| 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 | + } |
569 | 661 | escodegenOptions = _.isPlainObject(obj.escodegen) ? obj.escodegen : {}; |
570 | 662 | return publicAPI.generateCode(ast, escodegenOptions); |
571 | 663 | } |
|
0 commit comments